def patch(buf_id, patch, callback): """ Performs patching of given source in separate thread and dispatches result into `callback` function """ if not import_pyv8(): logger.error('PyV8 is not available') return if buf_id not in _patch_state: _patch_state[buf_id] = {'running': False, 'patches': None} state = _patch_state[buf_id] if patch: if not eutils.isstr(patch): patch = json.dumps(patch) with PyV8.JSLocker(): with PyV8.JSContext(extensions=['livestyle']) as c: state['patches'] = c.locals.livestyle.condensePatches( state['patches'], patch) if not state['running'] and state['patches']: _patches = state['patches'] state['patches'] = None _start_patch(buf_id, _patches, callback)
def patch(buf_id, patch, callback): """ Performs patching of given source in separate thread and dispatches result into `callback` function """ if not import_pyv8(): logger.error('PyV8 is not available') return if buf_id not in _patch_state: _patch_state[buf_id] = { 'running': False, 'patches': None } state = _patch_state[buf_id] if patch: if not eutils.isstr(patch): patch = json.dumps(patch) with PyV8.JSLocker(): with PyV8.JSContext(extensions=['livestyle']) as c: state['patches'] = c.locals.livestyle.condensePatches(state['patches'], patch) if not state['running'] and state['patches']: _patches = state['patches'] state['patches'] = None _start_patch(buf_id, _patches, callback)
def is_valid_patch(content): "Check if given content is a valid patch" if eutils.isstr(content): try: content = json.loads(content) except: return False try: return content and content.get('id') == 'livestyle' except: return False
def send_message(message, client=None, exclude=None): "Sends given message to websocket clients" if not eutils.isstr(message): message = json.dumps(message) clients = WSHandler.clients if not client else [client] if exclude: clients = [c for c in clients if c != exclude] if not clients: logger.debug('Cannot send message, client list empty') else: for c in clients: c.write_message(message)
def parse_patch(data): "Parses given patch and returns object with meta-data about patch" if not is_valid_patch(data): return None if eutils.isstr(data): data = json.loads(data) out = [] for k, v in data.get('files', {}).items(): out.append({ 'file': k, 'selectors': _stringify_selectors(v), 'data': v }) return out
def parse_json(data): return json.loads(data) if eutils.isstr(data) else data