Package madrona :: Package openid :: Package utils :: Module importlib
[hide private]

Source Code for Module madrona.openid.utils.importlib

 1  # From django project. See LICENSE_DJANGO for license. 
 2  # Taken from Python 2.7 with permission from/by the original author. 
 3   
 4  import sys 
 5   
6 -def _resolve_name(name, package, level):
7 """Return the absolute name of the module to be imported.""" 8 if not hasattr(package, 'rindex'): 9 raise ValueError("'package' not set to a string") 10 dot = len(package) 11 for x in xrange(level, 1, -1): 12 try: 13 dot = package.rindex('.', 0, dot) 14 except ValueError: 15 raise ValueError("attempted relative import beyond top-level " 16 "package") 17 return "%s.%s" % (package[:dot], name)
18 19
20 -def import_module(name, package=None):
21 """Import a module. 22 23 The 'package' argument is required when performing a relative import. It 24 specifies the package to use as the anchor point from which to resolve the 25 relative import to an absolute import. 26 27 """ 28 if name.startswith('.'): 29 if not package: 30 raise TypeError("relative imports require the 'package' argument") 31 level = 0 32 for character in name: 33 if character != '.': 34 break 35 level += 1 36 name = _resolve_name(name[level:], package, level) 37 __import__(name) 38 return sys.modules[name]
39