csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 1 | """Parse a Python module and describe its classes and functions. |
Sjoerd Mullender | 8cb4b1f | 1995-07-28 09:30:01 +0000 | [diff] [blame] | 2 | |
Guido van Rossum | 0a6f954 | 2002-12-03 08:14:35 +0000 | [diff] [blame] | 3 | Parse enough of a Python file to recognize imports and class and |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 4 | function definitions, and to find out the superclasses of a class. |
Sjoerd Mullender | 8cb4b1f | 1995-07-28 09:30:01 +0000 | [diff] [blame] | 5 | |
| 6 | The interface consists of a single function: |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 7 | readmodule_ex(module, path=None) |
Guido van Rossum | 0a6f954 | 2002-12-03 08:14:35 +0000 | [diff] [blame] | 8 | where module is the name of a Python module, and path is an optional |
| 9 | list of directories where the module is to be searched. If present, |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 10 | path is prepended to the system search path sys.path. The return value |
| 11 | is a dictionary. The keys of the dictionary are the names of the |
| 12 | classes and functions defined in the module (including classes that are |
| 13 | defined via the from XXX import YYY construct). The values are |
| 14 | instances of classes Class and Function. One special key/value pair is |
| 15 | present for packages: the key '__path__' has a list as its value which |
| 16 | contains the package search path. |
Sjoerd Mullender | 8cb4b1f | 1995-07-28 09:30:01 +0000 | [diff] [blame] | 17 | |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 18 | Classes and Functions have a common superclass: _Object. Every instance |
| 19 | has the following attributes: |
| 20 | module -- name of the module; |
| 21 | name -- name of the object; |
| 22 | file -- file in which the object is defined; |
| 23 | lineno -- line in the file where the object's definition starts; |
| 24 | parent -- parent of this object, if any; |
| 25 | children -- nested objects contained in this object. |
| 26 | The 'children' attribute is a dictionary mapping names to objects. |
| 27 | |
| 28 | Instances of Function describe functions with the attributes from _Object. |
| 29 | |
| 30 | Instances of Class describe classes with the attributes from _Object, |
| 31 | plus the following: |
| 32 | super -- list of super classes (Class instances if possible); |
| 33 | methods -- mapping of method names to beginning line numbers. |
Sjoerd Mullender | 8cb4b1f | 1995-07-28 09:30:01 +0000 | [diff] [blame] | 34 | If the name of a super class is not recognized, the corresponding |
| 35 | entry in the list of super classes is not a class instance but a |
| 36 | string giving the name of the super class. Since import statements |
| 37 | are recognized and imported modules are scanned as well, this |
| 38 | shouldn't happen often. |
Guido van Rossum | 4b8c6ea | 2000-02-04 15:39:30 +0000 | [diff] [blame] | 39 | """ |
Sjoerd Mullender | 8cb4b1f | 1995-07-28 09:30:01 +0000 | [diff] [blame] | 40 | |
Brett Cannon | ee78a2b | 2012-05-12 17:43:17 -0400 | [diff] [blame] | 41 | import io |
Sjoerd Mullender | 8cb4b1f | 1995-07-28 09:30:01 +0000 | [diff] [blame] | 42 | import sys |
Eric Snow | 6029e08 | 2014-01-25 15:32:46 -0700 | [diff] [blame] | 43 | import importlib.util |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 44 | import tokenize |
| 45 | from token import NAME, DEDENT, OP |
Sjoerd Mullender | 8cb4b1f | 1995-07-28 09:30:01 +0000 | [diff] [blame] | 46 | |
Guido van Rossum | 0ed7aa1 | 2002-12-02 14:54:20 +0000 | [diff] [blame] | 47 | __all__ = ["readmodule", "readmodule_ex", "Class", "Function"] |
Skip Montanaro | c62c81e | 2001-02-12 02:00:42 +0000 | [diff] [blame] | 48 | |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 49 | _modules = {} # Initialize cache of modules we've seen. |
Sjoerd Mullender | 8cb4b1f | 1995-07-28 09:30:01 +0000 | [diff] [blame] | 50 | |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 51 | |
| 52 | class _Object: |
Xtreak | 0d70227 | 2019-06-03 04:42:33 +0530 | [diff] [blame] | 53 | "Information about Python class or function." |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 54 | def __init__(self, module, name, file, lineno, parent): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 55 | self.module = module |
| 56 | self.name = name |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 57 | self.file = file |
| 58 | self.lineno = lineno |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 59 | self.parent = parent |
| 60 | self.children = {} |
| 61 | |
| 62 | def _addchild(self, name, obj): |
| 63 | self.children[name] = obj |
| 64 | |
| 65 | |
| 66 | class Function(_Object): |
| 67 | "Information about a Python function, including methods." |
| 68 | def __init__(self, module, name, file, lineno, parent=None): |
| 69 | _Object.__init__(self, module, name, file, lineno, parent) |
| 70 | |
| 71 | |
| 72 | class Class(_Object): |
| 73 | "Information about a Python class." |
| 74 | def __init__(self, module, name, super, file, lineno, parent=None): |
| 75 | _Object.__init__(self, module, name, file, lineno, parent) |
| 76 | self.super = [] if super is None else super |
| 77 | self.methods = {} |
Sjoerd Mullender | 8cb4b1f | 1995-07-28 09:30:01 +0000 | [diff] [blame] | 78 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 79 | def _addmethod(self, name, lineno): |
| 80 | self.methods[name] = lineno |
Sjoerd Mullender | 8cb4b1f | 1995-07-28 09:30:01 +0000 | [diff] [blame] | 81 | |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 82 | |
| 83 | def _nest_function(ob, func_name, lineno): |
| 84 | "Return a Function after nesting within ob." |
| 85 | newfunc = Function(ob.module, func_name, ob.file, lineno, ob) |
| 86 | ob._addchild(func_name, newfunc) |
| 87 | if isinstance(ob, Class): |
| 88 | ob._addmethod(func_name, lineno) |
| 89 | return newfunc |
| 90 | |
| 91 | def _nest_class(ob, class_name, lineno, super=None): |
| 92 | "Return a Class after nesting within ob." |
| 93 | newclass = Class(ob.module, class_name, super, ob.file, lineno, ob) |
| 94 | ob._addchild(class_name, newclass) |
| 95 | return newclass |
Guido van Rossum | a3b4a33 | 1999-06-10 14:39:39 +0000 | [diff] [blame] | 96 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 97 | def readmodule(module, path=None): |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 98 | """Return Class objects for the top-level classes in module. |
Guido van Rossum | a3b4a33 | 1999-06-10 14:39:39 +0000 | [diff] [blame] | 99 | |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 100 | This is the original interface, before Functions were added. |
| 101 | """ |
Guido van Rossum | a3b4a33 | 1999-06-10 14:39:39 +0000 | [diff] [blame] | 102 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 103 | res = {} |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 104 | for key, value in _readmodule(module, path or []).items(): |
Guido van Rossum | 0ed7aa1 | 2002-12-02 14:54:20 +0000 | [diff] [blame] | 105 | if isinstance(value, Class): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 106 | res[key] = value |
| 107 | return res |
Guido van Rossum | a3b4a33 | 1999-06-10 14:39:39 +0000 | [diff] [blame] | 108 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 109 | def readmodule_ex(module, path=None): |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 110 | """Return a dictionary with all functions and classes in module. |
Sjoerd Mullender | 8cb4b1f | 1995-07-28 09:30:01 +0000 | [diff] [blame] | 111 | |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 112 | Search for module in PATH + sys.path. |
| 113 | If possible, include imported superclasses. |
| 114 | Do this by reading source, without importing (and executing) it. |
| 115 | """ |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 116 | return _readmodule(module, path or []) |
Sjoerd Mullender | 8cb4b1f | 1995-07-28 09:30:01 +0000 | [diff] [blame] | 117 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 118 | def _readmodule(module, path, inpackage=None): |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 119 | """Do the hard work for readmodule[_ex]. |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 120 | |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 121 | If inpackage is given, it must be the dotted name of the package in |
Guido van Rossum | 0ed7aa1 | 2002-12-02 14:54:20 +0000 | [diff] [blame] | 122 | which we are searching for a submodule, and then PATH must be the |
| 123 | package search path; otherwise, we are searching for a top-level |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 124 | module, and path is combined with sys.path. |
| 125 | """ |
| 126 | # Compute the full module name (prepending inpackage if set). |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 127 | if inpackage is not None: |
Guido van Rossum | 0ed7aa1 | 2002-12-02 14:54:20 +0000 | [diff] [blame] | 128 | fullmodule = "%s.%s" % (inpackage, module) |
| 129 | else: |
| 130 | fullmodule = module |
| 131 | |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 132 | # Check in the cache. |
Guido van Rossum | 0ed7aa1 | 2002-12-02 14:54:20 +0000 | [diff] [blame] | 133 | if fullmodule in _modules: |
| 134 | return _modules[fullmodule] |
| 135 | |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 136 | # Initialize the dict for this module's contents. |
| 137 | tree = {} |
Guido van Rossum | 3d54871 | 1999-06-09 15:49:09 +0000 | [diff] [blame] | 138 | |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 139 | # Check if it is a built-in module; we don't do much for these. |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 140 | if module in sys.builtin_module_names and inpackage is None: |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 141 | _modules[module] = tree |
| 142 | return tree |
Sjoerd Mullender | 8cb4b1f | 1995-07-28 09:30:01 +0000 | [diff] [blame] | 143 | |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 144 | # Check for a dotted module name. |
Guido van Rossum | 0ed7aa1 | 2002-12-02 14:54:20 +0000 | [diff] [blame] | 145 | i = module.rfind('.') |
| 146 | if i >= 0: |
| 147 | package = module[:i] |
| 148 | submodule = module[i+1:] |
Guido van Rossum | 0a6f954 | 2002-12-03 08:14:35 +0000 | [diff] [blame] | 149 | parent = _readmodule(package, path, inpackage) |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 150 | if inpackage is not None: |
Guido van Rossum | 0ed7aa1 | 2002-12-02 14:54:20 +0000 | [diff] [blame] | 151 | package = "%s.%s" % (inpackage, package) |
Petri Lehtinen | 8d88604 | 2012-05-18 21:51:11 +0300 | [diff] [blame] | 152 | if not '__path__' in parent: |
| 153 | raise ImportError('No package named {}'.format(package)) |
Guido van Rossum | 0a6f954 | 2002-12-03 08:14:35 +0000 | [diff] [blame] | 154 | return _readmodule(submodule, parent['__path__'], package) |
Guido van Rossum | 0ed7aa1 | 2002-12-02 14:54:20 +0000 | [diff] [blame] | 155 | |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 156 | # Search the path for the module. |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 157 | f = None |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 158 | if inpackage is not None: |
Brett Cannon | ee78a2b | 2012-05-12 17:43:17 -0400 | [diff] [blame] | 159 | search_path = path |
Guido van Rossum | 0ed7aa1 | 2002-12-02 14:54:20 +0000 | [diff] [blame] | 160 | else: |
Brett Cannon | ee78a2b | 2012-05-12 17:43:17 -0400 | [diff] [blame] | 161 | search_path = path + sys.path |
Eric Snow | 6029e08 | 2014-01-25 15:32:46 -0700 | [diff] [blame] | 162 | spec = importlib.util._find_spec_from_path(fullmodule, search_path) |
Brett Cannon | 5086589 | 2019-03-22 15:16:50 -0700 | [diff] [blame] | 163 | if spec is None: |
| 164 | raise ModuleNotFoundError(f"no module named {fullmodule!r}", name=fullmodule) |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 165 | _modules[fullmodule] = tree |
| 166 | # Is module a package? |
Victor Stinner | 5c13aa1 | 2016-03-17 09:06:41 +0100 | [diff] [blame] | 167 | if spec.submodule_search_locations is not None: |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 168 | tree['__path__'] = spec.submodule_search_locations |
Brett Cannon | ee78a2b | 2012-05-12 17:43:17 -0400 | [diff] [blame] | 169 | try: |
Eric Snow | 02b9f9d | 2014-01-06 20:42:59 -0700 | [diff] [blame] | 170 | source = spec.loader.get_source(fullmodule) |
Brett Cannon | ee78a2b | 2012-05-12 17:43:17 -0400 | [diff] [blame] | 171 | except (AttributeError, ImportError): |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 172 | # If module is not Python source, we cannot do anything. |
| 173 | return tree |
Brett Cannon | 5086589 | 2019-03-22 15:16:50 -0700 | [diff] [blame] | 174 | else: |
| 175 | if source is None: |
| 176 | return tree |
Sjoerd Mullender | 8cb4b1f | 1995-07-28 09:30:01 +0000 | [diff] [blame] | 177 | |
Victor Stinner | 5c13aa1 | 2016-03-17 09:06:41 +0100 | [diff] [blame] | 178 | fname = spec.loader.get_filename(fullmodule) |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 179 | return _create_tree(fullmodule, path, fname, source, tree, inpackage) |
Victor Stinner | 5c13aa1 | 2016-03-17 09:06:41 +0100 | [diff] [blame] | 180 | |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 181 | |
| 182 | def _create_tree(fullmodule, path, fname, source, tree, inpackage): |
| 183 | """Return the tree for a particular module. |
| 184 | |
| 185 | fullmodule (full module name), inpackage+module, becomes o.module. |
| 186 | path is passed to recursive calls of _readmodule. |
| 187 | fname becomes o.file. |
| 188 | source is tokenized. Imports cause recursive calls to _readmodule. |
| 189 | tree is {} or {'__path__': <submodule search locations>}. |
| 190 | inpackage, None or string, is passed to recursive calls of _readmodule. |
| 191 | |
| 192 | The effect of recursive calls is mutation of global _modules. |
| 193 | """ |
Brett Cannon | ee78a2b | 2012-05-12 17:43:17 -0400 | [diff] [blame] | 194 | f = io.StringIO(source) |
| 195 | |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 196 | stack = [] # Initialize stack of (class, indent) pairs. |
Guido van Rossum | ad38055 | 1999-06-07 15:25:18 +0000 | [diff] [blame] | 197 | |
Guido van Rossum | 040d7ca | 2002-08-23 01:36:01 +0000 | [diff] [blame] | 198 | g = tokenize.generate_tokens(f.readline) |
| 199 | try: |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 200 | for tokentype, token, start, _end, _line in g: |
Guido van Rossum | 0a6f954 | 2002-12-03 08:14:35 +0000 | [diff] [blame] | 201 | if tokentype == DEDENT: |
Guido van Rossum | 040d7ca | 2002-08-23 01:36:01 +0000 | [diff] [blame] | 202 | lineno, thisindent = start |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 203 | # Close previous nested classes and defs. |
Guido van Rossum | 0a6f954 | 2002-12-03 08:14:35 +0000 | [diff] [blame] | 204 | while stack and stack[-1][1] >= thisindent: |
| 205 | del stack[-1] |
| 206 | elif token == 'def': |
| 207 | lineno, thisindent = start |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 208 | # Close previous nested classes and defs. |
Guido van Rossum | 0a6f954 | 2002-12-03 08:14:35 +0000 | [diff] [blame] | 209 | while stack and stack[-1][1] >= thisindent: |
| 210 | del stack[-1] |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 211 | tokentype, func_name, start = next(g)[0:3] |
Guido van Rossum | 040d7ca | 2002-08-23 01:36:01 +0000 | [diff] [blame] | 212 | if tokentype != NAME: |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 213 | continue # Skip def with syntax error. |
| 214 | cur_func = None |
Guido van Rossum | 0a6f954 | 2002-12-03 08:14:35 +0000 | [diff] [blame] | 215 | if stack: |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 216 | cur_obj = stack[-1][0] |
| 217 | cur_func = _nest_function(cur_obj, func_name, lineno) |
Guido van Rossum | 040d7ca | 2002-08-23 01:36:01 +0000 | [diff] [blame] | 218 | else: |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 219 | # It is just a function. |
| 220 | cur_func = Function(fullmodule, func_name, fname, lineno) |
| 221 | tree[func_name] = cur_func |
| 222 | stack.append((cur_func, thisindent)) |
Guido van Rossum | 040d7ca | 2002-08-23 01:36:01 +0000 | [diff] [blame] | 223 | elif token == 'class': |
| 224 | lineno, thisindent = start |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 225 | # Close previous nested classes and defs. |
Guido van Rossum | 0a6f954 | 2002-12-03 08:14:35 +0000 | [diff] [blame] | 226 | while stack and stack[-1][1] >= thisindent: |
| 227 | del stack[-1] |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 228 | tokentype, class_name, start = next(g)[0:3] |
Guido van Rossum | 040d7ca | 2002-08-23 01:36:01 +0000 | [diff] [blame] | 229 | if tokentype != NAME: |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 230 | continue # Skip class with syntax error. |
| 231 | # Parse what follows the class name. |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 232 | tokentype, token, start = next(g)[0:3] |
Guido van Rossum | 040d7ca | 2002-08-23 01:36:01 +0000 | [diff] [blame] | 233 | inherit = None |
| 234 | if token == '(': |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 235 | names = [] # Initialize list of superclasses. |
Guido van Rossum | 040d7ca | 2002-08-23 01:36:01 +0000 | [diff] [blame] | 236 | level = 1 |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 237 | super = [] # Tokens making up current superclass. |
Guido van Rossum | 040d7ca | 2002-08-23 01:36:01 +0000 | [diff] [blame] | 238 | while True: |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 239 | tokentype, token, start = next(g)[0:3] |
Guido van Rossum | 040d7ca | 2002-08-23 01:36:01 +0000 | [diff] [blame] | 240 | if token in (')', ',') and level == 1: |
| 241 | n = "".join(super) |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 242 | if n in tree: |
| 243 | # We know this super class. |
| 244 | n = tree[n] |
Guido van Rossum | 040d7ca | 2002-08-23 01:36:01 +0000 | [diff] [blame] | 245 | else: |
| 246 | c = n.split('.') |
| 247 | if len(c) > 1: |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 248 | # Super class form is module.class: |
| 249 | # look in module for class. |
Guido van Rossum | 040d7ca | 2002-08-23 01:36:01 +0000 | [diff] [blame] | 250 | m = c[-2] |
| 251 | c = c[-1] |
| 252 | if m in _modules: |
| 253 | d = _modules[m] |
| 254 | if c in d: |
| 255 | n = d[c] |
| 256 | names.append(n) |
Guido van Rossum | 0a6f954 | 2002-12-03 08:14:35 +0000 | [diff] [blame] | 257 | super = [] |
Guido van Rossum | 040d7ca | 2002-08-23 01:36:01 +0000 | [diff] [blame] | 258 | if token == '(': |
| 259 | level += 1 |
| 260 | elif token == ')': |
| 261 | level -= 1 |
| 262 | if level == 0: |
| 263 | break |
| 264 | elif token == ',' and level == 1: |
| 265 | pass |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 266 | # Only use NAME and OP (== dot) tokens for type name. |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 267 | elif tokentype in (NAME, OP) and level == 1: |
Guido van Rossum | 040d7ca | 2002-08-23 01:36:01 +0000 | [diff] [blame] | 268 | super.append(token) |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 269 | # Expressions in the base list are not supported. |
Guido van Rossum | 040d7ca | 2002-08-23 01:36:01 +0000 | [diff] [blame] | 270 | inherit = names |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 271 | if stack: |
| 272 | cur_obj = stack[-1][0] |
| 273 | cur_class = _nest_class( |
| 274 | cur_obj, class_name, lineno, inherit) |
| 275 | else: |
| 276 | cur_class = Class(fullmodule, class_name, inherit, |
| 277 | fname, lineno) |
| 278 | tree[class_name] = cur_class |
Guido van Rossum | 0a6f954 | 2002-12-03 08:14:35 +0000 | [diff] [blame] | 279 | stack.append((cur_class, thisindent)) |
Guido van Rossum | 040d7ca | 2002-08-23 01:36:01 +0000 | [diff] [blame] | 280 | elif token == 'import' and start[1] == 0: |
| 281 | modules = _getnamelist(g) |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 282 | for mod, _mod2 in modules: |
Guido van Rossum | 258cba8 | 2002-09-16 16:36:02 +0000 | [diff] [blame] | 283 | try: |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 284 | # Recursively read the imported module. |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 285 | if inpackage is None: |
Guido van Rossum | 0a6f954 | 2002-12-03 08:14:35 +0000 | [diff] [blame] | 286 | _readmodule(mod, path) |
Guido van Rossum | 0ed7aa1 | 2002-12-02 14:54:20 +0000 | [diff] [blame] | 287 | else: |
| 288 | try: |
Guido van Rossum | 0a6f954 | 2002-12-03 08:14:35 +0000 | [diff] [blame] | 289 | _readmodule(mod, path, inpackage) |
Guido van Rossum | 0ed7aa1 | 2002-12-02 14:54:20 +0000 | [diff] [blame] | 290 | except ImportError: |
Guido van Rossum | 0a6f954 | 2002-12-03 08:14:35 +0000 | [diff] [blame] | 291 | _readmodule(mod, []) |
Guido van Rossum | 258cba8 | 2002-09-16 16:36:02 +0000 | [diff] [blame] | 292 | except: |
| 293 | # If we can't find or parse the imported module, |
| 294 | # too bad -- don't die here. |
| 295 | pass |
Guido van Rossum | 040d7ca | 2002-08-23 01:36:01 +0000 | [diff] [blame] | 296 | elif token == 'from' and start[1] == 0: |
| 297 | mod, token = _getname(g) |
| 298 | if not mod or token != "import": |
| 299 | continue |
| 300 | names = _getnamelist(g) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 301 | try: |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 302 | # Recursively read the imported module. |
Guido van Rossum | 0a6f954 | 2002-12-03 08:14:35 +0000 | [diff] [blame] | 303 | d = _readmodule(mod, path, inpackage) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 304 | except: |
Guido van Rossum | 258cba8 | 2002-09-16 16:36:02 +0000 | [diff] [blame] | 305 | # If we can't find or parse the imported module, |
| 306 | # too bad -- don't die here. |
Guido van Rossum | 040d7ca | 2002-08-23 01:36:01 +0000 | [diff] [blame] | 307 | continue |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 308 | # Add any classes that were defined in the imported module |
| 309 | # to our name space if they were mentioned in the list. |
Guido van Rossum | 040d7ca | 2002-08-23 01:36:01 +0000 | [diff] [blame] | 310 | for n, n2 in names: |
| 311 | if n in d: |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 312 | tree[n2 or n] = d[n] |
Guido van Rossum | 040d7ca | 2002-08-23 01:36:01 +0000 | [diff] [blame] | 313 | elif n == '*': |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 314 | # Don't add names that start with _. |
Guido van Rossum | 040d7ca | 2002-08-23 01:36:01 +0000 | [diff] [blame] | 315 | for n in d: |
Guido van Rossum | 0a6f954 | 2002-12-03 08:14:35 +0000 | [diff] [blame] | 316 | if n[0] != '_': |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 317 | tree[n] = d[n] |
Guido van Rossum | 040d7ca | 2002-08-23 01:36:01 +0000 | [diff] [blame] | 318 | except StopIteration: |
| 319 | pass |
Guido van Rossum | ad38055 | 1999-06-07 15:25:18 +0000 | [diff] [blame] | 320 | |
Guido van Rossum | 040d7ca | 2002-08-23 01:36:01 +0000 | [diff] [blame] | 321 | f.close() |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 322 | return tree |
| 323 | |
Guido van Rossum | df9f7a3 | 1999-06-08 12:53:21 +0000 | [diff] [blame] | 324 | |
Guido van Rossum | 040d7ca | 2002-08-23 01:36:01 +0000 | [diff] [blame] | 325 | def _getnamelist(g): |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 326 | """Return list of (dotted-name, as-name or None) tuples for token source g. |
| 327 | |
| 328 | An as-name is the name that follows 'as' in an as clause. |
| 329 | """ |
Guido van Rossum | 040d7ca | 2002-08-23 01:36:01 +0000 | [diff] [blame] | 330 | names = [] |
| 331 | while True: |
| 332 | name, token = _getname(g) |
| 333 | if not name: |
| 334 | break |
| 335 | if token == 'as': |
| 336 | name2, token = _getname(g) |
| 337 | else: |
| 338 | name2 = None |
| 339 | names.append((name, name2)) |
| 340 | while token != "," and "\n" not in token: |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 341 | token = next(g)[1] |
Guido van Rossum | 040d7ca | 2002-08-23 01:36:01 +0000 | [diff] [blame] | 342 | if token != ",": |
| 343 | break |
| 344 | return names |
| 345 | |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 346 | |
Guido van Rossum | 040d7ca | 2002-08-23 01:36:01 +0000 | [diff] [blame] | 347 | def _getname(g): |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 348 | "Return (dotted-name or None, next-token) tuple for token source g." |
Guido van Rossum | 040d7ca | 2002-08-23 01:36:01 +0000 | [diff] [blame] | 349 | parts = [] |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 350 | tokentype, token = next(g)[0:2] |
Guido van Rossum | 040d7ca | 2002-08-23 01:36:01 +0000 | [diff] [blame] | 351 | if tokentype != NAME and token != '*': |
| 352 | return (None, token) |
| 353 | parts.append(token) |
| 354 | while True: |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 355 | tokentype, token = next(g)[0:2] |
Guido van Rossum | 040d7ca | 2002-08-23 01:36:01 +0000 | [diff] [blame] | 356 | if token != '.': |
| 357 | break |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 358 | tokentype, token = next(g)[0:2] |
Guido van Rossum | 040d7ca | 2002-08-23 01:36:01 +0000 | [diff] [blame] | 359 | if tokentype != NAME: |
| 360 | break |
| 361 | parts.append(token) |
| 362 | return (".".join(parts), token) |
Guido van Rossum | 0a6f954 | 2002-12-03 08:14:35 +0000 | [diff] [blame] | 363 | |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 364 | |
Guido van Rossum | 0a6f954 | 2002-12-03 08:14:35 +0000 | [diff] [blame] | 365 | def _main(): |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 366 | "Print module output (default this file) for quick visual check." |
Guido van Rossum | 0a6f954 | 2002-12-03 08:14:35 +0000 | [diff] [blame] | 367 | import os |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 368 | try: |
| 369 | mod = sys.argv[1] |
| 370 | except: |
| 371 | mod = __file__ |
Guido van Rossum | 0a6f954 | 2002-12-03 08:14:35 +0000 | [diff] [blame] | 372 | if os.path.exists(mod): |
| 373 | path = [os.path.dirname(mod)] |
| 374 | mod = os.path.basename(mod) |
| 375 | if mod.lower().endswith(".py"): |
| 376 | mod = mod[:-3] |
| 377 | else: |
| 378 | path = [] |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 379 | tree = readmodule_ex(mod, path) |
| 380 | lineno_key = lambda a: getattr(a, 'lineno', 0) |
| 381 | objs = sorted(tree.values(), key=lineno_key, reverse=True) |
| 382 | indent_level = 2 |
| 383 | while objs: |
| 384 | obj = objs.pop() |
| 385 | if isinstance(obj, list): |
| 386 | # Value is a __path__ key. |
| 387 | continue |
| 388 | if not hasattr(obj, 'indent'): |
| 389 | obj.indent = 0 |
| 390 | |
| 391 | if isinstance(obj, _Object): |
| 392 | new_objs = sorted(obj.children.values(), |
| 393 | key=lineno_key, reverse=True) |
| 394 | for ob in new_objs: |
| 395 | ob.indent = obj.indent + indent_level |
| 396 | objs.extend(new_objs) |
Guido van Rossum | 0a6f954 | 2002-12-03 08:14:35 +0000 | [diff] [blame] | 397 | if isinstance(obj, Class): |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 398 | print("{}class {} {} {}" |
| 399 | .format(' ' * obj.indent, obj.name, obj.super, obj.lineno)) |
Guido van Rossum | 0a6f954 | 2002-12-03 08:14:35 +0000 | [diff] [blame] | 400 | elif isinstance(obj, Function): |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 401 | print("{}def {} {}".format(' ' * obj.indent, obj.name, obj.lineno)) |
Guido van Rossum | 0a6f954 | 2002-12-03 08:14:35 +0000 | [diff] [blame] | 402 | |
| 403 | if __name__ == "__main__": |
| 404 | _main() |