0
|
1 #!/usr/bin/env python
|
|
2
|
|
3 """
|
|
4 load modules and their attributes from a string
|
|
5 """
|
|
6
|
|
7 import imp
|
|
8 import os
|
|
9
|
|
10 def import_dotted_path(module):
|
|
11 path = module.split('.')
|
|
12 module = __import__(module)
|
|
13 for name in path[1:]:
|
|
14 module = getattr(module, name)
|
|
15 return module
|
|
16
|
|
17 def load(string):
|
|
18 """loads a string and returns a python object"""
|
|
19
|
|
20 try:
|
|
21 if ':' in string:
|
|
22 path, target = string.split(':', 1)
|
|
23 if os.path.isabs(path) and os.path.exists(path):
|
|
24 # path to file
|
|
25 module = imp.load_source(path, path)
|
|
26 else:
|
|
27 module = import_dotted_path(path)
|
|
28 obj = module
|
|
29 while '.' in target:
|
|
30 attr, target = target.split('.', 1)
|
|
31 obj = getattr(obj, attr)
|
|
32 obj = getattr(obj, target)
|
|
33 return obj
|
|
34 else:
|
|
35 # module: dotted path
|
|
36 return import_dotted_path(string)
|
|
37 except:
|
|
38 print string
|
|
39 raise
|
|
40
|
|
41 # TODO: entry points
|
|
42
|
|
43 if __name__ == '__main__':
|
|
44 import sys
|
|
45 for i in sys.argv[1:]:
|
|
46 print load(i)
|