def _remove_video(path, filename): params = { 'properties': ['file'], 'filter': { 'and': [{ 'operator': 'is', 'field': 'path', 'value': path }, { 'operator': 'is', 'field': 'filename', 'value': filename }] } } movies = rpc('VideoLibrary.GetMovies', **params).get('result', {}).get('movies', []) if movies: return _rpc_remove_video('movie', movies[0]['movieid']) episodes = rpc('VideoLibrary.GetEpisodes', **params).get('result', {}).get('episodes', []) if episodes: return _rpc_remove_video('episode', episodes[0]['episodeid']) return False
def run(self): logger.debug(msg("running.")) self.stop_event_loop() while True: self._process_children() if self.srv_command_pipe.poll(): req = self.srv_command_pipe.recv() logger.debug(msg("incoming data on command pipe ", req)) if req.message_name == 'create_object': try: obj_id = self._create_object(req.object_class, *req.args, **req.kwargs) response = rpc('created_object') response.object_id = obj_id except Exception, e: logger.warn(msg("Object could not be created, exception thrown: ", e)) response = rpc('created_object') response.object_id = None response.exception = e self.srv_command_pipe.send(response) elif req.message_name == 'terminate_object': self._server_terminate_object(req.object_id) elif req.message_name == 'terminate': for obj in self.children: self._server_terminate_object(obj) response = rpc('terminated') self.srv_command_pipe.send(response)
def main(): ## load mappings ## try: setup_keymap_folder() except Exception: traceback.print_exc() utils.rpc('GUI.ShowNotification', title="Keymap Editor", message="Failed to remove old keymap file", image='error') return defaultkeymap = utils.read_keymap(default) userkeymap = [] if os.path.exists(gen_file): try: userkeymap = utils.read_keymap(gen_file) except Exception: traceback.print_exc() utils.rpc('GUI.ShowNotification', title="Keymap Editor", message="Failed to load keymap file", image='error') return ## main loop ## confirm_discard = False while True: idx = Dialog().select(tr(30000), [tr(30003), tr(30004), tr(30005)]) if idx == 0: # edit editor = Editor(defaultkeymap, userkeymap) editor.start() confirm_discard = editor.dirty elif idx == 1: # reset confirm_discard = bool(userkeymap) userkeymap = [] elif idx == 2: # save if os.path.exists(gen_file): shutil.copyfile(gen_file, gen_file + ".old") utils.write_keymap(userkeymap, gen_file) xbmc.executebuiltin("action(reloadkeymaps)") break elif idx == -1 and confirm_discard: if Dialog().yesno(tr(30000), tr(30006)) == 1: break else: break sys.modules.clear()
def _close(self, pipe_id): if pipe_id in self.queues: logger.debug(msg("Closing pipe", pipe_id)) req = rpc('close') req.pipe_id = pipe_id self.pipe.send(req) del self.queues[pipe_id]
def _send(self, pipe_id, data): logger.debug(msg("Router._send: sending data on pipe ", pipe_id, "(data ==", data,")")) if pipe_id not in self.queues: raise Exception("I/O Error: Non-existing or closed pipe") req = rpc('transport') req.pipe_id = pipe_id req.data = data self.pipe.send(req)
def _terminate_object(self, object_id): if object_id in self.children: req = rpc('terminate_object') req.object_id = object_id self.cli_command_pipe.send(req) self.children[object_id]['pipe'].close() self.children[object_id]['object'] = None del self.children[object_id]
def _rpc_remove_video(media_type, media_id): method = "VideoLibrary.Remove%s" % media_type params = {'%sid' % media_type: media_id} response = rpc(method, **params) if response.get('result') != 'OK': log("[rpc] failed to remove %s with id %s. response: %s" % (media_type, media_id, json.dumps(response, indent=4)), xbmc.LOGERROR) return False return True
def _get_run_addon_actions(): addons = [] addon_types = ['xbmc.python.pluginsource', 'xbmc.python.script'] for addon_type in addon_types: response = rpc('Addons.GetAddons', type=addon_type, properties=['name', 'enabled']) res = response['result'] if 'addons' in res: addons.extend([a for a in res['addons'] if a['enabled']]) actions = ['runaddon(%s)' % a['addonid'] for a in addons] names = ['Launch %s' % a['name'] for a in addons] return action_dict(actions, names)
def _remove_video(path, filename): params = { 'properties': ['file'], 'filter': { 'and': [ {'operator': 'is', 'field': 'path', 'value': path}, {'operator': 'is', 'field': 'filename', 'value': filename} ] } } movies = rpc('VideoLibrary.GetMovies', **params).get('result', {}).get('movies', []) if movies: return _rpc_remove_video('movie', movies[0]['movieid']) episodes = rpc('VideoLibrary.GetEpisodes', **params).get('result', {}).get('episodes', []) if episodes: return _rpc_remove_video('episode', episodes[0]['episodeid']) return False
def destroy(self): logger.debug("Destroying factory ...") req = rpc('terminate') logger.debug("Sending destroy command to remote factory ...") self.cli_command_pipe.send(req) logger.debug("Waiting for remote factory to destroy itself ...") if self.cli_command_pipe.poll(3): req = self.cli_command_pipe.recv() logger.debug("Terminating remote factory ...") self.terminate() logger.debug("Remote factory terminated.")
def _rpc_remove_video(media_type, media_id): method = "VideoLibrary.Remove%s" % media_type params = {'%sid' % media_type: media_id} response = rpc(method, **params) if response.get('result') != 'OK': log( "[rpc] failed to remove %s with id %s. response: %s" % (media_type, media_id, json.dumps(response, indent=4)), xbmc.LOGERROR) return False return True
def _process( self, min_priority_level = None ): if len(self.command_queue) == 0: return else: command, p = self.command_queue.pop(0) if min_priority_level is None or self._compute_priority(command) >= min_priority_level: logger.debug(msg("Processing command ", command.message_name, " priority = ", command.priority)) if command.rpc_type == rpc.OBJECT_MESSAGE: method = self._get_method(command.message_name) call_id = command.message_id try: ret = method(*command.args, **command.kwargs) response = rpc( command.message_name, ret, response_to = call_id, rpc_type = rpc.OBJECT_MESSAGE_RESPONSE ) self.pipe.send(response) except Exception, e: logger.warn(msg("Exception while running command ", command.message_name, " exception == ", e)) response = rpc( 'error', response_to = call_id, rpc_type = rpc.ERROR_MESSAGE) response.error_typ = 'exception' try: response.error_description = str(e) except: pass response.exception = e self.pipe.send(response)
def create_object(self, object_class, *args, **kwargs): req = rpc('create_object', *args, **kwargs) req.object_class = object_class self.cli_command_pipe.send(req) response = self.cli_command_pipe.recv() assert response.message_name == 'created_object', "new_InsulatedFactory: Unknown response " + response.message_name if response.object_id is None: raise response.exception else: object_pipe = self.client_router.create(response.object_id) proxy_object = local_proxy( object_pipe, object_class, self, response.object_id, _event_loop = self.client_event_loop) self.children[response.object_id] = { 'object':proxy_object, 'pipe':object_pipe } return proxy_object
def _dispatch_method(self, method_name, *args, **kwargs): if '_priority' in kwargs: p = kwargs['_priority'] del kwargs['_priority'] else: p = rpc.PRIORITY_NORMAL if '_async' in kwargs: async = kwargs['_async'] del kwargs['_async'] else: async = True if '_timeout' in kwargs: timeout = kwargs['_timeout'] del kwargs['_timeout'] async = False if '_default' in kwargs: default_ret = kwargs['_default'] del kwargs['_default'] else: default_ret = None else: timeout = None logger.debug(msg("Timeout == ", timeout)) command = rpc(method_name, *args, **kwargs) command.priority = p logger.debug(msg("Dispatching rpc for ", method_name)) self.pipe.send(command) time_start = time() if timeout is not None: logger.debug(msg("Time start == ", time_start)) while not async: ret = self._process_command(block=False, return_response_to=command.message_id) if ret is not None: return ret if timeout is not None and time() - time_start > timeout: return default_ret if time() % 10 == 0: logger.debug(msg(self, "Time:", time()))
def _sendError(self, *args, **kwargs): command = rpc( 'error', *args, **kwargs) command.rpc_type = rpc.ERROR_MESSAGE self.pipe.send(command)
def _signal_handler(self, signal_name, *args, **kwargs): command = rpc( signal_name, *args, **kwargs ) self.pipe.send(command)