home *** CD-ROM | disk | FTP | other *** search
/ Chip 2003 January / Chip_2003-01_cd2.bin / convert / eJayMp3Pro / mp3pro_demo.exe / PYCLBR.PYC (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2000-06-16  |  6.5 KB  |  191 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 1.5)
  3.  
  4. """Parse a Python file and retrieve classes and methods.
  5.  
  6. Parse enough of a Python file to recognize class and method
  7. definitions and to find out the superclasses of a class.
  8.  
  9. The interface consists of a single function:
  10. \treadmodule(module, path)
  11. module is the name of a Python module, path is an optional list of
  12. directories where the module is to be searched.  If present, path is
  13. prepended to the system search path sys.path.
  14. The return value is a dictionary.  The keys of the dictionary are
  15. the names of the classes defined in the module (including classes
  16. that are defined via the from XXX import YYY construct).  The values
  17. are class instances of the class Class defined here.
  18.  
  19. A class is described by the class Class in this module.  Instances
  20. of this class have the following instance variables:
  21. \tname -- the name of the class
  22. \tsuper -- a list of super classes (Class instances)
  23. \tmethods -- a dictionary of methods
  24. \tfile -- the file in which the class was defined
  25. \tlineno -- the line in the file on which the class statement occurred
  26. The dictionary of methods uses the method names as keys and the line
  27. numbers on which the method was defined as values.
  28. If the name of a super class is not recognized, the corresponding
  29. entry in the list of super classes is not a class instance but a
  30. string giving the name of the super class.  Since import statements
  31. are recognized and imported modules are scanned as well, this
  32. shouldn't happen often.
  33.  
  34. BUGS
  35. Continuation lines are not dealt with at all and strings may confuse
  36. the hell out of the parser, but it usually works."""
  37. import os
  38. import sys
  39. import imp
  40. import re
  41. import string
  42. id = '[A-Za-z_][A-Za-z0-9_]*'
  43. blank_line = re.compile('^[ \t]*($|#)')
  44. is_class = re.compile('^class[ \t]+(?P<id>' + id + ')[ \t]*(?P<sup>\\([^)]*\\))?[ \t]*:')
  45. is_method = re.compile('^[ \t]+def[ \t]+(?P<id>' + id + ')[ \t]*\\(')
  46. is_import = re.compile('^import[ \t]*(?P<imp>[^#;]+)')
  47. is_from = re.compile('^from[ \t]+(?P<module>' + id + '([ \t]*\\.[ \t]*' + id + ')*)[ \t]+import[ \t]+(?P<imp>[^#;]+)')
  48. dedent = re.compile('^[^ \t]')
  49. indent = re.compile('^[^ \t]*')
  50. _modules = { }
  51.  
  52. class Class:
  53.     '''Class to represent a Python class.'''
  54.     
  55.     def __init__(self, module, name, super, file, lineno):
  56.         self.module = module
  57.         self.name = name
  58.         if super is None:
  59.             super = []
  60.         
  61.         self.super = super
  62.         self.methods = { }
  63.         self.file = file
  64.         self.lineno = lineno
  65.  
  66.     
  67.     def _addmethod(self, name, lineno):
  68.         self.methods[name] = lineno
  69.  
  70.  
  71.  
  72. def readmodule(module, path = [], inpackage = 0):
  73.     '''Read a module file and return a dictionary of classes.
  74.  
  75. \tSearch for MODULE in PATH and sys.path, read and parse the
  76. \tmodule and return a dictionary with one entry for each class
  77. \tfound in the module.'''
  78.     i = string.rfind(module, '.')
  79.     if i >= 0:
  80.         package = string.strip(module[:i])
  81.         submodule = string.strip(module[i + 1:])
  82.         parent = readmodule(package, path, inpackage)
  83.         child = readmodule(submodule, parent['__path__'], 1)
  84.         return child
  85.     
  86.     if _modules.has_key(module):
  87.         return _modules[module]
  88.     
  89.     if module in sys.builtin_module_names:
  90.         dict = { }
  91.         _modules[module] = dict
  92.         return dict
  93.     
  94.     f = None
  95.     if inpackage:
  96.         
  97.         try:
  98.             (suff, mode, type) = (f, file)
  99.         except ImportError:
  100.             f = None
  101.  
  102.     
  103.     if f is None:
  104.         fullpath = list(path) + sys.path
  105.         (suff, mode, type) = (f, file)
  106.     
  107.     if type == imp.PKG_DIRECTORY:
  108.         dict = {
  109.             '__path__': [
  110.                 file] }
  111.         _modules[module] = dict
  112.         return dict
  113.     
  114.     if type != imp.PY_SOURCE:
  115.         f.close()
  116.         dict = { }
  117.         _modules[module] = dict
  118.         return dict
  119.     
  120.     cur_class = None
  121.     dict = { }
  122.     _modules[module] = dict
  123.     imports = []
  124.     lineno = 0
  125.     while 1:
  126.         line = f.readline()
  127.         if not line:
  128.             break
  129.         
  130.         lineno = lineno + 1
  131.         line = line[:-1]
  132.         if blank_line.match(line):
  133.             continue
  134.         
  135.         res = is_import.match(line)
  136.         res = is_from.match(line)
  137.         if res:
  138.             mod = res.group('module')
  139.             names = string.splitfields(res.group('imp'), ',')
  140.             
  141.             try:
  142.                 d = readmodule(mod, path, inpackage)
  143.             except:
  144.                 None if res else string.splitfields(res.group('imp'), ',')
  145.                 print 'module', mod, 'not found'
  146.                 continue
  147.  
  148.             for n in names:
  149.                 n = string.strip(n)
  150.                 if d.has_key(n):
  151.                     dict[n] = d[n]
  152.                 elif n == '*':
  153.                     for n in d.keys():
  154.                         pass
  155.                     
  156.                 
  157.             
  158.             continue
  159.         
  160.         res = is_class.match(line)
  161.         if res:
  162.             class_name = res.group('id')
  163.             inherit = res.group('sup')
  164.             if inherit:
  165.                 inherit = string.strip(inherit[1:-1])
  166.                 names = []
  167.                 for n in string.splitfields(inherit, ','):
  168.                     n = string.strip(n)
  169.                     names.append(n)
  170.                 
  171.                 inherit = names
  172.             
  173.             cur_class = Class(module, class_name, inherit, file, lineno)
  174.             dict[class_name] = cur_class
  175.             continue
  176.         
  177.         res = is_method.match(line)
  178.         if res:
  179.             if cur_class:
  180.                 meth_name = res.group('id')
  181.                 cur_class._addmethod(meth_name, lineno)
  182.             
  183.             continue
  184.         
  185.         if dedent.match(line):
  186.             cur_class = None
  187.         
  188.     f.close()
  189.     return dict
  190.  
  191.