home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_329 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  2.6 KB  |  96 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from compiler import ast
  5.  
  6. class ASTVisitor:
  7.     VERBOSE = 0
  8.     
  9.     def __init__(self):
  10.         self.node = None
  11.         self._cache = { }
  12.  
  13.     
  14.     def default(self, node, *args):
  15.         for child in node.getChildNodes():
  16.             self.dispatch(child, *args)
  17.         
  18.  
  19.     
  20.     def dispatch(self, node, *args):
  21.         self.node = node
  22.         klass = node.__class__
  23.         meth = self._cache.get(klass, None)
  24.         if meth is None:
  25.             className = klass.__name__
  26.             meth = getattr(self.visitor, 'visit' + className, self.default)
  27.             self._cache[klass] = meth
  28.         
  29.         return meth(node, *args)
  30.  
  31.     
  32.     def preorder(self, tree, visitor, *args):
  33.         self.visitor = visitor
  34.         visitor.visit = self.dispatch
  35.         self.dispatch(tree, *args)
  36.  
  37.  
  38.  
  39. class ExampleASTVisitor(ASTVisitor):
  40.     examples = { }
  41.     
  42.     def dispatch(self, node, *args):
  43.         self.node = node
  44.         meth = self._cache.get(node.__class__, None)
  45.         className = node.__class__.__name__
  46.         if meth is None:
  47.             meth = getattr(self.visitor, 'visit' + className, 0)
  48.             self._cache[node.__class__] = meth
  49.         
  50.         if self.VERBOSE > 1:
  51.             print 'dispatch', className,
  52.             if not meth or meth.__name__:
  53.                 pass
  54.             print ''
  55.         
  56.         if meth:
  57.             meth(node, *args)
  58.         elif self.VERBOSE > 0:
  59.             klass = node.__class__
  60.             if klass not in self.examples:
  61.                 self.examples[klass] = klass
  62.                 print 
  63.                 print self.visitor
  64.                 print klass
  65.                 for attr in dir(node):
  66.                     if attr[0] != '_':
  67.                         print '\t', '%-12.12s' % attr, getattr(node, attr)
  68.                         continue
  69.                 
  70.                 print 
  71.             
  72.             return self.default(node, *args)
  73.  
  74.  
  75. _walker = ASTVisitor
  76.  
  77. def walk(tree, visitor, walker = None, verbose = None):
  78.     if walker is None:
  79.         walker = _walker()
  80.     
  81.     if verbose is not None:
  82.         walker.VERBOSE = verbose
  83.     
  84.     walker.preorder(tree, visitor)
  85.     return walker.visitor
  86.  
  87.  
  88. def dumpNode(node):
  89.     print node.__class__
  90.     for attr in dir(node):
  91.         if attr[0] != '_':
  92.             print '\t', '%-10.10s' % attr, getattr(node, attr)
  93.             continue
  94.     
  95.  
  96.