def inject(path): lol = get_lol(path) #lol = ast.LOL() #entity1 = ast.Entity(id=ast.Identifier('foo'), value=ast.String('flex')) #entity2 = ast.Entity(id=ast.Identifier('foo2')) #exp1 = ast.BinaryExpression( # ast.BinaryOperator('+'), # ast.ParenthesisExpression(ast.Literal(1)), # ast.UnaryExpression( # ast.UnaryOperator('!'), # ast.Literal(2) # ) #) #macro1 = ast.Macro(id=ast.Identifier('foo3'), args=[ast.Identifier('a'), # ast.Identifier('b')], # expression=exp1) #com1 = ast.Comment("foo") #lol.body.append(entity1) #lol.body.append(com1) #lol.body.append(entity2) #lol.body.append(macro1) serializer = Serializer() string = serializer.serialize(lol) print(string)
'class', 'style', 'href', ) isloc = lambda attr: attr[0] not in ATTRS def extract(node): id = ast.Identifier(node['data-l10n-id']) attrs2 = filter(isloc, node.attrs) attrs = {} for i in attrs2: attrs[i[0]] = ast.Attribute(ast.Identifier(i[0]), ast.String(i[1])) for child in node.findAll(): child.attrs = filter(isloc, child.attrs) value = ast.String(node.renderContents()) return id, value, attrs if __name__ == '__main__': f = open(sys.argv[1], 'r') dom = BeautifulSoup(f) nodes = dom.findAll(True, {'data-l10n-id': True}) lol = ast.LOL() for node in nodes: id, value, attrs = extract(node) entity = ast.Entity(id, None, value, attrs) lol.body.append(entity) print(Serializer.serialize(lol))