1 #!/usr/bin/env python 2 3 """ 4 Import logic. 5 6 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 7 2014, 2015, 2016 Paul Boddie <paul@boddie.org.uk> 8 9 This program is free software; you can redistribute it and/or modify it under 10 the terms of the GNU General Public License as published by the Free Software 11 Foundation; either version 3 of the License, or (at your option) any later 12 version. 13 14 This program is distributed in the hope that it will be useful, but WITHOUT 15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 16 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 17 details. 18 19 You should have received a copy of the GNU General Public License along with 20 this program. If not, see <http://www.gnu.org/licenses/>. 21 """ 22 23 from errors import ProgramError 24 from os.path import exists, extsep, getmtime, join 25 from os import listdir, makedirs, remove 26 from common import init_item, readfile, writefile 27 from modules import CachedModule 28 from referencing import Reference 29 import inspector 30 import sys 31 32 class Importer: 33 34 "An import machine, searching for and loading modules." 35 36 def __init__(self, path, cache=None, verbose=False): 37 38 """ 39 Initialise the importer with the given search 'path' - a list of 40 directories to search for Python modules. 41 42 The optional 'cache' should be the name of a directory used to store 43 cached module information. 44 45 The optional 'verbose' parameter causes output concerning the activities 46 of the object to be produced if set to a true value (not the default). 47 """ 48 49 self.path = path 50 self.cache = cache 51 self.verbose = verbose 52 53 # Module importing queue, required modules, removed modules and active 54 # modules in the final program. 55 56 self.to_import = set() 57 self.required = set(["__main__"]) 58 self.removed = {} 59 self.modules = {} 60 61 # Module relationships and invalidated cached modules. 62 63 self.accessing_modules = {} 64 self.invalidated = set() 65 66 # Object relationships and dependencies. 67 68 self.depends = {} 69 70 # Basic program information. 71 72 self.objects = {} 73 self.classes = {} 74 self.function_parameters = {} 75 self.function_defaults = {} 76 self.function_locals = {} 77 self.function_targets = {} 78 self.function_arguments = {} 79 80 # Unresolved names. 81 82 self.missing = set() 83 84 # Derived information. 85 86 self.subclasses = {} 87 88 # Attributes of different object types. 89 90 self.all_class_attrs = {} 91 self.all_instance_attrs = {} 92 self.all_instance_attr_constants = {} 93 self.all_combined_attrs = {} 94 self.all_module_attrs = {} 95 self.all_shadowed_attrs = {} 96 97 # References to external names and aliases within program units. 98 99 self.all_name_references = {} 100 self.all_initialised_names = {} 101 self.all_aliased_names = {} 102 103 # General attribute accesses. 104 105 self.all_attr_accesses = {} 106 self.all_const_accesses = {} 107 self.all_attr_access_modifiers = {} 108 109 # Constant literals and values. 110 111 self.all_constants = {} 112 self.all_constant_values = {} 113 114 self.make_cache() 115 116 def make_cache(self): 117 if self.cache and not exists(self.cache): 118 makedirs(self.cache) 119 120 def check_cache(self, details): 121 122 """ 123 Check whether the cache applies for the given 'details', invalidating it 124 if it does not. 125 """ 126 127 recorded_details = self.get_cache_details() 128 129 if recorded_details != details: 130 self.remove_cache() 131 132 writefile(self.get_cache_details_filename(), details) 133 134 def get_cache_details_filename(self): 135 136 "Return the filename for the cache details." 137 138 return join(self.cache, "$details") 139 140 def get_cache_details(self): 141 142 "Return details of the cache." 143 144 details_filename = self.get_cache_details_filename() 145 146 if not exists(details_filename): 147 return None 148 else: 149 return readfile(details_filename) 150 151 def remove_cache(self): 152 153 "Remove the contents of the cache." 154 155 for filename in listdir(self.cache): 156 remove(join(self.cache, filename)) 157 158 def to_cache(self): 159 160 "Write modules to the cache." 161 162 if self.cache: 163 for module_name, module in self.modules.items(): 164 module.to_cache(join(self.cache, module_name)) 165 166 # Object retrieval and storage. 167 168 def get_object(self, name): 169 170 """ 171 Return a reference for the given 'name' or None if no such object 172 exists. 173 """ 174 175 return self.objects.get(name) 176 177 def set_object(self, name, value=None): 178 179 "Set the object with the given 'name' and the given 'value'." 180 181 if isinstance(value, Reference): 182 ref = value.alias(name) 183 else: 184 ref = Reference(value, name) 185 186 self.objects[name] = ref 187 188 # Identification of both stored object names and name references. 189 190 def identify(self, name): 191 192 "Identify 'name' using stored object and external name records." 193 194 return self.objects.get(name) or self.all_name_references.get(name) 195 196 # Indirect object retrieval. 197 198 def get_attributes(self, ref, attrname): 199 200 """ 201 Return attributes provided by 'ref' for 'attrname'. Class attributes 202 may be provided by instances. 203 """ 204 205 kind = ref.get_kind() 206 if kind == "<class>": 207 ref = self.get_class_attribute(ref.get_origin(), attrname) 208 return ref and set([ref]) or set() 209 elif kind == "<instance>": 210 return self.get_combined_attributes(ref.get_origin(), attrname) 211 elif kind == "<module>": 212 ref = self.get_module_attribute(ref.get_origin(), attrname) 213 return ref and set([ref]) or set() 214 else: 215 return set() 216 217 def get_class_attribute(self, object_type, attrname): 218 219 "Return from 'object_type' the details of class attribute 'attrname'." 220 221 attrs = self.all_class_attrs.get(object_type) 222 attr = attrs and attrs.get(attrname) 223 return attr and self.get_object(attr) 224 225 def get_instance_attributes(self, object_type, attrname): 226 227 """ 228 Return from 'object_type' the details of instance attribute 'attrname'. 229 """ 230 231 consts = self.all_instance_attr_constants.get(object_type) 232 attrs = set() 233 for attr in self.all_instance_attrs[object_type].get(attrname, []): 234 attrs.add(consts and consts.get(attrname) or Reference("<var>", attr)) 235 return attrs 236 237 def get_combined_attributes(self, object_type, attrname): 238 239 """ 240 Return from 'object_type' the details of class or instance attribute 241 'attrname'. 242 """ 243 244 ref = self.get_class_attribute(object_type, attrname) 245 refs = ref and set([ref]) or set() 246 refs.update(self.get_instance_attributes(object_type, attrname)) 247 return refs 248 249 def get_module_attribute(self, object_type, attrname): 250 251 "Return from 'object_type' the details of module attribute 'attrname'." 252 253 if attrname in self.all_module_attrs[object_type]: 254 return self.get_object("%s.%s" % (object_type, attrname)) 255 else: 256 return None 257 258 # Convenience methods for deducing which kind of object provided an 259 # attribute. 260 261 def get_attribute_provider(self, ref, attrname): 262 263 """ 264 Return the kind of provider of the attribute accessed via 'ref' using 265 'attrname'. 266 """ 267 268 kind = ref.get_kind() 269 270 if kind in ["<class>", "<module>"]: 271 return kind 272 else: 273 return self.get_instance_attribute_provider(ref.get_origin(), attrname) 274 275 def get_instance_attribute_provider(self, object_type, attrname): 276 277 """ 278 Return the kind of provider of the attribute accessed via an instance of 279 'object_type' using 'attrname'. 280 """ 281 282 if self.get_class_attribute(object_type, attrname): 283 return "<class>" 284 else: 285 return "<instance>" 286 287 # Module management. 288 289 def queue_module(self, name, accessor, required=False): 290 291 """ 292 Queue the module with the given 'name' for import from the given 293 'accessor' module. If 'required' is true (it is false by default), the 294 module will be required in the final program. 295 """ 296 297 if not self.modules.has_key(name): 298 self.to_import.add(name) 299 300 if required: 301 self.required.add(name) 302 303 init_item(self.accessing_modules, name, set) 304 self.accessing_modules[name].add(accessor.name) 305 306 def get_modules(self): 307 308 "Return all modules known to the importer." 309 310 return self.modules.values() 311 312 def get_module(self, name): 313 314 "Return the module with the given 'name'." 315 316 if not self.modules.has_key(name): 317 return None 318 319 return self.modules[name] 320 321 # Program operations. 322 323 def initialise(self, filename, reset=False): 324 325 """ 326 Initialise a program whose main module is 'filename', resetting the 327 cache if 'reset' is true. Return the main module. 328 """ 329 330 if reset: 331 self.remove_cache() 332 self.check_cache(filename) 333 334 # Load the program itself. 335 336 m = self.load_from_file(filename) 337 338 # Load any queued modules. 339 340 while self.to_import: 341 for name in list(self.to_import): # avoid mutation issue 342 self.load(name) 343 344 # Resolve dependencies between modules. 345 346 self.resolve() 347 348 # Record the type of all classes. 349 350 self.type_ref = self.get_object("__builtins__.type") 351 352 # Resolve dependencies within the program. 353 354 for module in self.modules.values(): 355 module.complete() 356 357 # Remove unneeded modules. 358 359 all_modules = self.modules.items() 360 361 for name, module in all_modules: 362 if name not in self.required: 363 module.unpropagate() 364 del self.modules[name] 365 self.removed[name] = module 366 367 # Collect redundant objects. 368 369 for module in self.removed.values(): 370 module.collect() 371 372 # Assert module objects where aliases have been removed. 373 374 for name in self.required: 375 if not self.objects.has_key(name): 376 self.objects[name] = Reference("<module>", name) 377 378 return m 379 380 def finalise(self): 381 382 """ 383 Finalise the inspected program, returning whether the program could be 384 finalised. 385 """ 386 387 self.finalise_classes() 388 self.add_init_dependencies() 389 self.to_cache() 390 391 if self.missing: 392 return False 393 394 self.set_class_types() 395 self.define_instantiators() 396 self.collect_constants() 397 398 return True 399 400 # Supporting operations. 401 402 def resolve(self): 403 404 "Resolve dependencies between modules." 405 406 self.waiting = {} 407 408 for module in self.modules.values(): 409 410 # Resolve all deferred references in each module. 411 412 original_deferred = [] 413 414 for ref in module.deferred: 415 416 # Retain original references for caching. 417 418 original_deferred.append(ref.copy()) 419 420 # Update references throughout the program. 421 422 found = self.find_dependency(ref) 423 if not found: 424 self.missing.add((module.name, ref.get_origin())) 425 426 # Record the resolved names and identify required modules. 427 428 else: 429 # Find the providing module of this reference. 430 # Where definitive details of the origin cannot be found, 431 # identify the provider using the deferred reference. 432 # NOTE: This may need to test for static origins. 433 434 provider = self.get_module_provider(found.unresolved() and ref or found) 435 ref.mutate(found) 436 437 # Record any external dependency. 438 439 if provider and provider != module.name: 440 441 # Record the provider dependency. 442 443 module.required.add(provider) 444 self.accessing_modules[provider].add(module.name) 445 446 # Postpone any inclusion of the provider until this 447 # module becomes required. 448 449 if module.name not in self.required: 450 init_item(self.waiting, module.name, set) 451 self.waiting[module.name].add(provider) 452 if self.verbose: 453 print >>sys.stderr, "Noting", provider, "for", ref, "from", module.name 454 455 # Make this module required in the accessing module. 456 457 elif provider not in self.required: 458 self.required.add(provider) 459 if self.verbose: 460 print >>sys.stderr, "Requiring", provider, "for", ref, "from", module.name 461 462 # Record a module ordering dependency. 463 464 if not found.static(): 465 self.add_dependency(module.name, provider) 466 467 # Restore the original references so that they may be read back in 468 # and produce the same results. 469 470 module.deferred = original_deferred 471 472 # Check modules again to see if they are now required and should now 473 # cause the inclusion of other modules providing objects to the program. 474 475 for module_name in self.waiting.keys(): 476 self.require_providers(module_name) 477 478 self.add_special_dependencies() 479 480 def require_providers(self, module_name): 481 482 """ 483 Test if 'module_name' is itself required and, if so, require modules 484 containing objects provided to the module. 485 """ 486 487 if module_name in self.required and self.waiting.has_key(module_name): 488 for provider in self.waiting[module_name]: 489 if provider not in self.required: 490 self.required.add(provider) 491 if self.verbose: 492 print >>sys.stderr, "Requiring", provider 493 self.require_providers(provider) 494 495 def add_special_dependencies(self): 496 497 "Add dependencies due to the use of special names in namespaces." 498 499 for module in self.modules.values(): 500 for ref, paths in module.special.values(): 501 for path in paths: 502 self.add_dependency(path, ref.get_origin()) 503 504 def add_init_dependencies(self): 505 506 "Add dependencies related to object initialisation." 507 508 for name in self.classes.keys(): 509 if self.is_dynamic_class(name): 510 511 # Make subclasses depend on any class with non-static 512 # attributes, plus its module for the initialisation. 513 514 for subclass in self.subclasses[name]: 515 ref = Reference("<class>", subclass) 516 self.add_dependency(subclass, name) 517 self.add_dependency(subclass, self.get_module_provider(ref)) 518 519 # Also make the class dependent on its module for 520 # initialisation. 521 522 ref = Reference("<class>", name) 523 self.add_dependency(name, self.get_module_provider(ref)) 524 525 for name in self.function_defaults.keys(): 526 if self.is_dynamic_callable(name): 527 528 # Make functions with defaults requiring initialisation depend 529 # on the parent scope. 530 531 ref = Reference("<function>", name) 532 self.add_dependency(name, ref.parent()) 533 534 def add_dependency(self, path, origin): 535 536 "Add dependency details for 'path' involving 'origin'." 537 538 if origin: 539 init_item(self.depends, path, set) 540 self.depends[path].add(origin) 541 542 # NOTE: Consolidate this information in a common location. 543 544 special_attributes = ("__args__", "__file__", "__fn__", "__fname__", "__mname__", "__name__") 545 546 def is_dynamic_class(self, name): 547 548 """ 549 Return whether 'name' refers to a class with members that must be 550 initialised dynamically. 551 """ 552 553 attrs = self.all_class_attrs.get(name) 554 555 if not attrs: 556 return False 557 558 for attrname, attr in attrs.items(): 559 if attrname in self.special_attributes: 560 continue 561 if not attr or not self.get_object(attr).static(): 562 return True 563 564 return False 565 566 def is_dynamic_callable(self, name): 567 568 """ 569 Return whether 'name' refers to a callable employing defaults that may 570 need initialising before the callable can be used. 571 """ 572 573 # Find any defaults for the function or method. 574 575 defaults = self.function_defaults.get(name) 576 if not defaults: 577 return False 578 579 # Identify non-constant defaults. 580 581 for name, ref in defaults: 582 if not ref.is_constant_alias(): 583 return True 584 585 return False 586 587 def order_objects(self): 588 589 "Produce an object initialisation ordering." 590 591 # Record the number of modules using or depending on each module. 592 593 usage = {} 594 595 for path in self.depends.keys(): 596 usage[path] = 0 597 598 for path, depends in self.depends.items(): 599 for origin in depends: 600 init_item(usage, origin, lambda: 0) 601 usage[origin] += 1 602 603 # Produce an ordering by obtaining exposed modules (required by modules 604 # already processed) and putting them at the start of the list. 605 606 ordered = [] 607 608 while usage: 609 have_next = False 610 611 for path, n in usage.items(): 612 if n == 0: 613 ordered.insert(0, path) 614 depends = self.depends.get(path) 615 616 # Reduce usage of the referenced objects. 617 618 if depends: 619 for origin in depends: 620 usage[origin] -= 1 621 622 del usage[path] 623 have_next = True 624 625 if not have_next: 626 raise ProgramError("Modules with unresolvable dependencies exist: %s" % ", ".join(usage.keys())) 627 628 ordered.remove("__main__") 629 ordered.append("__main__") 630 return ordered 631 632 def order_modules(self): 633 634 "Produce a module initialisation ordering." 635 636 ordered = self.order_objects() 637 filtered = [] 638 639 for module_name in self.modules.keys(): 640 if module_name not in ordered: 641 filtered.append(module_name) 642 643 for path in ordered: 644 if self.modules.has_key(path): 645 filtered.append(path) 646 647 return filtered 648 649 def find_dependency(self, ref): 650 651 "Find the ultimate dependency for 'ref'." 652 653 found = set() 654 while ref and ref.has_kind("<depends>") and not ref in found: 655 found.add(ref) 656 ref = self.identify(ref.get_origin()) 657 return ref 658 659 def get_module_provider(self, ref): 660 661 "Identify the provider of the given 'ref'." 662 663 for ancestor in ref.ancestors(): 664 if self.modules.has_key(ancestor): 665 return ancestor 666 return None 667 668 def finalise_classes(self): 669 670 "Finalise the class relationships and attributes." 671 672 self.derive_inherited_attrs() 673 self.derive_subclasses() 674 self.derive_shadowed_attrs() 675 676 def derive_inherited_attrs(self): 677 678 "Derive inherited attributes for classes throughout the program." 679 680 for name in self.classes.keys(): 681 self.propagate_attrs_for_class(name) 682 683 def propagate_attrs_for_class(self, name, visited=None): 684 685 "Propagate inherited attributes for class 'name'." 686 687 # Visit classes only once. 688 689 if self.all_combined_attrs.has_key(name): 690 return 691 692 visited = visited or [] 693 694 if name in visited: 695 raise ProgramError, "Class %s may not inherit from itself: %s -> %s." % (name, " -> ".join(visited), name) 696 697 visited.append(name) 698 699 class_attrs = {} 700 instance_attrs = {} 701 702 # Aggregate the attributes from base classes, recording the origins of 703 # applicable attributes. 704 705 for base in self.classes[name][::-1]: 706 707 # Get the identity of the class from the reference. 708 709 base = base.get_origin() 710 711 # Define the base class completely before continuing with this 712 # class. 713 714 self.propagate_attrs_for_class(base, visited) 715 class_attrs.update(self.all_class_attrs[base]) 716 717 # Instance attribute origins are combined if different. 718 719 for key, values in self.all_instance_attrs[base].items(): 720 init_item(instance_attrs, key, set) 721 instance_attrs[key].update(values) 722 723 # Class attributes override those defined earlier in the hierarchy. 724 725 class_attrs.update(self.all_class_attrs.get(name, {})) 726 727 # Instance attributes are merely added if not already defined. 728 729 for key in self.all_instance_attrs.get(name, []): 730 if not instance_attrs.has_key(key): 731 instance_attrs[key] = set(["%s.%s" % (name, key)]) 732 733 self.all_class_attrs[name] = class_attrs 734 self.all_instance_attrs[name] = instance_attrs 735 self.all_combined_attrs[name] = set(class_attrs.keys()).union(instance_attrs.keys()) 736 737 def derive_subclasses(self): 738 739 "Derive subclass details for classes." 740 741 for name, bases in self.classes.items(): 742 for base in bases: 743 744 # Get the identity of the class from the reference. 745 746 base = base.get_origin() 747 self.subclasses[base].add(name) 748 749 def derive_shadowed_attrs(self): 750 751 "Derive shadowed attributes for classes." 752 753 for name, attrs in self.all_instance_attrs.items(): 754 attrs = set(attrs.keys()).intersection(self.all_class_attrs[name].keys()) 755 if attrs: 756 self.all_shadowed_attrs[name] = attrs 757 758 def set_class_types(self): 759 760 "Set the type of each class." 761 762 for attrs in self.all_class_attrs.values(): 763 attrs["__class__"] = self.type_ref.get_origin() 764 765 def define_instantiators(self): 766 767 """ 768 Consolidate parameter and default details, incorporating initialiser 769 details to define instantiator signatures. 770 """ 771 772 for cls, attrs in self.all_class_attrs.items(): 773 initialiser = attrs["__init__"] 774 self.function_parameters[cls] = self.function_parameters[initialiser] 775 self.function_defaults[cls] = self.function_defaults[initialiser] 776 777 def collect_constants(self): 778 779 "Get constants from all active modules." 780 781 for module in self.modules.values(): 782 self.all_constants.update(module.constants) 783 784 # Import methods. 785 786 def find_in_path(self, name): 787 788 """ 789 Find the given module 'name' in the search path, returning None where no 790 such module could be found, or a 2-tuple from the 'find' method 791 otherwise. 792 """ 793 794 for d in self.path: 795 m = self.find(d, name) 796 if m: return m 797 return None 798 799 def find(self, d, name): 800 801 """ 802 In the directory 'd', find the given module 'name', where 'name' can 803 either refer to a single file module or to a package. Return None if the 804 'name' cannot be associated with either a file or a package directory, 805 or a 2-tuple from '_find_package' or '_find_module' otherwise. 806 """ 807 808 m = self._find_package(d, name) 809 if m: return m 810 m = self._find_module(d, name) 811 if m: return m 812 return None 813 814 def _find_module(self, d, name): 815 816 """ 817 In the directory 'd', find the given module 'name', returning None where 818 no suitable file exists in the directory, or a 2-tuple consisting of 819 None (indicating that no package directory is involved) and a filename 820 indicating the location of the module. 821 """ 822 823 name_py = name + extsep + "py" 824 filename = self._find_file(d, name_py) 825 if filename: 826 return None, filename 827 return None 828 829 def _find_package(self, d, name): 830 831 """ 832 In the directory 'd', find the given package 'name', returning None 833 where no suitable package directory exists, or a 2-tuple consisting of 834 a directory (indicating the location of the package directory itself) 835 and a filename indicating the location of the __init__.py module which 836 declares the package's top-level contents. 837 """ 838 839 filename = self._find_file(d, name) 840 if filename: 841 init_py = "__init__" + extsep + "py" 842 init_py_filename = self._find_file(filename, init_py) 843 if init_py_filename: 844 return filename, init_py_filename 845 return None 846 847 def _find_file(self, d, filename): 848 849 """ 850 Return the filename obtained when searching the directory 'd' for the 851 given 'filename', or None if no actual file exists for the filename. 852 """ 853 854 filename = join(d, filename) 855 if exists(filename): 856 return filename 857 else: 858 return None 859 860 def load(self, name): 861 862 """ 863 Load the module or package with the given 'name'. Return an object 864 referencing the loaded module or package, or None if no such module or 865 package exists. 866 """ 867 868 # Loaded modules are returned immediately. 869 # Modules may be known but not yet loading (having been registered as 870 # submodules), loading, loaded, or completely unknown. 871 872 module = self.get_module(name) 873 874 if module: 875 return self.modules[name] 876 877 # Otherwise, modules are loaded. 878 879 # Split the name into path components, and try to find the uppermost in 880 # the search path. 881 882 path = name.split(".") 883 path_so_far = [] 884 module = None 885 886 for p in path: 887 888 # Get the module's filesystem details. 889 890 if not path_so_far: 891 m = self.find_in_path(p) 892 elif d: 893 m = self.find(d, p) 894 else: 895 m = None 896 897 path_so_far.append(p) 898 module_name = ".".join(path_so_far) 899 900 # Return None if the module could not be located. 901 902 if not m: 903 if self.verbose: 904 print >>sys.stderr, "Not found (%s)" % name 905 return None 906 907 # Get the directory and module filename. 908 909 d, filename = m 910 911 # Get the module itself. 912 913 return self.load_from_file(filename, module_name) 914 915 def load_from_file(self, filename, module_name=None): 916 917 "Load the module from the given 'filename'." 918 919 if module_name is None: 920 module_name = "__main__" 921 922 module = self.modules.get(module_name) 923 924 if not module: 925 926 # Try to load from cache. 927 928 module = self.load_from_cache(filename, module_name) 929 if module: 930 return module 931 932 # If no cache entry exists, load from file. 933 934 module = inspector.InspectedModule(module_name, self) 935 self.add_module(module_name, module) 936 self.update_cache_validity(module) 937 938 self._load(module, module_name, lambda m: m.parse, filename) 939 940 return module 941 942 def update_cache_validity(self, module): 943 944 "Make 'module' valid in the cache, but invalidate accessing modules." 945 946 accessing = self.accessing_modules.get(module.name) 947 if accessing: 948 self.invalidated.update(accessing) 949 if module.name in self.invalidated: 950 self.invalidated.remove(module.name) 951 952 def source_is_new(self, filename, module_name): 953 954 "Return whether 'filename' is newer than the cached 'module_name'." 955 956 if self.cache: 957 cache_filename = join(self.cache, module_name) 958 return not exists(cache_filename) or \ 959 getmtime(filename) > getmtime(cache_filename) or \ 960 module_name in self.invalidated 961 else: 962 return True 963 964 def load_from_cache(self, filename, module_name): 965 966 "Return a module residing in the cache." 967 968 module = self.modules.get(module_name) 969 970 if not module and not self.source_is_new(filename, module_name): 971 module = CachedModule(module_name, self) 972 self.add_module(module_name, module) 973 974 filename = join(self.cache, module_name) 975 self._load(module, module_name, lambda m: m.from_cache, filename) 976 977 return module 978 979 def _load(self, module, module_name, fn, filename): 980 981 """ 982 Load 'module' for the given 'module_name', and with 'fn' performing an 983 invocation on the module with the given 'filename'. 984 """ 985 986 # Load the module. 987 988 if self.verbose: 989 print >>sys.stderr, module_name in self.required and "Required" or "Loading", module_name, "from", filename 990 fn(module)(filename) 991 992 # Add the module object if not already defined. 993 994 if not self.objects.has_key(module_name): 995 self.objects[module_name] = Reference("<module>", module_name) 996 997 def add_module(self, module_name, module): 998 999 """ 1000 Return the module with the given 'module_name', adding a new module 1001 object if one does not already exist. 1002 """ 1003 1004 self.modules[module_name] = module 1005 if module_name in self.to_import: 1006 self.to_import.remove(module_name) 1007 1008 # vim: tabstop=4 expandtab shiftwidth=4