Commit c382b2f8 authored by Yori's avatar Yori
Browse files

FEATURE: myplotlib can be contextualized after importing.

     - set AUTO_CONTEXTUALIZE to False in config.json
     - call myplotlib.mpl_contextualize(context)

This can be useful while designing remote figures preventing
always to reload mpl module after modifying config file.
parent 17fbfec1
Pipeline #724 failed with stage
......@@ -33,6 +33,7 @@
#
#
# IMPORT ---------------------------------------------------------------
import os
import sys
# CONFIGURATION
......@@ -40,99 +41,16 @@ from .mpl_config import load_config
module = sys.modules[__name__]
load_config(module)
import os as os
from .mpl_init import mpl_contextualize
import numpy as np
from .mpl_signal import Signal, Query, Status, Answer
from .mpl_unpickler import MplUnpickler
#np.set_printoptions(threshold='nan')
# matplotlib import for client/local -----------------------------------
if CONTEXT.lower() in ('client', 'local'):
if CONTEXT.lower() == 'client':
print(INFO + 'Import ' + __name__ + ' as a client.')
else:
print(INFO + 'Import ' + __name__ + ' for local use.')
# Test mpl version:
from matplotlib import __version__ as mplvers
if int(mplvers.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
from matplotlib.pyplot import figure
from matplotlib.pyplot import rc
from matplotlib.pyplot import show, draw, ion, ioff, clf
from matplotlib.pyplot import close as mpclose
from matplotlib.pyplot import fignum_exists, savefig
from matplotlib.axes import Axes
from matplotlib.figure import Figure
from matplotlib import rcParams
from matplotlib import is_interactive
from matplotlib import use
from socket import socket, AF_INET, SOCK_STREAM
elif CONTEXT.lower() == 'server':
if DBUG:
print(INFO + 'Import ' + __name__ + ' as a server.')
from socket import socket, AF_INET, SOCK_STREAM, SOL_SOCKET, SO_REUSEADDR
else:
print(SEVR + 'the value of CONTEXT has to be SERVER, CLIENT or LOCAL')
raise ImportError
_initialized = False
# myplotlib
# from .mpl_data import MplData
# CONFIGURATION --------------------------------------------------------
if CONTEXT.lower() in ('client', 'local'):
from .rconfig import rcParams
from .mpl_axes import MplAxes
from .mpl_grid import Grid, VerticalGrid, HorizontalGrid
from .mpl_fig import MplFig
from .empty_fig import EmptyFig
from .empty_axes import EmptyAxes
from .progressbar import ProgressBar
from .mpl_client import MplClient
SERVER_IOFUNCTIONS = {
# 'readStupidData': readStupidData,
# 'readStupidData2': readStupidData2
}
SERVER_FIGURES = {
# 'FigTest': FigTest
}
from .mpl_server import MplServer, MplHandler
if CONTEXT.lower() in ('client', 'local'):
_G_WINDOWS = []
print(rcParams['backend'])
#np.set_printoptions(threshold='nan')
# MyWin: Overlay on matplotlib.FigureManager class
if rcParams['backend'] == u'TkAgg':
from .mpl_win_tkagg import MplWin_TkAgg as MplWin
elif rcParams['backend'] == u'GTKAgg':
from .myWin_GTKAgg import MyWin_GTKAgg as MyWin
elif rcParams['backend'] == u'WXAgg':
from .myWin_WXAgg import MyWin_WXAgg as MyWin
elif rcParams['backend'] == u'Qt4Agg':
from .myWin_Qt4Agg import MyWin_Qt4Agg as MyWin
elif rcParams['backend'] == u'MacOSX':
from .myWin_MacOSx import MyWin_MacOSx as MyWin
else:
print(SEVR + """The backend you choosed is not
supported interactive mode not available""")
if AUTO_CONTEXTUALIZE:
mpl_contextualize()
{
"D_VERSION": "v5",
"CONTEXT": "local",
"AUTO_CONTEXTUALIZE": true,
"D_HOST": "localhost",
"D_PORT": 50803,
"INFO": " > info-: ",
......
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)
{
"D_VERSION": "v5",
"CONTEXT": "local",
"AUTO_CONTEXTUALIZE": true,
"D_HOST": "localhost",
"D_PORT": 50803,
"INFO": " > info-: ",
"WARN": " > warn-: ",
"SEVR": " > sevr-: ",
"DBUG": " > dbug-: ",
"SPCE": " > -----: ",
"D_FIGNUM": 0,
"D_FIGSIZE":[8.0, 6.0],
"D_REFORMAT": true,
"D_FORMATTED": false,
"D_INPUTARG": "current",
"D_IPATH": "../",
"D_OPATH": "myplotlib/img",
"D_OFORMAT": "png",
"D_DEBUG": true
}
{
"D_VERSION": "v5",
"CONTEXT": "local",
"AUTO_CONTEXTUALIZE": true,
"D_HOST": "localhost",
"D_PORT": 50803,
"INFO": " > info-: ",
......
{
"D_VERSION": "v5",
"CONTEXT": "client",
"AUTO_CONTEXTUALIZE": true,
"D_HOST": "localhost",
"D_PORT": 50803,
"INFO": " > info-: ",
......
{
"D_VERSION": "v5",
"CONTEXT": "server",
"AUTO_CONTEXTUALIZE": true,
"D_HOST": "localhost",
"D_PORT": 50803,
"INFO": " > info-: ",
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment