# HG changeset patch # User Jeff Hammel # Date 1305079257 25200 # Node ID 5ce55f6c8964250661bf055840e6a8483b86c76f # Parent 65d9a7c7ac63f0c8b190964cc5616065b951a76d invokation works in the barest sense diff -r 65d9a7c7ac63 -r 5ce55f6c8964 pyloader/cast.py --- a/pyloader/cast.py Tue May 10 10:26:19 2011 -0700 +++ b/pyloader/cast.py Tue May 10 19:00:57 2011 -0700 @@ -35,7 +35,10 @@ kw = {} for arg in args: if '=' in arg: - pass + key, value = arg.split('=', 1) + key = key.strip() + value = value.strip() + kw[key] = value else: _args.append(arg) return _args, kw diff -r 65d9a7c7ac63 -r 5ce55f6c8964 pyloader/invoke.py --- a/pyloader/invoke.py Tue May 10 10:26:19 2011 -0700 +++ b/pyloader/invoke.py Tue May 10 19:00:57 2011 -0700 @@ -1,23 +1,39 @@ import inspect +import sys +from cast import cast +from cast import str2args 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) - -if __name__ == '__main__': - import sys + try: + argspec = inspect.getargspec(inspected) + except TypeError: + argspec = None + if argspec is None: + args = [cast(i) for i in args] + kwargs = dict([(key, cast(value)) for key, value in kwargs.items()]) + else: + # TODO: get values from argspec + args = [cast(i) for i in args] + kwargs = dict([(key, cast(value)) for key, value in kwargs.items()]) + return obj(*args, **kwargs) + +def main(args=sys.argv[1:]): + """CLI entry point""" from loader import load from optparse import OptionParser usage = '%prog Object arg1=value1 arg2=value2 [...]' description = 'invoke an Object given string arguments' parser = OptionParser(usage=usage, description=description) - args = sys.argv[1:] if not args: parser.print_usage() parser.exit() - obj_args = dict([i.split('=', 1) for i in args[1:]]) obj = load(args[0]) - print call(obj, **obj_args) + obj_args, obj_kwargs = str2args(' '.join(args[1:])) + print call(obj, *obj_args, **obj_kwargs) + +if __name__ == '__main__': + main() diff -r 65d9a7c7ac63 -r 5ce55f6c8964 setup.py --- a/setup.py Tue May 10 10:26:19 2011 -0700 +++ b/setup.py Tue May 10 19:00:57 2011 -0700 @@ -25,6 +25,8 @@ install_requires=dependencies, entry_points=""" # -*- Entry points: -*- + [console_scripts] + pyloader = pyloader.invoke:main """, )