Debian Codegraph @

envisage/import_manager.py

raw
1 # (C) Copyright 2007-2025 Enthought, Inc., Austin, TX
2 # All rights reserved.
3 #
4 # This software is provided without warranty under the terms of the BSD
5 # license included in LICENSE.txt and may be redistributed only under
6 # the conditions described in the aforementioned license. The license
7 # is also available online at http://www.enthought.com/licenses/BSD.txt
8 #
9 # Thanks for using Enthought open source!
10 """ The default import manager implementation. """
11
12 import importlib
13
14 # Enthought library imports.
15 from traits.api import HasTraits, provides
16
17 # Local imports.
18 from .i_import_manager import IImportManager
19
20
21 @provides(IImportManager)
22 class ImportManager(HasTraits):
23 """The default import manager implementation.
24
25 Its just a guess, but I think using an import manager to do all imports
26 will make debugging easier (as opposed to just letting imports happen from
27 all over the place).
28
29 """
30
31 ###########################################################################
32 # 'IImportManager' interface.
33 ###########################################################################
34
35 def import_symbol(self, symbol_path):
36 """Import the symbol defined by the specified symbol path."""
37
38 if ":" in symbol_path:
39 module_name, symbol_name = symbol_path.split(":")
40
41 module = importlib.import_module(module_name)
42 symbol = eval(symbol_name, module.__dict__)
43
44 else:
45 components = symbol_path.split(".")
46
47 module_name = ".".join(components[:-1])
48 symbol_name = components[-1]
49
50 module = __import__(
51 module_name, globals(), locals(), [symbol_name]
52 )
53
54 symbol = getattr(module, symbol_name)
55
56 # Event notification.
57 self.symbol_imported = symbol
58
59 return symbol