paul@0 | 1 | #!/usr/bin/env python |
paul@0 | 2 | |
paul@0 | 3 | """ |
paul@0 | 4 | Module abstractions. |
paul@0 | 5 | |
paul@0 | 6 | Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012, 2013, |
paul@0 | 7 | 2014, 2015, 2016 Paul Boddie <paul@boddie.org.uk> |
paul@0 | 8 | |
paul@0 | 9 | This program is free software; you can redistribute it and/or modify it under |
paul@0 | 10 | the terms of the GNU General Public License as published by the Free Software |
paul@0 | 11 | Foundation; either version 3 of the License, or (at your option) any later |
paul@0 | 12 | version. |
paul@0 | 13 | |
paul@0 | 14 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@0 | 15 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@0 | 16 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@0 | 17 | details. |
paul@0 | 18 | |
paul@0 | 19 | You should have received a copy of the GNU General Public License along with |
paul@0 | 20 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@0 | 21 | """ |
paul@0 | 22 | |
paul@0 | 23 | from common import * |
paul@0 | 24 | from encoders import decode_modifier_term, encode_modifiers, encode_usage |
paul@0 | 25 | from referencing import decode_reference, Reference |
paul@0 | 26 | import sys |
paul@0 | 27 | |
paul@0 | 28 | class BasicModule(CommonModule): |
paul@0 | 29 | |
paul@0 | 30 | "The basic module information." |
paul@0 | 31 | |
paul@0 | 32 | def __init__(self, name, importer): |
paul@0 | 33 | CommonModule.__init__(self, name, importer) |
paul@0 | 34 | |
paul@0 | 35 | # Import machinery links. |
paul@0 | 36 | |
paul@0 | 37 | self.loaded = False |
paul@0 | 38 | |
paul@0 | 39 | # Module dependencies. |
paul@0 | 40 | |
paul@0 | 41 | self.imported = set() |
paul@0 | 42 | self.imported_hidden = set() |
paul@0 | 43 | self.imported_names = {} |
paul@0 | 44 | self.revealed = set() |
paul@0 | 45 | self.accessing_modules = set() |
paul@0 | 46 | |
paul@0 | 47 | # Global name information. |
paul@0 | 48 | |
paul@0 | 49 | self.objects = {} |
paul@0 | 50 | self.special = {} |
paul@0 | 51 | |
paul@0 | 52 | # Class relationships. |
paul@0 | 53 | |
paul@0 | 54 | self.classes = {} |
paul@0 | 55 | |
paul@0 | 56 | # Attributes. |
paul@0 | 57 | |
paul@0 | 58 | self.class_attrs = {} |
paul@0 | 59 | self.instance_attrs = {} |
paul@0 | 60 | self.instance_attr_constants = {} |
paul@0 | 61 | self.module_attrs = set() |
paul@0 | 62 | |
paul@0 | 63 | # Names used and missing. |
paul@0 | 64 | |
paul@0 | 65 | self.names_used = {} |
paul@0 | 66 | self.names_missing = {} |
paul@0 | 67 | self.name_references = {} # references to globals |
paul@0 | 68 | |
paul@0 | 69 | # Function details. |
paul@0 | 70 | |
paul@0 | 71 | self.function_parameters = {} |
paul@0 | 72 | self.function_defaults = {} |
paul@0 | 73 | self.function_locals = {} |
paul@0 | 74 | self.scope_globals = {} |
paul@0 | 75 | |
paul@0 | 76 | # Invocation details. |
paul@0 | 77 | |
paul@0 | 78 | self.function_targets = {} |
paul@0 | 79 | self.function_arguments = {} |
paul@0 | 80 | |
paul@0 | 81 | # Attribute usage at module and function levels. |
paul@0 | 82 | |
paul@0 | 83 | self.attr_usage = {} |
paul@0 | 84 | self.name_initialisers = {} |
paul@0 | 85 | |
paul@0 | 86 | # General attribute access expressions. |
paul@0 | 87 | |
paul@0 | 88 | self.attr_accesses = {} |
paul@0 | 89 | self.const_accesses = {} |
paul@0 | 90 | |
paul@0 | 91 | # Attribute accessor definition details. |
paul@0 | 92 | |
paul@0 | 93 | self.attr_accessors = {} |
paul@0 | 94 | |
paul@0 | 95 | # Assignment details for accesses. |
paul@0 | 96 | |
paul@0 | 97 | self.attr_access_modifiers = {} |
paul@0 | 98 | |
paul@0 | 99 | # Initialisation-related details. |
paul@0 | 100 | |
paul@0 | 101 | self.initialised_names = {} |
paul@0 | 102 | self.aliased_names = {} |
paul@0 | 103 | |
paul@0 | 104 | def __repr__(self): |
paul@0 | 105 | return "BasicModule(%r, %r)" % (self.name, self.importer) |
paul@0 | 106 | |
paul@0 | 107 | def resolve(self): |
paul@0 | 108 | |
paul@0 | 109 | "Resolve dependencies and complete definitions." |
paul@0 | 110 | |
paul@0 | 111 | self.resolve_class_bases() |
paul@0 | 112 | self.check_special() |
paul@0 | 113 | self.check_names_used() |
paul@0 | 114 | self.resolve_members() |
paul@0 | 115 | self.resolve_initialisers() |
paul@0 | 116 | self.resolve_literals() |
paul@0 | 117 | self.remove_redundant_accessors() |
paul@0 | 118 | self.set_invocation_usage() |
paul@0 | 119 | |
paul@0 | 120 | # Propagate to the importer information needed in subsequent activities. |
paul@0 | 121 | |
paul@0 | 122 | self.propagate() |
paul@0 | 123 | |
paul@0 | 124 | # Derived information methods. |
paul@0 | 125 | |
paul@0 | 126 | def propagate(self): |
paul@0 | 127 | |
paul@0 | 128 | "Finalise and propagate module information." |
paul@0 | 129 | |
paul@0 | 130 | self.propagate_attrs() |
paul@0 | 131 | self.propagate_name_references() |
paul@0 | 132 | self.propagate_attr_accesses() |
paul@0 | 133 | self.propagate_constants() |
paul@0 | 134 | |
paul@0 | 135 | def unpropagate(self): |
paul@0 | 136 | |
paul@0 | 137 | """ |
paul@0 | 138 | Retract information from the importer including information about this |
paul@0 | 139 | module derived by the importer. |
paul@0 | 140 | """ |
paul@0 | 141 | |
paul@0 | 142 | del self.importer.all_module_attrs[self.name] |
paul@0 | 143 | |
paul@0 | 144 | for name in self.classes.keys(): |
paul@0 | 145 | del self.importer.all_class_attrs[name] |
paul@0 | 146 | del self.importer.all_instance_attrs[name] |
paul@0 | 147 | del self.importer.all_combined_attrs[name] |
paul@0 | 148 | del self.importer.all_instance_attr_constants[name] |
paul@0 | 149 | |
paul@0 | 150 | for name, bases in self.classes.items(): |
paul@0 | 151 | for base in bases: |
paul@0 | 152 | |
paul@0 | 153 | # Get the identity of the class from the reference. |
paul@0 | 154 | |
paul@0 | 155 | base = base.get_origin() |
paul@0 | 156 | |
paul@0 | 157 | try: |
paul@0 | 158 | self.importer.subclasses[base].remove(name) |
paul@0 | 159 | except (KeyError, ValueError): |
paul@0 | 160 | pass |
paul@0 | 161 | |
paul@0 | 162 | remove_items(self.importer.all_name_references, self.name_references) |
paul@0 | 163 | remove_items(self.importer.all_initialised_names, self.initialised_names) |
paul@0 | 164 | remove_items(self.importer.all_aliased_names, self.aliased_names) |
paul@0 | 165 | remove_items(self.importer.all_attr_accesses, self.attr_accesses) |
paul@0 | 166 | remove_items(self.importer.all_const_accesses, self.const_accesses) |
paul@0 | 167 | remove_items(self.importer.all_attr_access_modifiers, self.attr_access_modifiers) |
paul@0 | 168 | remove_items(self.importer.all_constants, self.constants) |
paul@0 | 169 | remove_items(self.importer.all_constant_values, self.constant_values) |
paul@0 | 170 | |
paul@0 | 171 | # Remove this module's objects from the importer. Objects are |
paul@0 | 172 | # automatically propagated when defined. |
paul@0 | 173 | |
paul@0 | 174 | for name, ref in self.objects.items(): |
paul@0 | 175 | if ref.provided_by_module(self.name) or name in self.importer.hidden: |
paul@0 | 176 | if ref.get_kind() != "<module>": |
paul@0 | 177 | del self.importer.objects[name] |
paul@0 | 178 | |
paul@0 | 179 | def resolve_class_bases(self): |
paul@0 | 180 | |
paul@0 | 181 | "Resolve all class bases since some of them may have been deferred." |
paul@0 | 182 | |
paul@0 | 183 | for name, bases in self.classes.items(): |
paul@0 | 184 | resolved = [] |
paul@0 | 185 | bad = [] |
paul@0 | 186 | |
paul@0 | 187 | for base in bases: |
paul@0 | 188 | |
paul@0 | 189 | # Resolve dependencies. |
paul@0 | 190 | |
paul@0 | 191 | if base.has_kind("<depends>"): |
paul@0 | 192 | ref = self.importer.get_object(base.get_origin()) |
paul@0 | 193 | else: |
paul@0 | 194 | ref = base |
paul@0 | 195 | |
paul@0 | 196 | # Obtain the origin of the base class reference. |
paul@0 | 197 | |
paul@0 | 198 | if not ref or not ref.has_kind("<class>"): |
paul@0 | 199 | bad.append(base) |
paul@0 | 200 | break |
paul@0 | 201 | |
paul@0 | 202 | resolved.append(ref) |
paul@0 | 203 | |
paul@0 | 204 | if bad: |
paul@0 | 205 | print >>sys.stderr, "Bases of class %s were not classes." % (name, ", ".join(map(str, bad))) |
paul@0 | 206 | else: |
paul@0 | 207 | self.importer.classes[name] = self.classes[name] = resolved |
paul@0 | 208 | |
paul@0 | 209 | def propagate_attrs(self): |
paul@0 | 210 | |
paul@0 | 211 | "Derive attributes from the class and module member details." |
paul@0 | 212 | |
paul@0 | 213 | # Initialise class attribute records for all classes. |
paul@0 | 214 | |
paul@0 | 215 | for name in self.classes.keys(): |
paul@0 | 216 | self.importer.all_class_attrs[name] = self.class_attrs[name] = {} |
paul@0 | 217 | |
paul@0 | 218 | # Separate the objects into module and class attributes. |
paul@0 | 219 | |
paul@0 | 220 | for name in self.objects.keys(): |
paul@0 | 221 | if "." in name: |
paul@0 | 222 | parent, attrname = name.rsplit(".", 1) |
paul@0 | 223 | if self.classes.has_key(parent): |
paul@0 | 224 | self.class_attrs[parent][attrname] = name |
paul@0 | 225 | elif parent == self.name: |
paul@0 | 226 | self.module_attrs.add(attrname) |
paul@0 | 227 | |
paul@0 | 228 | # Propagate the module attributes. |
paul@0 | 229 | |
paul@0 | 230 | self.importer.all_module_attrs[self.name] = self.module_attrs |
paul@0 | 231 | |
paul@0 | 232 | def propagate_name_references(self): |
paul@0 | 233 | |
paul@0 | 234 | "Propagate name references for the module." |
paul@0 | 235 | |
paul@0 | 236 | self.importer.all_name_references.update(self.name_references) |
paul@0 | 237 | self.importer.all_initialised_names.update(self.initialised_names) |
paul@0 | 238 | self.importer.all_aliased_names.update(self.aliased_names) |
paul@0 | 239 | |
paul@0 | 240 | def propagate_attr_accesses(self): |
paul@0 | 241 | |
paul@0 | 242 | "Propagate attribute accesses for the module." |
paul@0 | 243 | |
paul@0 | 244 | self.importer.all_attr_accesses.update(self.attr_accesses) |
paul@0 | 245 | self.importer.all_const_accesses.update(self.const_accesses) |
paul@0 | 246 | self.importer.all_attr_access_modifiers.update(self.attr_access_modifiers) |
paul@0 | 247 | |
paul@0 | 248 | def propagate_constants(self): |
paul@0 | 249 | |
paul@0 | 250 | "Propagate constant values and aliases for the module." |
paul@0 | 251 | |
paul@0 | 252 | self.importer.all_constants.update(self.constants) |
paul@0 | 253 | self.importer.all_constant_values.update(self.constant_values) |
paul@0 | 254 | |
paul@0 | 255 | for name in self.classes.keys(): |
paul@0 | 256 | self.importer.all_instance_attrs[name] = self.instance_attrs.get(name) or {} |
paul@0 | 257 | self.importer.all_instance_attr_constants[name] = self.instance_attr_constants.get(name) or {} |
paul@0 | 258 | |
paul@0 | 259 | # Module-relative naming. |
paul@0 | 260 | |
paul@0 | 261 | def is_global(self, name): |
paul@0 | 262 | |
paul@0 | 263 | """ |
paul@0 | 264 | Return whether 'name' is registered as a global in the current |
paul@0 | 265 | namespace. |
paul@0 | 266 | """ |
paul@0 | 267 | |
paul@0 | 268 | path = self.get_namespace_path() |
paul@0 | 269 | return name in self.scope_globals.get(path, []) |
paul@0 | 270 | |
paul@0 | 271 | def get_globals(self): |
paul@0 | 272 | |
paul@0 | 273 | """ |
paul@0 | 274 | Get the globals from this module, returning a dictionary mapping names |
paul@0 | 275 | to references incorporating original definition details. |
paul@0 | 276 | """ |
paul@0 | 277 | |
paul@0 | 278 | l = [] |
paul@0 | 279 | for name, value in self.objects.items(): |
paul@0 | 280 | parent, attrname = name.rsplit(".", 1) |
paul@0 | 281 | if parent == self.name: |
paul@0 | 282 | l.append((attrname, value)) |
paul@0 | 283 | return dict(l) |
paul@0 | 284 | |
paul@0 | 285 | def get_global(self, name): |
paul@0 | 286 | |
paul@0 | 287 | """ |
paul@0 | 288 | Get the global of the given 'name' from this module, returning a |
paul@0 | 289 | reference incorporating the original definition details. |
paul@0 | 290 | """ |
paul@0 | 291 | |
paul@0 | 292 | path = self.get_global_path(name) |
paul@0 | 293 | return self.objects.get(path) |
paul@0 | 294 | |
paul@0 | 295 | # Name definition discovery. |
paul@0 | 296 | |
paul@0 | 297 | def get_global_or_builtin(self, name): |
paul@0 | 298 | |
paul@0 | 299 | """ |
paul@0 | 300 | Return the object recorded the given 'name' from this module or from the |
paul@0 | 301 | __builtins__ module. If no object has yet been defined, perhaps due to |
paul@0 | 302 | circular module references, None is returned. |
paul@0 | 303 | """ |
paul@0 | 304 | |
paul@0 | 305 | return self.get_global(name) or self.get_builtin(name) |
paul@0 | 306 | |
paul@0 | 307 | def get_builtin(self, name): |
paul@0 | 308 | |
paul@0 | 309 | "Return the object providing the given built-in 'name'." |
paul@0 | 310 | |
paul@0 | 311 | path = "__builtins__.%s" % name |
paul@0 | 312 | |
paul@0 | 313 | # Obtain __builtins__. |
paul@0 | 314 | |
paul@0 | 315 | module = self.ensure_builtins(path) |
paul@0 | 316 | |
paul@0 | 317 | # Attempt to find the named object within __builtins__. |
paul@0 | 318 | |
paul@0 | 319 | if module: |
paul@0 | 320 | self.find_imported_name(name, module.name, module) |
paul@0 | 321 | |
paul@0 | 322 | # Return the path and any reference for the named object. |
paul@0 | 323 | |
paul@0 | 324 | return self.importer.get_object(path) |
paul@0 | 325 | |
paul@0 | 326 | def ensure_builtins(self, path): |
paul@0 | 327 | |
paul@0 | 328 | """ |
paul@0 | 329 | If 'path' is a reference to an object within __builtins__, return the |
paul@0 | 330 | __builtins__ module. |
paul@0 | 331 | """ |
paul@0 | 332 | |
paul@0 | 333 | if path.split(".", 1)[0] == "__builtins__": |
paul@0 | 334 | return self.importer.load("__builtins__", True, True) |
paul@0 | 335 | else: |
paul@0 | 336 | return None |
paul@0 | 337 | |
paul@0 | 338 | def get_object(self, path): |
paul@0 | 339 | |
paul@0 | 340 | """ |
paul@0 | 341 | Get the details of an object with the given 'path', either from this |
paul@0 | 342 | module or from the whole program. Return a tuple containing the path and |
paul@0 | 343 | any object found. |
paul@0 | 344 | """ |
paul@0 | 345 | |
paul@0 | 346 | if self.objects.has_key(path): |
paul@0 | 347 | return self.objects[path] |
paul@0 | 348 | else: |
paul@0 | 349 | self.ensure_builtins(path) |
paul@0 | 350 | return self.importer.get_object(path) |
paul@0 | 351 | |
paul@0 | 352 | def set_object(self, name, value=None): |
paul@0 | 353 | |
paul@0 | 354 | "Set an object with the given 'name' and the given 'value'." |
paul@0 | 355 | |
paul@0 | 356 | ref = decode_reference(value, name) |
paul@0 | 357 | multiple = self.objects.has_key(name) and self.objects[name].get_kind() != ref.get_kind() |
paul@0 | 358 | self.importer.objects[name] = self.objects[name] = multiple and ref.as_var() or ref |
paul@0 | 359 | |
paul@0 | 360 | # Special names. |
paul@0 | 361 | |
paul@0 | 362 | def get_special(self, name): |
paul@0 | 363 | |
paul@0 | 364 | "Return any stored value for the given special 'name'." |
paul@0 | 365 | |
paul@0 | 366 | return self.special.get(name) |
paul@0 | 367 | |
paul@0 | 368 | def set_special(self, name, value): |
paul@0 | 369 | |
paul@0 | 370 | """ |
paul@0 | 371 | Set a special 'name' that merely tracks the use of an implicit object |
paul@0 | 372 | 'value'. |
paul@0 | 373 | """ |
paul@0 | 374 | |
paul@0 | 375 | self.special[name] = value |
paul@0 | 376 | |
paul@0 | 377 | def set_special_literal(self, name, ref): |
paul@0 | 378 | |
paul@0 | 379 | """ |
paul@0 | 380 | Set a special name for the literal type 'name' having type 'ref'. Such |
paul@0 | 381 | special names provide a way of referring to literal object types. |
paul@0 | 382 | """ |
paul@0 | 383 | |
paul@0 | 384 | literal_name = "$L%s" % name |
paul@0 | 385 | value = ResolvedNameRef(literal_name, ref) |
paul@0 | 386 | self.set_special(literal_name, value) |
paul@0 | 387 | |
paul@0 | 388 | # Revealing modules by tracking name imports across modules. |
paul@0 | 389 | |
paul@0 | 390 | def set_imported_name(self, name, module_name, alias=None, path=None): |
paul@0 | 391 | |
paul@0 | 392 | """ |
paul@0 | 393 | Record 'name' as being imported from the given 'module_name', employing |
paul@0 | 394 | the given 'alias' in the local namespace if specified. |
paul@0 | 395 | """ |
paul@0 | 396 | |
paul@0 | 397 | path = path or self.get_namespace_path() |
paul@0 | 398 | init_item(self.imported_names, path, dict) |
paul@0 | 399 | self.imported_names[path][alias or name] = (name, module_name) |
paul@0 | 400 | |
paul@0 | 401 | def get_imported_name(self, name, path): |
paul@0 | 402 | |
paul@0 | 403 | "Get details of any imported 'name' within the namespace at 'path'." |
paul@0 | 404 | |
paul@0 | 405 | if self.imported_names.has_key(path): |
paul@0 | 406 | return self.imported_names[path].get(name) |
paul@0 | 407 | else: |
paul@0 | 408 | return None |
paul@0 | 409 | |
paul@0 | 410 | def find_imported_name(self, name, path, module=None): |
paul@0 | 411 | |
paul@0 | 412 | """ |
paul@0 | 413 | Find details of the imported 'name' within the namespace at 'path', |
paul@0 | 414 | starting within the given 'module' if indicated, or within this module |
paul@0 | 415 | otherwise. |
paul@0 | 416 | """ |
paul@0 | 417 | |
paul@0 | 418 | module = module or self |
paul@0 | 419 | |
paul@0 | 420 | # Obtain any module required by the name. |
paul@0 | 421 | |
paul@0 | 422 | name_modname = module.get_imported_name(name, path) |
paul@0 | 423 | |
paul@0 | 424 | if name_modname: |
paul@0 | 425 | name, modname = name_modname |
paul@0 | 426 | module = self._find_imported_name(name, modname, module) |
paul@0 | 427 | |
paul@0 | 428 | # Obtain the name from the final module, revealing it if appropriate. |
paul@0 | 429 | |
paul@0 | 430 | if module: |
paul@0 | 431 | init_item(self.importer.revealing, module.name, set) |
paul@0 | 432 | self.importer.set_revealing(module, name, self) |
paul@0 | 433 | |
paul@0 | 434 | def _find_imported_name(self, name, modname, module): |
paul@0 | 435 | |
paul@0 | 436 | """ |
paul@0 | 437 | Traverse details for 'name' via 'modname' to the module providing the |
paul@0 | 438 | name, tentatively revealing the module even if the module is not yet |
paul@0 | 439 | loaded and cannot provide the details of the object recorded for the |
paul@0 | 440 | name. |
paul@0 | 441 | """ |
paul@0 | 442 | |
paul@0 | 443 | _name = name |
paul@0 | 444 | |
paul@0 | 445 | # Obtain any modules referenced by each required module. |
paul@0 | 446 | |
paul@0 | 447 | while True: |
paul@0 | 448 | |
paul@0 | 449 | # Get the module directly or traverse possibly-aliased names. |
paul@0 | 450 | |
paul@0 | 451 | module = self.get_module_direct(modname, True) |
paul@0 | 452 | if not module: |
paul@0 | 453 | top, module = self.get_module(modname, True) |
paul@0 | 454 | name_modname = module.get_imported_name(_name, module.name) |
paul@0 | 455 | if not name_modname: |
paul@0 | 456 | break |
paul@0 | 457 | else: |
paul@0 | 458 | _name, modname = name_modname |
paul@0 | 459 | |
paul@0 | 460 | return module |
paul@0 | 461 | |
paul@0 | 462 | def reveal_referenced(self): |
paul@0 | 463 | |
paul@0 | 464 | """ |
paul@0 | 465 | Reveal modules referenced by this module. |
paul@0 | 466 | """ |
paul@0 | 467 | |
paul@0 | 468 | for path, names in self.imported_names.items(): |
paul@0 | 469 | for alias, (name, modname) in names.items(): |
paul@0 | 470 | module = self._find_imported_name(name, modname, self) |
paul@0 | 471 | self.reveal_module(module) |
paul@0 | 472 | |
paul@0 | 473 | def reveal_module(self, module): |
paul@0 | 474 | |
paul@0 | 475 | """ |
paul@0 | 476 | Reveal the given 'module', recording the revealed modules on this |
paul@0 | 477 | module. |
paul@0 | 478 | """ |
paul@0 | 479 | |
paul@0 | 480 | if module is not self: |
paul@0 | 481 | self.importer.reveal_module(module) |
paul@0 | 482 | self.revealed.add(module) |
paul@0 | 483 | module.accessing_modules.add(self.name) |
paul@0 | 484 | |
paul@0 | 485 | # Module loading. |
paul@0 | 486 | |
paul@0 | 487 | def get_module_direct(self, modname, hidden=False): |
paul@0 | 488 | |
paul@0 | 489 | """ |
paul@0 | 490 | Return 'modname' without traversing parent modules, keeping the module |
paul@0 | 491 | 'hidden' if set to a true value, loading the module if not already |
paul@0 | 492 | loaded. |
paul@0 | 493 | """ |
paul@0 | 494 | |
paul@0 | 495 | return self.importer.get_module(modname, True) or self.importer.load(modname, hidden=hidden) |
paul@0 | 496 | |
paul@0 | 497 | def get_module(self, name, hidden=False): |
paul@0 | 498 | |
paul@0 | 499 | """ |
paul@0 | 500 | Use the given 'name' to obtain the identity of a module. Return module |
paul@0 | 501 | objects or None if the module cannot be found. This method is required |
paul@0 | 502 | when aliases are used to refer to modules and where a module "path" does |
paul@0 | 503 | not correspond to the actual module path. |
paul@0 | 504 | |
paul@0 | 505 | A tuple is returned containing the top or base module and the deepest or |
paul@0 | 506 | leaf module involved. |
paul@0 | 507 | """ |
paul@0 | 508 | |
paul@0 | 509 | path_so_far = [] |
paul@0 | 510 | top = module = None |
paul@0 | 511 | parts = name.split(".") |
paul@0 | 512 | |
paul@0 | 513 | for i, part in enumerate(parts): |
paul@0 | 514 | path_so_far.append(part) |
paul@0 | 515 | module_name = ".".join(path_so_far) |
paul@0 | 516 | ref = self.get_object(module_name) |
paul@0 | 517 | |
paul@0 | 518 | # If no known object exists, attempt to load it. |
paul@0 | 519 | |
paul@0 | 520 | if not ref: |
paul@0 | 521 | module = self.importer.load(module_name, True, hidden) |
paul@0 | 522 | if not module: |
paul@0 | 523 | return None |
paul@0 | 524 | |
paul@0 | 525 | # Rewrite the path to use the actual module details. |
paul@0 | 526 | |
paul@0 | 527 | path_so_far = module.name.split(".") |
paul@0 | 528 | |
paul@0 | 529 | # If the object exists and is not a module, stop. |
paul@0 | 530 | |
paul@0 | 531 | elif ref.has_kind(["<class>", "<function>", "<var>"]): |
paul@0 | 532 | return None |
paul@0 | 533 | |
paul@0 | 534 | else: |
paul@0 | 535 | module = self.importer.get_module(ref.get_origin(), hidden) |
paul@0 | 536 | |
paul@0 | 537 | if not top: |
paul@0 | 538 | top = module |
paul@0 | 539 | |
paul@0 | 540 | return top, module |
paul@0 | 541 | |
paul@0 | 542 | class CachedModule(BasicModule): |
paul@0 | 543 | |
paul@0 | 544 | "A cached module." |
paul@0 | 545 | |
paul@0 | 546 | def __repr__(self): |
paul@0 | 547 | return "CachedModule(%r, %r)" % (self.name, self.importer) |
paul@0 | 548 | |
paul@0 | 549 | def resolve(self): |
paul@0 | 550 | pass |
paul@0 | 551 | |
paul@0 | 552 | def to_cache(self, filename): |
paul@0 | 553 | |
paul@0 | 554 | "Not actually writing the module back to 'filename'." |
paul@0 | 555 | |
paul@0 | 556 | pass |
paul@0 | 557 | |
paul@0 | 558 | def from_cache(self, filename): |
paul@0 | 559 | |
paul@0 | 560 | """ |
paul@0 | 561 | Read a module's details from the file with the given 'filename' as |
paul@0 | 562 | described in the to_cache method of InspectedModule. |
paul@0 | 563 | """ |
paul@0 | 564 | |
paul@0 | 565 | f = open(filename) |
paul@0 | 566 | try: |
paul@0 | 567 | self.filename = f.readline().rstrip() |
paul@0 | 568 | |
paul@0 | 569 | accessing_modules = f.readline().split(": ", 1)[-1].rstrip() |
paul@0 | 570 | |
paul@0 | 571 | module_names = f.readline().split(": ", 1)[-1].rstrip() |
paul@0 | 572 | if module_names: |
paul@0 | 573 | for module_name in module_names.split(", "): |
paul@0 | 574 | self.imported.add(self.importer.load(module_name)) |
paul@0 | 575 | |
paul@0 | 576 | module_names = f.readline().split(": ", 1)[-1].rstrip() |
paul@0 | 577 | if module_names: |
paul@0 | 578 | for module_name in module_names.split(", "): |
paul@0 | 579 | self.imported_hidden.add(self.importer.load(module_name, hidden=True)) |
paul@0 | 580 | |
paul@0 | 581 | module_names = f.readline().split(": ", 1)[-1].rstrip() |
paul@0 | 582 | if module_names: |
paul@0 | 583 | for module_name in module_names.split(", "): |
paul@0 | 584 | module = self.importer.load(module_name, True, True) |
paul@0 | 585 | self.reveal_module(module) |
paul@0 | 586 | |
paul@0 | 587 | f.readline() # (empty line) |
paul@0 | 588 | |
paul@0 | 589 | self._get_imported_names(f) |
paul@0 | 590 | self._get_members(f) |
paul@0 | 591 | self._get_class_relationships(f) |
paul@0 | 592 | self._get_instance_attrs(f) |
paul@0 | 593 | self._get_instance_attr_constants(f) |
paul@0 | 594 | self.from_lines(f, self.names_used) # "names used:" |
paul@0 | 595 | self.from_lines(f, self.names_missing) # "names missing:" |
paul@0 | 596 | self._get_name_references(f) |
paul@0 | 597 | self._get_initialised_names(f) |
paul@0 | 598 | self._get_aliased_names(f) |
paul@0 | 599 | self._get_function_parameters(f) |
paul@0 | 600 | self._get_function_defaults(f) |
paul@0 | 601 | self._get_function_locals(f) |
paul@0 | 602 | self.from_lines(f, self.scope_globals) # "scope globals:" |
paul@0 | 603 | self._get_function_targets(f) |
paul@0 | 604 | self._get_function_arguments(f) |
paul@0 | 605 | self._get_attribute_usage(f) |
paul@0 | 606 | self._get_attr_accesses(f) |
paul@0 | 607 | self._get_const_accesses(f) |
paul@0 | 608 | self._get_attr_accessors(f) |
paul@0 | 609 | self._get_attr_access_modifiers(f) |
paul@0 | 610 | self._get_constant_literals(f) |
paul@0 | 611 | self._get_constant_values(f) |
paul@0 | 612 | |
paul@0 | 613 | finally: |
paul@0 | 614 | f.close() |
paul@0 | 615 | |
paul@0 | 616 | self.loaded = True |
paul@0 | 617 | |
paul@0 | 618 | def resolve(self): |
paul@0 | 619 | self.propagate() |
paul@0 | 620 | |
paul@0 | 621 | def _get_imported_names(self, f): |
paul@0 | 622 | f.readline() # "imported names:" |
paul@0 | 623 | line = f.readline().rstrip() |
paul@0 | 624 | while line: |
paul@0 | 625 | path, alias_or_name, name, module_name = self._get_fields(line, 4) |
paul@0 | 626 | init_item(self.imported_names, path, dict) |
paul@0 | 627 | self.imported_names[path][alias_or_name] = (name, module_name) |
paul@0 | 628 | line = f.readline().rstrip() |
paul@0 | 629 | |
paul@0 | 630 | def _get_members(self, f): |
paul@0 | 631 | f.readline() # "members:" |
paul@0 | 632 | line = f.readline().rstrip() |
paul@0 | 633 | while line: |
paul@0 | 634 | name, ref = line.split(" ", 1) |
paul@0 | 635 | self.set_object(name, ref) |
paul@0 | 636 | line = f.readline().rstrip() |
paul@0 | 637 | |
paul@0 | 638 | def _get_class_relationships(self, f): |
paul@0 | 639 | f.readline() # "class relationships:" |
paul@0 | 640 | line = f.readline().rstrip() |
paul@0 | 641 | while line: |
paul@0 | 642 | name, value = self._get_fields(line) |
paul@0 | 643 | values = value and value.split(", ") or [] |
paul@0 | 644 | self.importer.classes[name] = self.classes[name] = map(decode_reference, values) |
paul@0 | 645 | self.importer.subclasses[name] = set() |
paul@0 | 646 | line = f.readline().rstrip() |
paul@0 | 647 | |
paul@0 | 648 | def _get_instance_attrs(self, f): |
paul@0 | 649 | f.readline() # "instance attributes:" |
paul@0 | 650 | line = f.readline().rstrip() |
paul@0 | 651 | while line: |
paul@0 | 652 | name, value = self._get_fields(line) |
paul@0 | 653 | self.importer.all_instance_attrs[name] = self.instance_attrs[name] = set(value and value.split(", ") or []) |
paul@0 | 654 | line = f.readline().rstrip() |
paul@0 | 655 | |
paul@0 | 656 | def _get_instance_attr_constants(self, f): |
paul@0 | 657 | f.readline() # "instance attribute constants:" |
paul@0 | 658 | line = f.readline().rstrip() |
paul@0 | 659 | while line: |
paul@0 | 660 | name, attrname, ref = self._get_fields(line, 3) |
paul@0 | 661 | init_item(self.instance_attr_constants, name, dict) |
paul@0 | 662 | self.instance_attr_constants[name][attrname] = decode_reference(ref) |
paul@0 | 663 | line = f.readline().rstrip() |
paul@0 | 664 | |
paul@0 | 665 | def _get_name_references(self, f): |
paul@0 | 666 | f.readline() # "name references:" |
paul@0 | 667 | line = f.readline().rstrip() |
paul@0 | 668 | while line: |
paul@0 | 669 | name, value = self._get_fields(line) |
paul@0 | 670 | self.name_references[name] = value |
paul@0 | 671 | line = f.readline().rstrip() |
paul@0 | 672 | |
paul@0 | 673 | def _get_initialised_names(self, f): |
paul@0 | 674 | f.readline() # "initialised names:" |
paul@0 | 675 | line = f.readline().rstrip() |
paul@0 | 676 | while line: |
paul@0 | 677 | name, version, value = self._get_fields(line, 3) |
paul@0 | 678 | init_item(self.initialised_names, name, dict) |
paul@0 | 679 | self.initialised_names[name][int(version)] = decode_reference(value) |
paul@0 | 680 | line = f.readline().rstrip() |
paul@0 | 681 | |
paul@0 | 682 | def _get_aliased_names(self, f): |
paul@0 | 683 | f.readline() # "aliased names:" |
paul@0 | 684 | line = f.readline().rstrip() |
paul@0 | 685 | while line: |
paul@0 | 686 | name, version, original_name, attrnames, number = self._get_fields(line, 5) |
paul@0 | 687 | init_item(self.aliased_names, name, dict) |
paul@0 | 688 | if number == "{}": number = None |
paul@0 | 689 | else: number = int(number) |
paul@0 | 690 | self.aliased_names[name][int(version)] = (original_name, attrnames != "{}" and attrnames or None, number) |
paul@0 | 691 | line = f.readline().rstrip() |
paul@0 | 692 | |
paul@0 | 693 | def _get_function_parameters(self, f): |
paul@0 | 694 | f.readline() # "function parameters:" |
paul@0 | 695 | line = f.readline().rstrip() |
paul@0 | 696 | while line: |
paul@0 | 697 | function, names = self._get_fields(line) |
paul@0 | 698 | self.importer.function_parameters[function] = \ |
paul@0 | 699 | self.function_parameters[function] = names and names.split(", ") or [] |
paul@0 | 700 | line = f.readline().rstrip() |
paul@0 | 701 | |
paul@0 | 702 | def _get_function_defaults(self, f): |
paul@0 | 703 | f.readline() # "function default parameters:" |
paul@0 | 704 | line = f.readline().rstrip() |
paul@0 | 705 | while line: |
paul@0 | 706 | function, defaults = self._get_fields(line) |
paul@0 | 707 | self.importer.function_defaults[function] = \ |
paul@0 | 708 | self.function_defaults[function] = l = [] |
paul@0 | 709 | if defaults != "{}": |
paul@0 | 710 | for value in defaults.split(", "): |
paul@0 | 711 | name, default = value.split("=") |
paul@0 | 712 | default = decode_reference(default) |
paul@0 | 713 | l.append((name, default)) |
paul@0 | 714 | line = f.readline().rstrip() |
paul@0 | 715 | |
paul@0 | 716 | def _get_function_locals(self, f): |
paul@0 | 717 | f.readline() # "function locals:" |
paul@0 | 718 | line = f.readline().rstrip() |
paul@0 | 719 | while line: |
paul@0 | 720 | function, name, value = self._get_fields(line, 3) |
paul@0 | 721 | init_item(self.function_locals, function, dict) |
paul@0 | 722 | if name != "{}": |
paul@0 | 723 | self.function_locals[function][name] = decode_reference(value) |
paul@0 | 724 | line = f.readline().rstrip() |
paul@0 | 725 | |
paul@0 | 726 | def _get_function_targets(self, f): |
paul@0 | 727 | f.readline() # "function targets:" |
paul@0 | 728 | line = f.readline().rstrip() |
paul@0 | 729 | while line: |
paul@0 | 730 | function, n = self._get_fields(line) |
paul@0 | 731 | self.importer.function_targets[function] = \ |
paul@0 | 732 | self.function_targets[function] = int(n) |
paul@0 | 733 | line = f.readline().rstrip() |
paul@0 | 734 | |
paul@0 | 735 | def _get_function_arguments(self, f): |
paul@0 | 736 | f.readline() # "function arguments:" |
paul@0 | 737 | line = f.readline().rstrip() |
paul@0 | 738 | while line: |
paul@0 | 739 | function, n = self._get_fields(line) |
paul@0 | 740 | self.importer.function_arguments[function] = \ |
paul@0 | 741 | self.function_arguments[function] = int(n) |
paul@0 | 742 | line = f.readline().rstrip() |
paul@0 | 743 | |
paul@0 | 744 | def _get_attribute_usage(self, f): |
paul@0 | 745 | f.readline() # "attribute usage:" |
paul@0 | 746 | line = f.readline().rstrip() |
paul@0 | 747 | while line: |
paul@0 | 748 | unit, value = self._get_fields(line) |
paul@0 | 749 | init_item(self.attr_usage, unit, dict) |
paul@0 | 750 | self.usage_from_cache(value, self.attr_usage[unit]) |
paul@0 | 751 | line = f.readline().rstrip() |
paul@0 | 752 | |
paul@0 | 753 | def _get_attr_accesses(self, f): |
paul@0 | 754 | f.readline() # "attribute accesses:" |
paul@0 | 755 | line = f.readline().rstrip() |
paul@0 | 756 | while line: |
paul@0 | 757 | name, value = self._get_fields(line) |
paul@0 | 758 | self.attr_accesses[name] = set(value.split(", ")) |
paul@0 | 759 | line = f.readline().rstrip() |
paul@0 | 760 | |
paul@0 | 761 | def _get_const_accesses(self, f): |
paul@0 | 762 | f.readline() # "constant accesses:" |
paul@0 | 763 | line = f.readline().rstrip() |
paul@0 | 764 | while line: |
paul@0 | 765 | name, original_name, attrnames, objpath, ref, remaining = self._get_fields(line, 6) |
paul@0 | 766 | if attrnames == "{}": attrnames = None |
paul@0 | 767 | init_item(self.const_accesses, name, dict) |
paul@0 | 768 | self.const_accesses[name][(original_name, attrnames)] = (objpath, decode_reference(ref), remaining != "{}" and remaining or "") |
paul@0 | 769 | line = f.readline().rstrip() |
paul@0 | 770 | |
paul@0 | 771 | def _get_attr_accessors(self, f): |
paul@0 | 772 | f.readline() # "attribute access usage:" |
paul@0 | 773 | line = f.readline().rstrip() |
paul@0 | 774 | while line: |
paul@0 | 775 | objpath, name, attrname, value = self._get_fields(line, 4) |
paul@0 | 776 | if attrname == "{}": attrname = None |
paul@0 | 777 | access = name, attrname |
paul@0 | 778 | init_item(self.attr_accessors, objpath, dict) |
paul@0 | 779 | init_item(self.attr_accessors[objpath], access, list) |
paul@0 | 780 | positions = map(int, value.split(", ")) |
paul@0 | 781 | self.attr_accessors[objpath][access].append(positions) |
paul@0 | 782 | line = f.readline().rstrip() |
paul@0 | 783 | |
paul@0 | 784 | def _get_attr_access_modifiers(self, f): |
paul@0 | 785 | f.readline() # "attribute access modifiers:" |
paul@0 | 786 | line = f.readline().rstrip() |
paul@0 | 787 | while line: |
paul@0 | 788 | objpath, name, attrnames, value = self._get_fields(line, 4) |
paul@0 | 789 | if name == "{}": name = None |
paul@0 | 790 | if attrnames == "{}": attrnames = None |
paul@0 | 791 | access = name, attrnames |
paul@0 | 792 | init_item(self.attr_access_modifiers, objpath, dict) |
paul@0 | 793 | init_item(self.attr_access_modifiers[objpath], access, list) |
paul@0 | 794 | modifiers = [decode_modifier_term(s) for s in value] |
paul@0 | 795 | self.attr_access_modifiers[objpath][access] = modifiers |
paul@0 | 796 | line = f.readline().rstrip() |
paul@0 | 797 | |
paul@0 | 798 | def _get_constant_literals(self, f): |
paul@0 | 799 | f.readline() # "constant literals:" |
paul@0 | 800 | line = f.readline().rstrip() |
paul@0 | 801 | last_path = None |
paul@0 | 802 | n = None |
paul@0 | 803 | while line: |
paul@0 | 804 | path, constant = self._get_fields(line) |
paul@0 | 805 | if path != last_path: |
paul@0 | 806 | n = 0 |
paul@0 | 807 | last_path = path |
paul@0 | 808 | else: |
paul@0 | 809 | n += 1 |
paul@0 | 810 | init_item(self.constants, path, dict) |
paul@0 | 811 | self.constants[path][eval(constant)] = n |
paul@0 | 812 | line = f.readline().rstrip() |
paul@0 | 813 | |
paul@0 | 814 | def _get_constant_values(self, f): |
paul@0 | 815 | f.readline() # "constant values:" |
paul@0 | 816 | line = f.readline().rstrip() |
paul@0 | 817 | while line: |
paul@0 | 818 | name, value_type, value = self._get_fields(line, 3) |
paul@0 | 819 | self.constant_values[name] = eval(value), value_type |
paul@0 | 820 | line = f.readline().rstrip() |
paul@0 | 821 | |
paul@0 | 822 | # Generic parsing methods. |
paul@0 | 823 | |
paul@0 | 824 | def from_lines(self, f, d): |
paul@0 | 825 | |
paul@0 | 826 | "Read lines from 'f', populating 'd'." |
paul@0 | 827 | |
paul@0 | 828 | f.readline() # section heading |
paul@0 | 829 | line = f.readline().rstrip() |
paul@0 | 830 | while line: |
paul@0 | 831 | name, value = self._get_fields(line) |
paul@0 | 832 | d[name] = set(value and value.split(", ") or []) |
paul@0 | 833 | line = f.readline().rstrip() |
paul@0 | 834 | |
paul@0 | 835 | def usage_from_cache(self, value, mapping): |
paul@0 | 836 | |
paul@0 | 837 | """ |
paul@0 | 838 | Interpret the given 'value' containing name and usage information, |
paul@0 | 839 | storing the information in the given 'mapping'. |
paul@0 | 840 | """ |
paul@0 | 841 | |
paul@0 | 842 | local, usage = self._get_fields(value) |
paul@0 | 843 | init_item(mapping, local, list) |
paul@0 | 844 | self._usage_from_cache(mapping[local], usage) |
paul@0 | 845 | |
paul@0 | 846 | def _usage_from_cache(self, d, usage): |
paul@0 | 847 | |
paul@0 | 848 | # Interpret descriptions of each version of the name. |
paul@0 | 849 | |
paul@0 | 850 | all_usages = set() |
paul@0 | 851 | for attrnames in usage.split("; "): |
paul@0 | 852 | if attrnames == "{}": |
paul@0 | 853 | all_attrnames = () |
paul@0 | 854 | else: |
paul@0 | 855 | # Decode attribute details for each usage description. |
paul@0 | 856 | |
paul@0 | 857 | all_attrnames = set() |
paul@0 | 858 | for attrname_str in attrnames.split(", "): |
paul@0 | 859 | all_attrnames.add(attrname_str) |
paul@0 | 860 | |
paul@0 | 861 | all_attrnames = list(all_attrnames) |
paul@0 | 862 | all_attrnames.sort() |
paul@0 | 863 | |
paul@0 | 864 | all_usages.add(tuple(all_attrnames)) |
paul@0 | 865 | |
paul@0 | 866 | d.append(all_usages) |
paul@0 | 867 | |
paul@0 | 868 | def _get_fields(self, s, n=2): |
paul@0 | 869 | result = s.split(" ", n-1) |
paul@0 | 870 | if len(result) == n: |
paul@0 | 871 | return result |
paul@0 | 872 | else: |
paul@0 | 873 | return tuple(result) + tuple([""] * (n - len(result))) |
paul@0 | 874 | |
paul@0 | 875 | class CacheWritingModule: |
paul@0 | 876 | |
paul@0 | 877 | """ |
paul@0 | 878 | A mix-in providing cache-writing support, to be combined with BasicModule. |
paul@0 | 879 | """ |
paul@0 | 880 | |
paul@0 | 881 | def to_cache(self, filename): |
paul@0 | 882 | |
paul@0 | 883 | """ |
paul@0 | 884 | Write a cached representation of the inspected module with the following |
paul@0 | 885 | format to the file having the given 'filename': |
paul@0 | 886 | |
paul@0 | 887 | filename |
paul@0 | 888 | "accessed by:" accessing module names during this module's import |
paul@0 | 889 | "imports:" imported module names |
paul@0 | 890 | "hidden imports:" imported module names |
paul@0 | 891 | "reveals:" revealed module names |
paul@0 | 892 | (empty line) |
paul@0 | 893 | "imported names:" |
paul@0 | 894 | zero or more: qualified name " " name " " module name |
paul@0 | 895 | (empty line) |
paul@0 | 896 | "members:" |
paul@0 | 897 | zero or more: qualified name " " reference |
paul@0 | 898 | (empty line) |
paul@0 | 899 | "class relationships:" |
paul@0 | 900 | zero or more: qualified class name " " base class references |
paul@0 | 901 | (empty line) |
paul@0 | 902 | "instance attributes:" |
paul@0 | 903 | zero or more: qualified class name " " instance attribute names |
paul@0 | 904 | (empty line) |
paul@0 | 905 | "instance attribute constants:" |
paul@0 | 906 | zero or more: qualified class name " " attribute name " " reference |
paul@0 | 907 | (empty line) |
paul@0 | 908 | "names used:" |
paul@0 | 909 | zero or more: qualified class/function/module name " " names |
paul@0 | 910 | (empty line) |
paul@0 | 911 | "names missing:" |
paul@0 | 912 | zero or more: qualified class/function/module name " " names |
paul@0 | 913 | (empty line) |
paul@0 | 914 | "name references:" |
paul@0 | 915 | zero or more: qualified name " " reference |
paul@0 | 916 | (empty line) |
paul@0 | 917 | "initialised names:" |
paul@0 | 918 | zero or more: qualified name " " definition version " " reference |
paul@0 | 919 | (empty line) |
paul@0 | 920 | "aliased names:" |
paul@0 | 921 | zero or more: qualified name " " definition version " " original name " " attribute names " " access number |
paul@0 | 922 | (empty line) |
paul@0 | 923 | "function parameters:" |
paul@0 | 924 | zero or more: qualified function name " " parameter names |
paul@0 | 925 | (empty line) |
paul@0 | 926 | "function default parameters:" |
paul@0 | 927 | zero or more: qualified function name " " parameter names with defaults |
paul@0 | 928 | (empty line) |
paul@0 | 929 | "function locals:" |
paul@0 | 930 | zero or more: qualified function name " " local variable name " " reference |
paul@0 | 931 | (empty line) |
paul@0 | 932 | "scope globals:" |
paul@0 | 933 | zero or more: qualified function name " " global variable names |
paul@0 | 934 | (empty line) |
paul@0 | 935 | "function targets:" |
paul@0 | 936 | zero or more: qualified function name " " maximum number of targets allocated |
paul@0 | 937 | (empty line) |
paul@0 | 938 | "function arguments:" |
paul@0 | 939 | zero or more: qualified function name " " maximum number of arguments allocated |
paul@0 | 940 | (empty line) |
paul@0 | 941 | "attribute usage:" |
paul@0 | 942 | zero or more: qualified scope name " " local/global/qualified variable name " " usages |
paul@0 | 943 | (empty line) |
paul@0 | 944 | "attribute accesses:" |
paul@0 | 945 | zero or more: qualified scope name " " attribute-chains |
paul@0 | 946 | (empty line) |
paul@0 | 947 | "constant accesses:" |
paul@0 | 948 | zero or more: qualified function name " " attribute-chain " " reference " " remaining attribute-chain |
paul@0 | 949 | (empty line) |
paul@0 | 950 | "attribute access usage:" |
paul@0 | 951 | zero or more: qualified function name " " local/global variable name " " attribute name " " definition versions |
paul@0 | 952 | (empty line) |
paul@0 | 953 | "attribute access modifiers:" |
paul@0 | 954 | zero or more: qualified function name " " local/global variable name " " attribute name " " access modifiers |
paul@0 | 955 | "constant literals:" |
paul@0 | 956 | zero or more: qualified scope name " " constant literal |
paul@0 | 957 | "constant values:" |
paul@0 | 958 | zero or more: qualified name " " value type " " constant literal |
paul@0 | 959 | |
paul@0 | 960 | All collections of names are separated by ", " characters. |
paul@0 | 961 | |
paul@0 | 962 | References can be "<var>", a module name, or one of "<class>" or |
paul@0 | 963 | "<function>" followed optionally by a ":" character and a qualified |
paul@0 | 964 | name. |
paul@0 | 965 | |
paul@0 | 966 | Parameter names with defaults are separated by ", " characters, with |
paul@0 | 967 | each name followed by "=" and then followed by a reference. If "{}" is |
paul@0 | 968 | indicated, no defaults are defined for the function. Similarly, function |
paul@0 | 969 | locals may be indicated as "{}" meaning that there are no locals. |
paul@0 | 970 | |
paul@0 | 971 | All usages (attribute usage sets) are separated by "; " characters, with |
paul@0 | 972 | the special string "{}" representing an empty set. |
paul@0 | 973 | |
paul@0 | 974 | Each usage is a collection of names separated by ", " characters, with |
paul@0 | 975 | assigned attribute names suffixed with a "*" character. |
paul@0 | 976 | |
paul@0 | 977 | Each attribute-chain expression is a dot-separated chain of attribute |
paul@0 | 978 | names, with assignments suffixed with a "*" character. |
paul@0 | 979 | |
paul@0 | 980 | Definition versions are separated by ", " characters and indicate the |
paul@0 | 981 | name definition version associated with the access. |
paul@0 | 982 | |
paul@0 | 983 | Access modifiers are separated by ", " characters and indicate features |
paul@0 | 984 | of each access, with multiple accesses described on a single line. |
paul@0 | 985 | """ |
paul@0 | 986 | |
paul@0 | 987 | f = open(filename, "w") |
paul@0 | 988 | try: |
paul@0 | 989 | print >>f, self.filename |
paul@0 | 990 | |
paul@0 | 991 | accessing_modules = list(self.accessing_modules) |
paul@0 | 992 | accessing_modules.sort() |
paul@0 | 993 | print >>f, "accessed by:", ", ".join(accessing_modules) |
paul@0 | 994 | |
paul@0 | 995 | module_names = [m.name for m in self.imported] |
paul@0 | 996 | module_names.sort() |
paul@0 | 997 | print >>f, "imports:", ", ".join(module_names) |
paul@0 | 998 | |
paul@0 | 999 | module_names = [m.name for m in self.imported_hidden] |
paul@0 | 1000 | module_names.sort() |
paul@0 | 1001 | print >>f, "hidden imports:", ", ".join(module_names) |
paul@0 | 1002 | |
paul@0 | 1003 | module_names = [m.name for m in self.revealed] |
paul@0 | 1004 | module_names.sort() |
paul@0 | 1005 | print >>f, "reveals:", ", ".join(module_names) |
paul@0 | 1006 | |
paul@0 | 1007 | print >>f |
paul@0 | 1008 | print >>f, "imported names:" |
paul@0 | 1009 | paths = self.imported_names.keys() |
paul@0 | 1010 | paths.sort() |
paul@0 | 1011 | for path in paths: |
paul@0 | 1012 | names = self.imported_names[path].keys() |
paul@0 | 1013 | names.sort() |
paul@0 | 1014 | for alias_or_name in names: |
paul@0 | 1015 | name, modname = self.imported_names[path][alias_or_name] |
paul@0 | 1016 | print >>f, path, alias_or_name, name, modname |
paul@0 | 1017 | |
paul@0 | 1018 | print >>f |
paul@0 | 1019 | print >>f, "members:" |
paul@0 | 1020 | objects = self.objects.keys() |
paul@0 | 1021 | objects.sort() |
paul@0 | 1022 | for name in objects: |
paul@0 | 1023 | print >>f, name, self.objects[name] |
paul@0 | 1024 | |
paul@0 | 1025 | print >>f |
paul@0 | 1026 | print >>f, "class relationships:" |
paul@0 | 1027 | classes = self.classes.keys() |
paul@0 | 1028 | classes.sort() |
paul@0 | 1029 | for class_ in classes: |
paul@0 | 1030 | bases = self.classes[class_] |
paul@0 | 1031 | if bases: |
paul@0 | 1032 | print >>f, class_, ", ".join(map(str, bases)) |
paul@0 | 1033 | else: |
paul@0 | 1034 | print >>f, class_ |
paul@0 | 1035 | |
paul@0 | 1036 | self.to_lines(f, "instance attributes:", self.instance_attrs) |
paul@0 | 1037 | |
paul@0 | 1038 | print >>f |
paul@0 | 1039 | print >>f, "instance attribute constants:" |
paul@0 | 1040 | classes = self.instance_attr_constants.items() |
paul@0 | 1041 | classes.sort() |
paul@0 | 1042 | for name, attrs in classes: |
paul@0 | 1043 | attrs = attrs.items() |
paul@0 | 1044 | attrs.sort() |
paul@0 | 1045 | for attrname, ref in attrs: |
paul@0 | 1046 | print >>f, name, attrname, ref |
paul@0 | 1047 | |
paul@0 | 1048 | self.to_lines(f, "names used:", self.names_used) |
paul@0 | 1049 | self.to_lines(f, "names missing:", self.names_missing) |
paul@0 | 1050 | |
paul@0 | 1051 | print >>f |
paul@0 | 1052 | print >>f, "name references:" |
paul@0 | 1053 | refs = self.name_references.items() |
paul@0 | 1054 | refs.sort() |
paul@0 | 1055 | for name, ref in refs: |
paul@0 | 1056 | print >>f, name, ref |
paul@0 | 1057 | |
paul@0 | 1058 | print >>f |
paul@0 | 1059 | print >>f, "initialised names:" |
paul@0 | 1060 | assignments = self.initialised_names.items() |
paul@0 | 1061 | assignments.sort() |
paul@0 | 1062 | for name, refs in assignments: |
paul@0 | 1063 | versions = refs.items() |
paul@0 | 1064 | versions.sort() |
paul@0 | 1065 | for version, ref in versions: |
paul@0 | 1066 | print >>f, name, version, ref |
paul@0 | 1067 | |
paul@0 | 1068 | print >>f |
paul@0 | 1069 | print >>f, "aliased names:" |
paul@0 | 1070 | assignments = self.aliased_names.items() |
paul@0 | 1071 | assignments.sort() |
paul@0 | 1072 | for name, aliases in assignments: |
paul@0 | 1073 | versions = aliases.items() |
paul@0 | 1074 | versions.sort() |
paul@0 | 1075 | for version, alias in versions: |
paul@0 | 1076 | original_name, attrnames, number = alias |
paul@0 | 1077 | print >>f, name, version, original_name, attrnames or "{}", number is None and "{}" or number |
paul@0 | 1078 | |
paul@0 | 1079 | print >>f |
paul@0 | 1080 | print >>f, "function parameters:" |
paul@0 | 1081 | functions = self.function_parameters.keys() |
paul@0 | 1082 | functions.sort() |
paul@0 | 1083 | for function in functions: |
paul@0 | 1084 | print >>f, function, ", ".join(self.function_parameters[function]) |
paul@0 | 1085 | |
paul@0 | 1086 | print >>f |
paul@0 | 1087 | print >>f, "function default parameters:" |
paul@0 | 1088 | functions = self.function_defaults.keys() |
paul@0 | 1089 | functions.sort() |
paul@0 | 1090 | for function in functions: |
paul@0 | 1091 | parameters = self.function_defaults[function] |
paul@0 | 1092 | if parameters: |
paul@0 | 1093 | print >>f, function, ", ".join([("%s=%s" % (name, default)) for (name, default) in parameters]) |
paul@0 | 1094 | else: |
paul@0 | 1095 | print >>f, function, "{}" |
paul@0 | 1096 | |
paul@0 | 1097 | print >>f |
paul@0 | 1098 | print >>f, "function locals:" |
paul@0 | 1099 | functions = self.function_locals.keys() |
paul@0 | 1100 | functions.sort() |
paul@0 | 1101 | for function in functions: |
paul@0 | 1102 | names = self.function_locals[function].items() |
paul@0 | 1103 | if names: |
paul@0 | 1104 | names.sort() |
paul@0 | 1105 | for name, value in names: |
paul@0 | 1106 | print >>f, function, name, value |
paul@0 | 1107 | else: |
paul@0 | 1108 | print >>f, function, "{}" |
paul@0 | 1109 | |
paul@0 | 1110 | self.to_lines(f, "scope globals:", self.scope_globals) |
paul@0 | 1111 | |
paul@0 | 1112 | print >>f |
paul@0 | 1113 | print >>f, "function targets:" |
paul@0 | 1114 | functions = self.function_targets.keys() |
paul@0 | 1115 | functions.sort() |
paul@0 | 1116 | for function in functions: |
paul@0 | 1117 | print >>f, function, self.function_targets[function] |
paul@0 | 1118 | |
paul@0 | 1119 | print >>f |
paul@0 | 1120 | print >>f, "function arguments:" |
paul@0 | 1121 | functions = self.function_arguments.keys() |
paul@0 | 1122 | functions.sort() |
paul@0 | 1123 | for function in functions: |
paul@0 | 1124 | print >>f, function, self.function_arguments[function] |
paul@0 | 1125 | |
paul@0 | 1126 | print >>f |
paul@0 | 1127 | print >>f, "attribute usage:" |
paul@0 | 1128 | units = self.attr_usage.keys() |
paul@0 | 1129 | units.sort() |
paul@0 | 1130 | for unit in units: |
paul@0 | 1131 | d = self.attr_usage[unit] |
paul@0 | 1132 | self.usage_to_cache(d, f, unit) |
paul@0 | 1133 | |
paul@0 | 1134 | print >>f |
paul@0 | 1135 | print >>f, "attribute accesses:" |
paul@0 | 1136 | paths = self.attr_accesses.keys() |
paul@0 | 1137 | paths.sort() |
paul@0 | 1138 | for path in paths: |
paul@0 | 1139 | accesses = list(self.attr_accesses[path]) |
paul@0 | 1140 | accesses.sort() |
paul@0 | 1141 | print >>f, path, ", ".join(accesses) |
paul@0 | 1142 | |
paul@0 | 1143 | print >>f |
paul@0 | 1144 | print >>f, "constant accesses:" |
paul@0 | 1145 | paths = self.const_accesses.keys() |
paul@0 | 1146 | paths.sort() |
paul@0 | 1147 | for path in paths: |
paul@0 | 1148 | accesses = self.const_accesses[path].items() |
paul@0 | 1149 | accesses.sort() |
paul@0 | 1150 | for (original_name, attrnames), (objpath, ref, remaining_attrnames) in accesses: |
paul@0 | 1151 | print >>f, path, original_name, attrnames, objpath, ref, remaining_attrnames or "{}" |
paul@0 | 1152 | |
paul@0 | 1153 | print >>f |
paul@0 | 1154 | print >>f, "attribute access usage:" |
paul@0 | 1155 | paths = self.attr_accessors.keys() |
paul@0 | 1156 | paths.sort() |
paul@0 | 1157 | for path in paths: |
paul@0 | 1158 | all_accesses = self.attr_accessors[path].items() |
paul@0 | 1159 | all_accesses.sort() |
paul@0 | 1160 | for (name, attrname), accesses in all_accesses: |
paul@0 | 1161 | for positions in accesses: |
paul@0 | 1162 | positions = map(str, positions) |
paul@0 | 1163 | print >>f, path, name, attrname or "{}", ", ".join(positions) |
paul@0 | 1164 | |
paul@0 | 1165 | print >>f |
paul@0 | 1166 | print >>f, "attribute access modifiers:" |
paul@0 | 1167 | paths = self.attr_access_modifiers.keys() |
paul@0 | 1168 | paths.sort() |
paul@0 | 1169 | for path in paths: |
paul@0 | 1170 | all_accesses = self.attr_access_modifiers[path].items() |
paul@0 | 1171 | all_accesses.sort() |
paul@0 | 1172 | for (name, attrnames), modifiers in all_accesses: |
paul@0 | 1173 | print >>f, path, name or "{}", attrnames or "{}", encode_modifiers(modifiers) |
paul@0 | 1174 | |
paul@0 | 1175 | print >>f |
paul@0 | 1176 | print >>f, "constant literals:" |
paul@0 | 1177 | paths = self.constants.keys() |
paul@0 | 1178 | paths.sort() |
paul@0 | 1179 | for path in paths: |
paul@0 | 1180 | constants = [(v, k) for (k, v) in self.constants[path].items()] |
paul@0 | 1181 | constants.sort() |
paul@0 | 1182 | for n, constant in constants: |
paul@0 | 1183 | print >>f, path, repr(constant) |
paul@0 | 1184 | |
paul@0 | 1185 | print >>f |
paul@0 | 1186 | print >>f, "constant values:" |
paul@0 | 1187 | names = self.constant_values.keys() |
paul@0 | 1188 | names.sort() |
paul@0 | 1189 | for name in names: |
paul@0 | 1190 | value, value_type = self.constant_values[name] |
paul@0 | 1191 | print >>f, name, value_type, repr(value) |
paul@0 | 1192 | |
paul@0 | 1193 | finally: |
paul@0 | 1194 | f.close() |
paul@0 | 1195 | |
paul@0 | 1196 | def to_lines(self, f, heading, d): |
paul@0 | 1197 | |
paul@0 | 1198 | "Write lines to 'f' with the given 'heading', using 'd'." |
paul@0 | 1199 | |
paul@0 | 1200 | print >>f |
paul@0 | 1201 | print >>f, heading |
paul@0 | 1202 | keys = d.keys() |
paul@0 | 1203 | keys.sort() |
paul@0 | 1204 | for key in keys: |
paul@0 | 1205 | attrs = list(d[key]) |
paul@0 | 1206 | if attrs: |
paul@0 | 1207 | attrs.sort() |
paul@0 | 1208 | print >>f, key, ", ".join(attrs) |
paul@0 | 1209 | |
paul@0 | 1210 | def usage_to_cache(self, details, f, prefix): |
paul@0 | 1211 | |
paul@0 | 1212 | "Write the given namespace usage details to the cache." |
paul@0 | 1213 | |
paul@0 | 1214 | names = list(details.keys()) |
paul@0 | 1215 | if names: |
paul@0 | 1216 | names.sort() |
paul@0 | 1217 | for name in names: |
paul@0 | 1218 | if details[name]: |
paul@0 | 1219 | |
paul@0 | 1220 | # Produce descriptions for each version of the name. |
paul@0 | 1221 | |
paul@0 | 1222 | for version in details[name]: |
paul@0 | 1223 | all_usages = [] |
paul@0 | 1224 | for usage in version: |
paul@0 | 1225 | all_usages.append(encode_usage(usage)) |
paul@0 | 1226 | |
paul@0 | 1227 | print >>f, "%s %s %s" % (prefix, name, "; ".join(all_usages)) |
paul@0 | 1228 | |
paul@0 | 1229 | # vim: tabstop=4 expandtab shiftwidth=4 |