| 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 | """
|
|---|
| 9 |
|
|---|
| 10 | import sys, imp, __builtin__
|
|---|
| 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:
|
|---|
| 19 | return q
|
|---|
| 20 | if hasattr(m, "__path__"):
|
|---|
| 21 | ensure_fromlist(m, fromlist)
|
|---|
| 22 | return m
|
|---|
| 23 |
|
|---|
| 24 | def determine_parent(globals):
|
|---|
| 25 | if not globals or not globals.has_key("__name__"):
|
|---|
| 26 | return None
|
|---|
| 27 | pname = globals['__name__']
|
|---|
| 28 | if globals.has_key("__path__"):
|
|---|
| 29 | parent = sys.modules[pname]
|
|---|
| 30 | assert globals is parent.__dict__
|
|---|
| 31 | return parent
|
|---|
| 32 | if '.' in pname:
|
|---|
| 33 | i = pname.rfind('.')
|
|---|
| 34 | pname = pname[:i]
|
|---|
| 35 | parent = sys.modules[pname]
|
|---|
| 36 | assert parent.__name__ == pname
|
|---|
| 37 | return parent
|
|---|
| 38 | return None
|
|---|
| 39 |
|
|---|
| 40 | def find_head_package(parent, name):
|
|---|
| 41 | if '.' in name:
|
|---|
| 42 | i = name.find('.')
|
|---|
| 43 | head = name[:i]
|
|---|
| 44 | tail = name[i+1:]
|
|---|
| 45 | else:
|
|---|
| 46 | head = name
|
|---|
| 47 | tail = ""
|
|---|
| 48 | if parent:
|
|---|
| 49 | qname = "%s.%s" % (parent.__name__, head)
|
|---|
| 50 | else:
|
|---|
| 51 | qname = head
|
|---|
| 52 | q = import_module(head, qname, parent)
|
|---|
| 53 | if q: return q, tail
|
|---|
| 54 | if parent:
|
|---|
| 55 | qname = head
|
|---|
| 56 | parent = None
|
|---|
| 57 | q = import_module(head, qname, parent)
|
|---|
| 58 | if q: return q, tail
|
|---|
| 59 | raise ImportError, "No module named " + qname
|
|---|
| 60 |
|
|---|
| 61 | def load_tail(q, tail):
|
|---|
| 62 | m = q
|
|---|
| 63 | while tail:
|
|---|
| 64 | i = tail.find('.')
|
|---|
| 65 | if i < 0: i = len(tail)
|
|---|
| 66 | head, tail = tail[:i], tail[i+1:]
|
|---|
|
|---|