Lichen

Annotated importer.py

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