def _setRemote(self, api): _remote = dotdict() for i in range(len(api)): if type(api[i]) is dict and "name" in api[i]: name = api[i]["name"] data = api[i].get("data", None) if data is not None: if type(data) is dict: data2 = dotdict() for key in data: if key in data: if data[key] == "**@@FUNCTION@@**:" + key: data2[key] = self._genRemoteMethod(name + '.' + key) else: data2[key] = data[key] _remote[name] = data2 else: _remote[name] = data else: _remote[name] = self._genRemoteMethod(name) self._setLocalAPI(_remote) return _remote
def __init__(self, pid, secret, protocol='http', host='127.0.0.1', port=9527, queue=None, loop=None, worker=None, namespace='/', work_dir=None, daemon=False, api=None): if work_dir is None or work_dir == '' or work_dir == '.': self.work_dir = os.getcwd() else: self.work_dir = work_dir if not os.path.exists(self.work_dir): os.makedirs(self.work_dir) os.chdir(self.work_dir) socketIO = SocketIO(host, port, LoggingNamespace) self.socketIO = socketIO self._init = False self.secret = secret self.id = pid self.daemon = daemon def emit(msg): socketIO.emit('from_plugin_' + secret, msg) self.emit = emit self._local = {} _remote = dotdict() self._setLocalAPI(_remote) self._interface = {} self._plugin_interfaces = {} self._remote_set = False self._store = ReferenceStore() self._executed = False self.queue = queue self.loop = loop self._init = False sys.stdout.flush() socketIO.on('to_plugin_' + secret, self.sio_plugin_message) self.emit({"type": "initialized", "dedicatedThread": True}) print('Plugin "{}" Initialized.'.format(pid)) def on_disconnect(): if not self.daemon: self.exit(1) socketIO.on('disconnect', on_disconnect) self.abort = threading.Event() self.worker = worker
def _decode(self, aObject, callbackId, withPromise): if aObject is None: return aObject if '__jailed_type__' in aObject and '__value__' in aObject: if aObject['__jailed_type__'] == 'callback': bObject = self._genRemoteCallback(callbackId, aObject['num'], withPromise) elif aObject['__jailed_type__'] == 'interface': name = aObject['__value__'] if name in self._remote: bObject = self._remote[name] else: bObject = self._genRemoteMethod(name) elif aObject['__jailed_type__'] == 'plugin_interface': bObject = self._genRemoteMethod(aObject['__value__'], aObject['__plugin_id__']) elif aObject['__jailed_type__'] == 'ndarray': # create build array/tensor if used in the plugin try: np = self._local['np'] if isinstance(aObject['__value__'], bytearray): aObject['__value__'] = aObject['__value__'] elif isinstance(aObject['__value__'], list) or isinstance(aObject['__value__'], tuple): aObject['__value__'] = reduce((lambda x, y: x + y), aObject['__value__']) else: raise Exception('Unsupported data type: ', type(aObject['__value__']), aObject['__value__']) bObject = np.frombuffer(aObject['__value__'], dtype=aObject['__dtype__']).reshape(tuple(aObject['__shape__'])) except Exception as e: logger.debug('Error in converting: %s', e) bObject = aObject raise e elif aObject['__jailed_type__'] == 'error': bObject = Exception(aObject['__value__']) elif aObject['__jailed_type__'] == 'argument': bObject = aObject['__value__'] else: bObject = aObject['__value__'] return bObject else: if type(aObject) is tuple: aObject = list(aObject) isarray = type(aObject) is list bObject = [] if isarray else dotdict() keys = range(len(aObject)) if isarray else aObject.keys() for k in keys: if isarray or k in aObject: v = aObject[k] if isinstance(v, dict)or type(v) is list: if isarray: bObject.append(self._decode(v, callbackId, withPromise)) else: bObject[k] = self._decode(v, callbackId, withPromise) return bObject
def ndarray(typedArray, shape, dtype): _dtype = type(typedArray) if dtype and dtype != _dtype: raise Exception("dtype doesn't match the type of the array: " + _dtype + ' != ' + dtype) shape = shape or (len(typedArray), ) return { "__jailed_type__": 'ndarray', "__value__": typedArray, "__shape__": shape, "__dtype__": _dtype } api_utils = dotdict(ndarray=ndarray, kill=kill, debounce=debounce, setInterval=setInterval) class PluginConnection(): def __init__(self, pid, secret, protocol='http', host='127.0.0.1', port=9527, queue=None, loop=None, worker=None, namespace='/', work_dir=None,