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 _rewrite(self, javascript_code): first_token = Frewriter.tokenizer(javascript_code) token = first_token while token: if token.type == TokenType.IDENTIFIER and \ token.string == 'RES': start_paren = token.next quote = start_paren.next # print 'type: [%s] string: [%s]' % (quote.type, quote.string) if quote.type != TokenType.SINGLE_QUOTE_STRING_START and \ quote.type != TokenType.DOUBLE_QUOTE_STRING_START: logging.error('Invalid call RES() function @%s.', token.line_number) token = quote continue resource = quote.next if resource.type != TokenType.STRING_TEXT: logging.error('RES()\' parameter must be const STRING.') token = resource continue print 'type: [%s] string: [%s]' % (resource.type, resource.string) new_resource = self._found_resource(None, resource.string) if new_resource: resource.string = new_resource token = resource.next token = token.next return Frewriter.dump_source(first_token)
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 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)