/Tools/compiler/demo.py

http://unladen-swallow.googlecode.com/ · Python · 38 lines · 19 code · 8 blank · 11 comment · 3 complexity · d948791d5a83bd9ce530ed87b7358720 MD5 · raw file

  1. #! /usr/bin/env python
  2. """Print names of all methods defined in module
  3. This script demonstrates use of the visitor interface of the compiler
  4. package.
  5. """
  6. import compiler
  7. class MethodFinder:
  8. """Print the names of all the methods
  9. Each visit method takes two arguments, the node and its current
  10. scope. The scope is the name of the current class or None.
  11. """
  12. def visitClass(self, node, scope=None):
  13. self.visit(node.code, node.name)
  14. def visitFunction(self, node, scope=None):
  15. if scope is not None:
  16. print "%s.%s" % (scope, node.name)
  17. self.visit(node.code, None)
  18. def main(files):
  19. mf = MethodFinder()
  20. for file in files:
  21. f = open(file)
  22. buf = f.read()
  23. f.close()
  24. ast = compiler.parse(buf)
  25. compiler.walk(ast, mf)
  26. if __name__ == "__main__":
  27. import sys
  28. main(sys.argv[1:])