Mercurial > hg > pyloader
diff pyloader/__init__.py @ 2:6cb7696edc4d
add some more stuff for string loady type things and rearrange structure
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Tue, 10 May 2011 09:28:40 -0700 |
parents | 47d9472e7754 |
children | 36f5d31c3ed6 |
line wrap: on
line diff
--- a/pyloader/__init__.py Tue May 03 14:26:35 2011 -0700 +++ b/pyloader/__init__.py Tue May 10 09:28:40 2011 -0700 @@ -1,49 +1,2 @@ -#!/usr/bin/env python - -""" -load modules and their attributes from a string -""" - -import imp -import os -import sys - -def import_dotted_path(module): - path = module.split('.') - module = __import__(module) - for name in path[1:]: - module = getattr(module, name) - return module - -def load(string): - """loads a string and returns a python object""" - - try: - if ':' in string: - path, target = string.split(':', 1) - if os.path.isabs(path) and os.path.exists(path): - # path to file - sys.path.append(os.path.dirname(path)) - module = imp.load_source(path, path) - sys.path.pop() - else: - module = import_dotted_path(path) - obj = module - while '.' in target: - attr, target = target.split('.', 1) - obj = getattr(obj, attr) - obj = getattr(obj, target) - return obj - else: - # module: dotted path - return import_dotted_path(string) - except: - print string - raise - - # TODO: entry points - -if __name__ == '__main__': - import sys - for i in sys.argv[1:]: - print load(i) +# import front-ends +from loader import *