Exemplo n.º 1
0
class ClassToJsUnitTest(SimpleRpcLogicBase):
    '''
    #TODO: document
    '''
    def __post_init__(self):
        self.class_to_js = ClassToJs(self.context)
        self.templates = TemplatesCollector(self.context)
        self.file_manager = FileManager(self.context)

    def translateClass(self, class_):
        '''  '''
        ast_tree = self.class_to_js.translateClass(class_)
        templates_set = 'js_unittest_templates'
        templates = self.templates.collectBuiltIn(templates_set)
        js_str = ast_tree.getString(templates)
        return js_str

    def translateToFile(self, tested_class, file_path, overwrite=False):
        js_str = self.__getClassJsUnit(tested_class)
        #create directory just in case
        test_dir = os.path.dirname(file_path)
        self.file_manager.makedirs(test_dir)
        #Say where it is in a pretty way
        path = self.file_manager.formatFilePath(file_path)
        name = tested_class.__name__
        self.log.d('Creating js for %s test at:\n %s' % (name, path))
        #save file
        self.file_manager.saveTextFile(file_path, js_str, overwrite)

    def __getClassJsUnit(self, tested_class):
        js_str = self.translateClass(tested_class)
        translate = dict(EXPOSED_RPC_API_CLASS=self.context.js_rpc_file)
        js_str = Template(js_str).safe_substitute(translate)
        return js_str
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)