home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 1.5)
-
- """Parse a Python file and retrieve classes and methods.
-
- Parse enough of a Python file to recognize class and method
- definitions and to find out the superclasses of a class.
-
- The interface consists of a single function:
- \treadmodule(module, path)
- module is the name of a Python module, path is an optional list of
- directories where the module is to be searched. If present, path is
- prepended to the system search path sys.path.
- The return value is a dictionary. The keys of the dictionary are
- the names of the classes defined in the module (including classes
- that are defined via the from XXX import YYY construct). The values
- are class instances of the class Class defined here.
-
- A class is described by the class Class in this module. Instances
- of this class have the following instance variables:
- \tname -- the name of the class
- \tsuper -- a list of super classes (Class instances)
- \tmethods -- a dictionary of methods
- \tfile -- the file in which the class was defined
- \tlineno -- the line in the file on which the class statement occurred
- The dictionary of methods uses the method names as keys and the line
- numbers on which the method was defined as values.
- If the name of a super class is not recognized, the corresponding
- entry in the list of super classes is not a class instance but a
- string giving the name of the super class. Since import statements
- are recognized and imported modules are scanned as well, this
- shouldn't happen often.
-
- BUGS
- Continuation lines are not dealt with at all and strings may confuse
- the hell out of the parser, but it usually works."""
- import os
- import sys
- import imp
- import re
- import string
- id = '[A-Za-z_][A-Za-z0-9_]*'
- blank_line = re.compile('^[ \t]*($|#)')
- is_class = re.compile('^class[ \t]+(?P<id>' + id + ')[ \t]*(?P<sup>\\([^)]*\\))?[ \t]*:')
- is_method = re.compile('^[ \t]+def[ \t]+(?P<id>' + id + ')[ \t]*\\(')
- is_import = re.compile('^import[ \t]*(?P<imp>[^#;]+)')
- is_from = re.compile('^from[ \t]+(?P<module>' + id + '([ \t]*\\.[ \t]*' + id + ')*)[ \t]+import[ \t]+(?P<imp>[^#;]+)')
- dedent = re.compile('^[^ \t]')
- indent = re.compile('^[^ \t]*')
- _modules = { }
-
- class Class:
- '''Class to represent a Python class.'''
-
- def __init__(self, module, name, super, file, lineno):
- self.module = module
- self.name = name
- if super is None:
- super = []
-
- self.super = super
- self.methods = { }
- self.file = file
- self.lineno = lineno
-
-
- def _addmethod(self, name, lineno):
- self.methods[name] = lineno
-
-
-
- def readmodule(module, path = [], inpackage = 0):
- '''Read a module file and return a dictionary of classes.
-
- \tSearch for MODULE in PATH and sys.path, read and parse the
- \tmodule and return a dictionary with one entry for each class
- \tfound in the module.'''
- i = string.rfind(module, '.')
- if i >= 0:
- package = string.strip(module[:i])
- submodule = string.strip(module[i + 1:])
- parent = readmodule(package, path, inpackage)
- child = readmodule(submodule, parent['__path__'], 1)
- return child
-
- if _modules.has_key(module):
- return _modules[module]
-
- if module in sys.builtin_module_names:
- dict = { }
- _modules[module] = dict
- return dict
-
- f = None
- if inpackage:
-
- try:
- (suff, mode, type) = (f, file)
- except ImportError:
- f = None
-
-
- if f is None:
- fullpath = list(path) + sys.path
- (suff, mode, type) = (f, file)
-
- if type == imp.PKG_DIRECTORY:
- dict = {
- '__path__': [
- file] }
- _modules[module] = dict
- return dict
-
- if type != imp.PY_SOURCE:
- f.close()
- dict = { }
- _modules[module] = dict
- return dict
-
- cur_class = None
- dict = { }
- _modules[module] = dict
- imports = []
- lineno = 0
- while 1:
- line = f.readline()
- if not line:
- break
-
- lineno = lineno + 1
- line = line[:-1]
- if blank_line.match(line):
- continue
-
- res = is_import.match(line)
- res = is_from.match(line)
- if res:
- mod = res.group('module')
- names = string.splitfields(res.group('imp'), ',')
-
- try:
- d = readmodule(mod, path, inpackage)
- except:
- None if res else string.splitfields(res.group('imp'), ',')
- print 'module', mod, 'not found'
- continue
-
- for n in names:
- n = string.strip(n)
- if d.has_key(n):
- dict[n] = d[n]
- elif n == '*':
- for n in d.keys():
- pass
-
-
-
- continue
-
- res = is_class.match(line)
- if res:
- class_name = res.group('id')
- inherit = res.group('sup')
- if inherit:
- inherit = string.strip(inherit[1:-1])
- names = []
- for n in string.splitfields(inherit, ','):
- n = string.strip(n)
- names.append(n)
-
- inherit = names
-
- cur_class = Class(module, class_name, inherit, file, lineno)
- dict[class_name] = cur_class
- continue
-
- res = is_method.match(line)
- if res:
- if cur_class:
- meth_name = res.group('id')
- cur_class._addmethod(meth_name, lineno)
-
- continue
-
- if dedent.match(line):
- cur_class = None
-
- f.close()
- return dict
-
-