# HG changeset patch # User Jeff Hammel # Date 1305044920 25200 # Node ID 6cb7696edc4ddd283e1700a6c836ba0da1bd4692 # Parent 47d9472e77549f8d6e39e2e217efb6f01154286e add some more stuff for string loady type things and rearrange structure diff -r 47d9472e7754 -r 6cb7696edc4d pyloader/__init__.py --- 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 * diff -r 47d9472e7754 -r 6cb7696edc4d pyloader/cast.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pyloader/cast.py Tue May 10 09:28:40 2011 -0700 @@ -0,0 +1,50 @@ +class StringCastException(Exception): + """raised on unsucessful cast""" + +class StringCaster(object): + """cast strings to other things""" + # as yet unused + + def __init__(self, *casts): + self.casts = casts + + def __call__(self, string): + for cast in self.casts: + try: + return cast(string) + except: + continue + return string + +### casters + +def str2bool(string): + mapping = {'true': True, + 'True': True, + 'false': False, + 'False': False} + return mapping[string] + +def str2list(string, separator=None): + string = string.rstrip(separator) + return [i.strip() for i in string.split(separator)] + +def str2args(string, separator=None): + args = str2list(string, separator) + _args = [] + kw = {} + for arg in args: + if '=' in arg: + pass + else: + _args.append(arg) + return _args, kw + +# convenience functions +casts = [int, float, str2bool, str2list, str2args] +cast = StringCaster(*casts) + +if __name__ == '__main__': + import sys + for arg in sys.argv[1:]: + print cast(arg) diff -r 47d9472e7754 -r 6cb7696edc4d pyloader/invoke.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pyloader/invoke.py Tue May 10 09:28:40 2011 -0700 @@ -0,0 +1,11 @@ +import inspect + +def call(obj, *args, **kwargs): + """call a thing with string arguments""" + inspected = obj + if inspect.isclass(obj): + inspected = obj.__init__ # inspect the ctor + argspec = inspect.getargspec(inspected) + + + diff -r 47d9472e7754 -r 6cb7696edc4d pyloader/loader.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pyloader/loader.py Tue May 10 09:28:40 2011 -0700 @@ -0,0 +1,49 @@ +#!/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) diff -r 47d9472e7754 -r 6cb7696edc4d setup.py --- a/setup.py Tue May 03 14:26:35 2011 -0700 +++ b/setup.py Tue May 10 09:28:40 2011 -0700 @@ -7,7 +7,7 @@ except IOError: description = '' -version = "0.1.1" +version = "0.1.2" dependencies = [] setup(name='pyloader',