Guido van Rossum | 4ece95d | 1997-09-09 20:44:04 +0000 | [diff] [blame] | 1 | """An Python re-implementation of hierarchical module import. |
| 2 | |
| 3 | This code is intended to be read, not executed. However, it does work |
| 4 | -- all you need to do to enable it is "import knee". |
| 5 | |
| 6 | (The name is a pun on the klunkier predecessor of this module, "ni".) |
| 7 | |
| 8 | """ |
Guido van Rossum | 501d0bb | 1997-09-09 20:35:20 +0000 | [diff] [blame] | 9 | |
Eric S. Raymond | db5ebc7 | 2001-02-09 09:48:45 +0000 | [diff] [blame] | 10 | import sys, imp, __builtin__ |
Guido van Rossum | 501d0bb | 1997-09-09 20:35:20 +0000 | [diff] [blame] | 11 | |
| 12 | |
| 13 | # Replacement for __import__() |
| 14 | def import_hook(name, globals=None, locals=None, fromlist=None): |
| 15 | parent = determine_parent(globals) |
| 16 | q, tail = find_head_package(parent, name) |
| 17 | m = load_tail(q, tail) |
| 18 | if not fromlist: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 19 | return q |
Guido van Rossum | 501d0bb | 1997-09-09 20:35:20 +0000 | [diff] [blame] | 20 | if hasattr(m, "__path__"): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 21 | ensure_fromlist(m, fromlist) |
Guido van Rossum | 501d0bb | 1997-09-09 20:35:20 +0000 | [diff] [blame] | 22 | return m |
| 23 | |
| 24 | def determine_parent(globals): |
| 25 | if not globals or not globals.has_key("__name__"): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 26 | return None |
Guido van Rossum | 501d0bb | 1997-09-09 20:35:20 +0000 | [diff] [blame] | 27 | pname = globals['__name__'] |
| 28 | if globals.has_key("__path__"): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 29 | parent = sys.modules[pname] |
| 30 | assert globals is parent.__dict__ |
| 31 | return parent |
Guido van Rossum | 501d0bb | 1997-09-09 20:35:20 +0000 | [diff] [blame] | 32 | if '.' in pname: |
Eric S. Raymond | db5ebc7 | 2001-02-09 09:48:45 +0000 | [diff] [blame] | 33 | i = pname.rfind('.') |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 34 | pname = pname[:i] |
| 35 | parent = sys.modules[pname] |
| 36 | assert parent.__name__ == pname |
| 37 | return parent |
Guido van Rossum | 501d0bb | 1997-09-09 20:35:20 +0000 | [diff] [blame] | 38 | return None |
| 39 | |
| 40 | def find_head_package(parent, name): |
| 41 | if '.' in name: |
Eric S. Raymond | db5ebc7 | 2001-02-09 09:48:45 +0000 | [diff] [blame] | 42 | i = name.find('.') |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 43 | head = name[:i] |
| 44 | tail = name[i+1:] |
Guido van Rossum | 501d0bb | 1997-09-09 20:35:20 +0000 | [diff] [blame] | 45 | else: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 46 | head = name |
| 47 | tail = "" |
Guido van Rossum | 501d0bb | 1997-09-09 20:35:20 +0000 | [diff] [blame] | 48 | if parent: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 49 | qname = "%s.%s" % (parent.__name__, head) |
Guido van Rossum | 501d0bb | 1997-09-09 20:35:20 +0000 | [diff] [blame] | 50 | else: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 51 | qname = head |
Guido van Rossum | 501d0bb | 1997-09-09 20:35:20 +0000 | [diff] [blame] | 52 | q = import_module(head, qname, parent) |
| 53 | if q: return q, tail |
| 54 | if parent: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 55 | qname = head |
| 56 | parent = None |
| 57 | q = import_module(head, qname, parent) |
| 58 | if q: return q, tail |
Guido van Rossum | 501d0bb | 1997-09-09 20:35:20 +0000 | [diff] [blame] | 59 | raise ImportError, "No module named " + qname |
| 60 | |
| 61 | def load_tail(q, tail): |
| 62 | m = q |
| 63 | while tail: |
Eric S. Raymond | db5ebc7 | 2001-02-09 09:48:45 +0000 | [diff] [blame] | 64 | i = tail.find('.') |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 65 | if i < 0: i = len(tail) |
| 66 | head, tail = tail[:i], tail[i+1:] |
| 67 | mname = "%s.%s" % (m.__name__, head) |
| 68 | m = import_module(head, mname, m) |
| 69 | if not m: |
| 70 | raise ImportError, "No module named " + mname |
Guido van Rossum | 501d0bb | 1997-09-09 20:35:20 +0000 | [diff] [blame] | 71 | return m |
| 72 | |
Guido van Rossum | 4f9f8e3 | 1997-09-09 20:39:58 +0000 | [diff] [blame] | 73 | def ensure_fromlist(m, fromlist, recursive=0): |
Guido van Rossum | 501d0bb | 1997-09-09 20:35:20 +0000 | [diff] [blame] | 74 | for sub in fromlist: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 75 | if sub == "*": |
| 76 | if not recursive: |
| 77 | try: |
| 78 | all = m.__all__ |
| 79 | except AttributeError: |
| 80 | pass |
| 81 | else: |
| 82 | ensure_fromlist(m, all, 1) |
| 83 | continue |
| 84 | if sub != "*" and not hasattr(m, sub): |
| 85 | subname = "%s.%s" % (m.__name__, sub) |
| 86 | submod = import_module(sub, subname, m) |
| 87 | if not submod: |
| 88 | raise ImportError, "No module named " + subname |
Guido van Rossum | 501d0bb | 1997-09-09 20:35:20 +0000 | [diff] [blame] | 89 | |
| 90 | def import_module(partname, fqname, parent): |
| 91 | try: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 92 | return sys.modules[fqname] |
Guido van Rossum | 501d0bb | 1997-09-09 20:35:20 +0000 | [diff] [blame] | 93 | except KeyError: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 94 | pass |
Guido van Rossum | 501d0bb | 1997-09-09 20:35:20 +0000 | [diff] [blame] | 95 | try: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 96 | fp, pathname, stuff = imp.find_module(partname, |
| 97 | parent and parent.__path__) |
Guido van Rossum | 501d0bb | 1997-09-09 20:35:20 +0000 | [diff] [blame] | 98 | except ImportError: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 99 | return None |
Guido van Rossum | 501d0bb | 1997-09-09 20:35:20 +0000 | [diff] [blame] | 100 | try: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 101 | m = imp.load_module(fqname, fp, pathname, stuff) |
Guido van Rossum | 501d0bb | 1997-09-09 20:35:20 +0000 | [diff] [blame] | 102 | finally: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 103 | if fp: fp.close() |
Guido van Rossum | 501d0bb | 1997-09-09 20:35:20 +0000 | [diff] [blame] | 104 | if parent: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 105 | setattr(parent, partname, m) |
Guido van Rossum | 501d0bb | 1997-09-09 20:35:20 +0000 | [diff] [blame] | 106 | return m |
| 107 | |
| 108 | |
| 109 | # Replacement for reload() |
| 110 | def reload_hook(module): |
| 111 | name = module.__name__ |
| 112 | if '.' not in name: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 113 | return import_module(name, name, None) |
Eric S. Raymond | db5ebc7 | 2001-02-09 09:48:45 +0000 | [diff] [blame] | 114 | i = name.rfind('.') |
Guido van Rossum | 501d0bb | 1997-09-09 20:35:20 +0000 | [diff] [blame] | 115 | pname = name[:i] |
| 116 | parent = sys.modules[pname] |
| 117 | return import_module(name[i+1:], name, parent) |
| 118 | |
| 119 | |
| 120 | # Save the original hooks |
| 121 | original_import = __builtin__.__import__ |
| 122 | original_reload = __builtin__.reload |
| 123 | |
| 124 | # Now install our hooks |
| 125 | __builtin__.__import__ = import_hook |
| 126 | __builtin__.reload = reload_hook |