1 # Copyright 2013 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 """Module scan and load system.
7 The main interface to this module is the Scan function, which triggers a
8 recursive scan of all packages and modules below cr, with modules being
9 imported as they are found.
10 This allows all the plugins in the system to self register.
11 The aim is to make writing plugins as simple as possible, minimizing the
12 boilerplate so the actual functionality is clearer.
14 from importlib import import_module
20 # This is the name of the variable inserted into modules to track which
21 # scanners have been applied.
22 _MODULE_SCANNED_TAG = '_CR_MODULE_SCANNED'
25 class AutoExport(object):
26 """A marker for classes that should be promoted up into the cr namespace."""
29 def _AutoExportScanner(module):
30 """Scan the modules for things that need wiring up automatically."""
31 for name, value in module.__dict__.items():
32 if isinstance(value, type) and issubclass(value, AutoExport):
33 # Add this straight to the cr module.
34 if not hasattr(cr, name):
35 setattr(cr, name, value)
38 scan_hooks = [_AutoExportScanner]
42 """Import a module or package if it is not already imported."""
43 module = sys.modules.get(name, None)
44 if module is not None:
46 return import_module(name, None)
49 def _ScanModule(module):
50 """Runs all the scan_hooks for a module."""
51 scanner_tags = getattr(module, _MODULE_SCANNED_TAG, None)
52 if scanner_tags is None:
53 # First scan, add the scanned marker set.
55 setattr(module, _MODULE_SCANNED_TAG, scanner_tags)
56 for scan in scan_hooks:
57 if scan not in scanner_tags:
58 scanner_tags.add(scan)
62 def _ScanPackage(package):
63 """Scan a package for child packages and modules."""
65 # Recurse sub folders.
66 for path in package.__path__:
68 basenames = sorted(os.listdir(path))
72 for basename in basenames:
73 fullpath = os.path.join(path, basename)
74 if os.path.isdir(fullpath):
75 name = '.'.join([package.__name__, basename])
77 elif basename.endswith('.py') and not basename.startswith('_'):
78 name = '.'.join([package.__name__, basename[:-3]])
79 module = _Import(name)
81 modules.append(module)
84 modules.extend(_ScanPackage(child))
88 def Import(package, name):
89 module = _Import(package + '.' + name)
90 path = getattr(module, '__path__', None)
99 """Scans from the cr package down, loading modules as needed.
101 This finds all packages and modules below the cr package, by scanning the
102 file system. It imports all the packages, and then runs post import hooks on
103 each module to do any automated work. One example of this is the hook that
104 finds all classes that extend AutoExport and copies them up into the cr
107 Modules are allowed to refer to each other, their import will be retried
108 until it succeeds or no progress can be made on any module.
110 modules = _ScanPackage(cr)
111 # Now scan all the found modules one more time.
112 # This happens after all imports, in case any imports register scan hooks.
113 for module in modules: