# HG changeset patch # User paulb # Date 1106563001 0 # Node ID e7ecb820d8ef4912c113c5ccd81384f3df8457f8 # Parent 5a430aba00c780a9deaaacc2bcb88fe3f4404e28 [project @ 2005-01-24 10:36:41 by paulb] Added prefix definition using special import statements. diff -r 5a430aba00c7 -r e7ecb820d8ef libxml2macro.py --- a/libxml2macro.py Mon Jan 24 02:16:16 2005 +0000 +++ b/libxml2macro.py Mon Jan 24 10:36:41 2005 +0000 @@ -33,7 +33,7 @@ # Processing functions. -def process_nodes(root_node, prefix): +def process_nodes(root_node, prefix=None): """ Under the 'root_node', process all suitable expression nodes which employ @@ -41,14 +41,40 @@ """ for node in root_node.getChildNodes(): - # Identify suitable names and add such nodes - if isinstance(node, compiler.ast.CallFunc): + + # Find imports and discover if they are really used to define the + # special prefix. + + if isinstance(node, compiler.ast.Import): + prefix = process_import(node, root_node) or prefix + + # Identify suitable names and add replacement nodes. + + elif isinstance(node, compiler.ast.CallFunc): process_callfunc(node, prefix) elif isinstance(node, compiler.ast.Getattr): process_getattr(node, prefix, root_node) else: process_nodes(node, prefix) +def process_import(node, parent): + + """ + Process the Import 'node' searching for the special incantation required to + set the prefix. Remove compliant nodes from their 'parent' node. If no new + prefix is found, return None. + """ + + for name, alias in node.names: + if name == "libxml2macro": + + # Remove this node from its parent. + + parent.nodes.remove(node) + return alias + + return None + def process_callfunc(node, prefix): """ @@ -56,7 +82,13 @@ 'prefix'. """ + # Check the prefix. + + if prefix is None: + return + # Check the target. + target = node.node if isinstance(target, compiler.ast.Getattr): if process_getattr(target, prefix, node): @@ -95,6 +127,11 @@ processed and where a replacement of such arguments is occurring. """ + # Check the prefix. + + if prefix is None: + return + # Detected cases: # node.attr plus node.attr.attr # (obj.node).attr plus (obj.node).attr.attr @@ -167,16 +204,27 @@ # Check the expression as a simple name: # node.attr + if isinstance(node.expr, compiler.ast.Name) and node.expr.name.startswith(prefix): return 1 + # Check the attribute name of child expressions: # (obj.node).attr + elif isinstance(node.expr, compiler.ast.Getattr) and node.expr.attrname.startswith(prefix): return 1 else: return 0 -def process_file(filename, prefix="x2_"): +def include_import(module): + + """ + Include an import statement in 'module' so make the macro library available. + """ + + module.node.nodes.insert(0, compiler.ast.From("libxml2dom.macrolib", [("*", None)])) + +def process_file(filename): """ Process the module given by the specified 'filename'. The optional special @@ -189,7 +237,11 @@ # Find references to special variables. - process_nodes(module, prefix) + process_nodes(module) + + # Add necessary imports. + + include_import(module) # Write the module.