def find_ad_config(code): first_token = Frewriter.tokenizer(code) token = Frewriter.find_token(first_token, 'AD_CONFIG') token = Frewriter.find_token(token, 'AD_CONFIG') start_block = token.next.next.next.next # whitespace operator whitespace START_BLOCK # start_block = Frewriter.find_token(token, "{", TokenType.START_BLOCK) before_start_block = start_block.previous if start_block is None or start_block.type != TokenType.START_BLOCK: return (None, None) end_block = Frewriter.find_end_token(start_block.next) config = Frewriter.dump_source(start_block, end_block) remove_tokens_between(start_block, end_block) # 插入var AD_CONFIG = %AD_CONFIG%; # 保证后续如果想把处理之后的AD_CONFIG重新放回到code的时候,有一个 # 可以确定的位置。 tokenutil.InsertTokenAfter(Token(';', TokenType.SEMICOLON, token.line, token.line_number), before_start_block) tokenutil.InsertTokenAfter(Token('%AD_CONFIG%', TokenType.IDENTIFIER, token.line, token.line_number), before_start_block) code = Frewriter.dump_source(first_token) config = config.strip().rstrip(';') return (code, config)
def find_widget_config(code): first_token = Frewriter.tokenizer(code) token = Frewriter.find_token(first_token, 'WIDGET_CONFIG') start_block = Frewriter.find_token(token, "{", TokenType.START_BLOCK) if start_block is None: return None end_block = Frewriter.find_end_token(start_block.next) config = Frewriter.dump_source(start_block, end_block) return config.strip().rstrip(';')
def testFindEndToken(self): token = Frewriter.find_token(self.first_token, "a.this_is_a_module.config") self.assertEquals(token.type, TokenType.SIMPLE_LVALUE) self.assertEquals(token.values['identifier'], 'a.this_is_a_module.config') start_block = Frewriter.find_token(token, "{", TokenType.START_BLOCK) self.assertTrue(start_block is not None) end_block = Frewriter.find_end_token(start_block.next) self.assertTrue(end_block is not None)
def register_action(module_path, action_path, app_cfg): # 修改module.js,添加path和action关系 abs_path = os.path.join(module_path, 'module.js') module_config_identifier = "%s.config" % app_cfg["app.module"] first_token = Frewriter.tokenizer(open(abs_path).read()) token = Frewriter.find_token(first_token, module_config_identifier) start_block = Frewriter.find_token(token, "{", TokenType.START_BLOCK) end_block = Frewriter.find_end_token(start_block.next) map_code = "\n// Autogenerated at %s\n%s['action'].push({'location':'%s','action':'%s'});" % ( datetime.now().strftime('%Y/%m/%d %H:%M:%S'), module_config_identifier, action_path, app_cfg['app.name']) Frewriter.append_code(end_block.previous, Frewriter.tokenizer(map_code)) open(abs_path, 'wb').write(Frewriter.dump_source(first_token)) logging.info('M %s' % abs_path)
def testInsertActionConfig(self): token = Frewriter.find_token(self.first_token, "a.this_is_a_module.config") self.assertEquals(token.type, TokenType.SIMPLE_LVALUE) self.assertEquals(token.values['identifier'], 'a.this_is_a_module.config') start_block = Frewriter.find_token(token, "{", TokenType.START_BLOCK) self.assertTrue(start_block is not None) end_block = Frewriter.find_end_token(start_block.next) self.assertTrue(end_block is not None) # a.this_is_a_module.config['action'].push({'location':'','action':''}); code = "a.this_is_a_module.config['action'].push({'location':'/jn/demo/xxx','action':'jn.demo.Xxx'});" Frewriter.append_code(end_block, Frewriter.tokenizer(code)) self.assertTrue(self._dump_source(self.first_token).find(code) > 0)
def testFindToken(self): token = Frewriter.find_token(self.first_token, "a.this_is_a_module.config") self.assertEquals(token.type, TokenType.SIMPLE_LVALUE) self.assertEquals(token.values['identifier'], 'a.this_is_a_module.config') token = Frewriter.find_token(self.first_token, "a.this_is_a_module.Fields") self.assertEquals(token.type, TokenType.SIMPLE_LVALUE) self.assertEquals(token.values['identifier'], 'a.this_is_a_module.Fields') token = Frewriter.find_token(self.first_token, "a.this_is_a_module.data") self.assertEquals(token.type, TokenType.SIMPLE_LVALUE) self.assertEquals(token.values['identifier'], 'a.this_is_a_module.data') self.assertEquals(token.string, 'a.this_is_a_module.data') token = Frewriter.find_token(self.first_token, "/*", TokenType.START_BLOCK_COMMENT) self.assertTrue(token is not None) self.assertEquals(token.type, TokenType.START_BLOCK_COMMENT) self.assertEquals(token.string, "/*") token = Frewriter.find_token(self.first_token, "a.this_is_a_module.NotFound") self.assertTrue(token is None)