1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
# Copyright: 2005 Gentoo Foundation
# Author(s): Brian Harring (ferringb@gentoo.org)
# License: GPL2
# $Header: /local/data/ulm/cvs/history/var/cvsroot/gentoo-src/portage/portage/util/modules.py,v 1.3 2005/08/03 00:22:52 ferringb Exp $
class FailedImport(ImportError):
def __init__(self, trg, e): self.trg, self.e = trg, e
def __str__(self): return "Failed importing target '%s': '%s'" % (self.trg, self.e)
def load_module(name):
try:
m = __import__(name)
nl = name.split('.')
# __import__ returns nl[0]... so.
nl.pop(0)
while len(nl):
m = getattr(m, nl[0])
nl.pop(0)
return m
except (AttributeError, ImportError), e:
raise FailedImport(name, e)
def load_attribute(name):
try:
i = name.rfind(".")
if i == -1:
raise ValueError("name isn't an attribute, it's a module... : %s" % name)
m = load_module(name[:i])
m = getattr(m, name[i+1:])
return m
except (AttributeError, ImportError), e:
raise FailedImport(name, e)
|