1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
""" The default import manager implementation. """
# Enthought library imports.
from enthought.traits.api import HasTraits, implements
# Local imports.
from i_import_manager import IImportManager
class ImportManager(HasTraits):
""" The default import manager implementation.
Its just a guess, but I think using an import manager to do all imports
will make debugging easier (as opposed to just letting imports happen from
all over the place).
"""
implements(IImportManager)
###########################################################################
# 'IImportManager' interface.
###########################################################################
def import_symbol(self, symbol_path):
""" Import the symbol defined by the specified symbol path. """
if ':' in symbol_path:
module_name, symbol_name = symbol_path.split(':')
module = self._import_module(module_name)
symbol = eval(symbol_name, module.__dict__)
else:
components = symbol_path.split('.')
module_name = '.'.join(components[:-1])
symbol_name = components[-1]
module = __import__(
module_name, globals(), locals(), [symbol_name]
)
symbol = getattr(module, symbol_name)
# Event notification.
self.symbol_imported = symbol
return symbol
###########################################################################
# Private interface.
###########################################################################
def _import_module(self, module_name):
""" Import the module with the specified (and possibly dotted) name.
Returns the imported module.
This method is copied from the documentation of the '__import__'
function in the Python Library Reference Manual.
"""
module = __import__(module_name)
components = module_name.split('.')
for component in components[1:]:
module = getattr(module, component)
return module
#### EOF ######################################################################
|