def __post_init__(self, packages, constructor_kwargs={}): #we need the package browser to build the dispatch dicitonary self.package_browser = ExposedPackageBrowser(self.context) #we need to know how a module is called in javascript (thus a command) self.js_util = JsTranslateUtil(self.context) #we can now build the dispatch dictionary self.__dispatch_dict = self.__buildDispatchDict(packages, constructor_kwargs)
class NodeJsRunner(SimpleRpcLogicBase): def __post_init__(self): self.js_util = JsTranslateUtil(self.context) self.file_manager = FileManager(self.context) def setNodePath(self): NODE_PATH = 'NODE_PATH' path = self.js_util._getProjectJsPath() if NODE_PATH not in os.environ: os.environ[NODE_PATH] = path else: os.environ[NODE_PATH] += ':' + path def __printJasmineErrors(self, file_path, ret_val, out, err): if ret_val: path = self.file_manager.formatFilePath(file_path) self.log.w('Error running jasmine test return value %s:\n%s' % (ret_val, path)) self.log.w('Stdout:\n%s' % out) self.log.w('Stderr:\%s' % err) return ret_val def runJasmineTest(self, file_path): argv = [self.context.jasmine_node_command, file_path, '--matchall', '--captureExceptions', '--noColor'] return self.__printJasmineErrors(file_path, *self.__runProcess(argv, pipe=True)) def __runProcess(self, argv, pipe): #we need to know where the .js are self.setNodePath() #run the process! if pipe: p = subprocess.Popen(argv, stdout=subprocess.PIPE, stderr=subprocess.PIPE) return p.wait(), p.stdout.read(), p.stderr.read() else: p = subprocess.Popen(argv) return p.wait() def runNodeJs(self, argv=None, pipe=False): #node_command = 'node' if not argv: argv = sys.argv[1:] argv = [self.context.node_command] + argv return self.__runProcess(argv, pipe)
class RPCJavascriptGenerator(SimpleRpcLogicBase): def __post_init__(self): self.file_manager = FileManager(self.context) self.package_translator = PackageToJs(self.context) self.templates_collector = TemplatesCollector(self.context) self.js_util = JsTranslateUtil(self.context) def getRpcNode(self, packages): packages_node = AutoTemplateAstNode() for p in packages: n = self.package_translator.translatePackage(p) name = p.__name__.split('.')[-1] packages_node.translate(name, n) exposed_rpc_node = TranslationAstNode('exposed_rpc.CommandQueueApi') exposed_rpc_node.translate(EXPOSED_PACKAGES=packages_node) return exposed_rpc_node def translateToFile(self, packages, js_rpc_file=None, templates=None, overwrite=False): js_rpc_file = self.js_util._getJsRpcFile(js_rpc_file) if not templates: templates = self.templates_collector.collectBuiltIn() text = self.getRpcNode(packages).getString(templates) self.file_manager.saveTextFile(js_rpc_file, text, overwrite)
def __post_init__(self): self.file_manager = FileManager(self.context) self.package_translator = PackageToJs(self.context) self.templates_collector = TemplatesCollector(self.context) self.js_util = JsTranslateUtil(self.context)
def __post_init__(self): self.js_util = JsTranslateUtil(self.context) self.file_manager = FileManager(self.context)
class RPCDispatcher(SimpleRpcLogicBase): ''' Creates the dispatching dictionary. Then answers incoming commands already converted to python. Each requested command must exist in the dispatch dictionary. ''' def __post_init__(self, packages, constructor_kwargs={}): #we need the package browser to build the dispatch dicitonary self.package_browser = ExposedPackageBrowser(self.context) #we need to know how a module is called in javascript (thus a command) self.js_util = JsTranslateUtil(self.context) #we can now build the dispatch dictionary self.__dispatch_dict = self.__buildDispatchDict(packages, constructor_kwargs) def __buildDispatchDict(self, packages, constructor_kwargs): exposed = self.context.exposed_roots handlers_dict = {} for root_package, sub_packages in packages.items(): if root_package not in exposed: msg = 'Root package %s is not exposed in the simplerpc_settings' % \ root_package.__name__ raise SimpleRpcError(msg) self.__buildPackageDict(root_package, sub_packages, constructor_kwargs, handlers_dict) return handlers_dict def __buildPackageDict(self, root_package, sub_packages, constructor_kwargs, handlers_dict): modname_prefix = root_package.__name__ for package_name in sub_packages: self.log.d('Importing module %s.%s as RPC handler.' % (modname_prefix, package_name)) package = import_module('%s.%s' % (modname_prefix, package_name)) self.__getPackageHandlers(package, self.__getDecorators(), handlers_dict, constructor_kwargs) return handlers_dict def __getDecorators(self): decorators = getDecoratorsList() dec_dict = dict() for dec in decorators: dec_dict[dec.__name__] = dec return dec_dict def __getPackageHandlers(self, package, decorators, handlers_dict, constructor_kwargs): classes = self.package_browser.getModuleAndClass(package) for module, class_ in classes: instance = class_(**constructor_kwargs) class_namespace = self.js_util._getJsNamespace(module) for dec_name, dec in decorators.items(): if dec_name not in handlers_dict: handlers_dict[dec_name] = {} for method_name in instance.exposedMethods(dec): cmd_str = '.'.join([class_namespace, method_name]) handlers_dict[dec_name][cmd_str] = getattr(instance, method_name) return handlers_dict def answer(self, decorator_class, cmd, args, kwargs): if isclass(decorator_class): #we got a decorator class, convert to string decorator_class = decorator_class.__name__ if decorator_class in self.__dispatch_dict \ and cmd in self.__dispatch_dict[decorator_class]: method = self.__dispatch_dict[decorator_class][cmd] else: raise RpcNotFoundError('Command %r of type %r not exposed or existent.' % (cmd, decorator_class)) return_value = method(*args, **kwargs) #TODO: security review? return return_value