mpl_init.py 5.28 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import importlib as imp

from . import INFO, SEVR, WARN, DBUG, SPCE
from . import sys, os, module

def dyn_from_relative_import_as(module, submodule_name, item, alias):
    submodule = imp.import_module(module.__name__ + submodule_name, module.__name__ + submodule_name)
    setattr(module, alias, getattr(submodule, item))
    sys.modules.update({module.__name__ + '.' + alias: getattr(submodule, item)})

def dyn_from_import_as(module, submodule_name, item, alias):
    submodule = imp.import_module(submodule_name, submodule_name)
    setattr(module, alias, getattr(submodule, item))
    sys.modules.update({module.__name__ + '.' + alias: getattr(submodule, item)})

def mpl_contextualize(context=module.CONTEXT):

    if not module._initialized:
    
        if context.lower() in ('local', 'client', 'server'):
            module.CONTEXT = context.lower()
            module._initialized = _init()
        else:
            print(SEVR + 'the value of CONTEXT has to be SERVER, CLIENT or LOCAL')
            raise ImportError

    else:
        print(WARN + 'myplotlib was already contextualized: ' + module.CONTEXT)
        
    
def _init():

    if module.CONTEXT.lower() in ('local', 'client'):

        if module.CONTEXT.lower() == 'client':
            print(INFO + 'Import ' + module.__name__ + ' as a client.')
        else:
            print(INFO + 'Import ' + module.__name__ + ' for local use.')

        # Test matplotlib version
        dyn_from_import_as(module, 'matplotlib', '__version__', 'mpvers')

        if int(module.mpvers.replace('.', '')[:3]) < 143:
            print('\n\n' \
              '{warn} {sep} \n' \
              '{spce} The matplotlib version you are using is not supported.\n' \
              '{spce} Most of myplotlib should work, but some stuff may not.\n' \
              '{spce} {sep} \n\n'.format(warn=WARN, spce=SPCE, sep=72*'='))

        # matplotlib
        dyn_from_import_as(module, 'matplotlib.pyplot', 'figure', 'figure')
        dyn_from_import_as(module, 'matplotlib.pyplot', 'rc', 'rc')
        dyn_from_import_as(module, 'matplotlib.pyplot', 'show', 'show')
        dyn_from_import_as(module, 'matplotlib.pyplot', 'draw', 'draw')
        dyn_from_import_as(module, 'matplotlib.pyplot', 'ion', 'ion')
        dyn_from_import_as(module, 'matplotlib.pyplot', 'ioff', 'ioff')
        dyn_from_import_as(module, 'matplotlib.pyplot', 'clf', 'clf')
        dyn_from_import_as(module, 'matplotlib.pyplot', 'close', 'mpclose')
        dyn_from_import_as(module, 'matplotlib.pyplot', 'fignum_exists', 'fignum_exists')
        dyn_from_import_as(module, 'matplotlib.pyplot', 'savefig', 'savefig')
        dyn_from_import_as(module, 'matplotlib.axes', 'Axes', 'Axes')
        dyn_from_import_as(module, 'matplotlib.figure', 'Figure', 'Figure')
        dyn_from_import_as(module, 'matplotlib', 'rcParams', 'rcParams')
        dyn_from_import_as(module, 'matplotlib', 'is_interactive', 'is_interactive')
        dyn_from_import_as(module, 'matplotlib', 'use', 'use')

        # socket
        dyn_from_import_as(module, 'socket', 'socket', 'socket')
        dyn_from_import_as(module, 'socket', 'AF_INET', 'AF_INET')
        dyn_from_import_as(module, 'socket', 'SOCK_STREAM', 'SOCK_STREAM')

        
    elif module.CONTEXT.lower() == 'server':

        if DBUG:
            print(INFO + 'Import ' + module.__name__ + ' as a server.')

        # socket
        dyn_from_import_as(module, 'socket', 'socket', 'socket')
        dyn_from_import_as(module, 'socket', 'AF_INET', 'AF_INET')
        dyn_from_import_as(module, 'socket', 'SOCK_STREAM', 'SOCK_STREAM')
        dyn_from_import_as(module, 'socket', 'SOL_SOCKET', 'SOL_SOCKET')
        dyn_from_import_as(module, 'socket', 'SO_REUSEADDR', 'SO_REUSEADDR')
            
    else:

        print(SEVR + 'the value of CONTEXT has to be SERVER, CLIENT or LOCAL')
        raise ImportError

    if module.CONTEXT.lower() in ('client', 'local'):
        dyn_from_relative_import_as(module, '.rconfig', 'rcParams', 'rcParams')

    dyn_from_relative_import_as(module, '.mpl_axes', 'MplAxes', 'MplAxes')

    dyn_from_relative_import_as(module, '.mpl_grid', 'Grid', 'Grid')
    dyn_from_relative_import_as(module, '.mpl_grid', 'VerticalGrid', 'VerticalGrid')
    dyn_from_relative_import_as(module, '.mpl_grid', 'HorizontalGrid', 'HorizontalGrid')

    dyn_from_relative_import_as(module, '.mpl_fig', 'MplFig', 'MplFig')

    dyn_from_relative_import_as(module, '.empty_fig', 'EmptyFig', 'EmptyFig')
    dyn_from_relative_import_as(module, '.empty_axes', 'EmptyAxes', 'EmptyAxes')

    dyn_from_relative_import_as(module, '.progressbar', 'ProgressBar', 'ProgressBar')
    dyn_from_relative_import_as(module, '.mpl_client', 'MplClient', 'MplClient')

    module.SERVER_IOFUNCTIONS = {}
    module.SERVER_FIGURES = {}
    
    dyn_from_relative_import_as(module, '.mpl_server', 'MplServer', 'MplServer')
    dyn_from_relative_import_as(module, '.mpl_server', 'MplHandler', 'MplHandler')

    if module.CONTEXT.lower() in ('client', 'local'):

        module._G_WINDOWS = []

        print(module.rcParams['backend'])

        if module.rcParams['backend'] == u'TkAgg':
            dyn_from_relative_import_as(module, '.mpl_win_tkagg', 'MplWin_TkAgg', 'MplWin')
        else:
            print(SEVR + """The backend you choosed is not
                    supported interactive mode not available""")

    return(True)