| 1 | # (c) 2005 Ian Bicking and contributors; written for Paste (http://pythonpaste.org) |
|---|
| 2 | # Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php |
|---|
| 3 | |
|---|
| 4 | """ |
|---|
| 5 | 'imports' a string -- converts a string to a Python object, importing |
|---|
| 6 | any necessary modules and evaluating the expression. Everything |
|---|
| 7 | before the : in an import expression is the module path; everything |
|---|
| 8 | after is an expression to be evaluated in the namespace of that |
|---|
| 9 | module. |
|---|
| 10 | |
|---|
| 11 | Alternately, if no : is present, then import the modules and get the |
|---|
| 12 | attributes as necessary. Arbitrary expressions are not allowed in |
|---|
| 13 | that case. |
|---|
| 14 | """ |
|---|
| 15 | |
|---|
| 16 | def eval_import(s): |
|---|
| 17 | """ |
|---|
| 18 | Import a module, or import an object from a module. |
|---|
| 19 | |
|---|
| 20 | A module name like ``foo.bar:baz()`` can be used, where |
|---|
| 21 | ``foo.bar`` is the module, and ``baz()`` is an expression |
|---|
| 22 | evaluated in the context of that module. Note this is not safe on |
|---|
| 23 | arbitrary strings because of the eval. |
|---|
| 24 | """ |
|---|
| 25 | if ':' not in s: |
|---|
| 26 | return simple_import(s) |
|---|
| 27 | module_name, expr = s.split(':', 1) |
|---|
| 28 | module = import_module(module_name) |
|---|
| 29 | obj = eval(expr, module.__dict__) |
|---|
| 30 | return obj |
|---|
| 31 | |
|---|
| 32 | def simple_import(s): |
|---|
| 33 | """ |
|---|
| 34 | Import a module, or import an object from a module. |
|---|
| 35 | |
|---|
| 36 | A name like ``foo.bar.baz`` can be a module ``foo.bar.baz`` or a |
|---|
| 37 | module ``foo.bar`` with an object ``baz`` in it, or a module |
|---|
| 38 | ``foo`` with an object ``bar`` with an attribute ``baz``. |
|---|
| 39 | """ |
|---|
| 40 | parts = s.split('.') |
|---|
| 41 | module = import_module(parts[0]) |
|---|
| 42 | name = parts[0] |
|---|
| 43 | parts = parts[1:] |
|---|
| 44 | last_import_error = None |
|---|
| 45 | while parts: |
|---|
| 46 | name += '.' + parts[0] |
|---|
| 47 | try: |
|---|
| 48 | module = import_module(name) |
|---|
| 49 | parts = parts[1:] |
|---|
| 50 | except ImportError, e: |
|---|
| 51 | last_import_error = e |
|---|
| 52 | break |
|---|
| 53 | obj = module |
|---|
| 54 | while parts: |
|---|
| 55 | try: |
|---|
| 56 | obj = getattr(module, parts[0]) |
|---|
| 57 | except AttributeError: |
|---|
| 58 | raise ImportError( |
|---|
| 59 | "Cannot find %s in module %r (stopped importing modules with error %s)" % (parts[0], module, last_import_error)) |
|---|
| 60 | parts = parts[1:] |
|---|
| 61 | return obj |
|---|
| 62 | |
|---|
| 63 | def import_module(s): |
|---|
| 64 | """ |
|---|
| 65 | Import a module. |
|---|
| 66 | """ |
|---|
| 67 | mod = __import__(s) |
|---|
| 68 | parts = s.split('.') |
|---|
| 69 | for part in parts[1:]: |
|---|
| 70 | mod = getattr(mod, part) |
|---|
| 71 | return mod |
|---|
| 72 | |
|---|
| 73 | def try_import_module(module_name): |
|---|
| 74 | """ |
|---|
| 75 | Imports a module, but catches import errors. Only catches errors |
|---|
| 76 | when that module doesn't exist; if that module itself has an |
|---|
| 77 | import error it will still get raised. Returns None if the module |
|---|
| 78 | doesn't exist. |
|---|
| 79 | """ |
|---|
| 80 | try: |
|---|
| 81 | return import_module(module_name) |
|---|
| 82 | except ImportError, e: |
|---|
| 83 | if not getattr(e, 'args', None): |
|---|
| 84 | raise |
|---|
| 85 | desc = e.args[0] |
|---|
| 86 | if not desc.startswith('No module named '): |
|---|
| 87 | raise |
|---|
| 88 | desc = desc[len('No module named '):] |
|---|
| 89 | # If you import foo.bar.baz, the bad import could be any |
|---|
| 90 | # of foo.bar.baz, bar.baz, or baz; we'll test them all: |
|---|
| 91 | parts = module_name.split('.') |
|---|
| 92 | for i in range(len(parts)): |
|---|
| 93 | if desc == '.'.join(parts[i:]): |
|---|
| 94 | return None |
|---|
| 95 | raise |
|---|