changeset 4:5ce55f6c8964

invokation works in the barest sense
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 10 May 2011 19:00:57 -0700
parents 65d9a7c7ac63
children a825f00fe062
files pyloader/cast.py pyloader/invoke.py setup.py
diffstat 3 files changed, 29 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- 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
--- 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()
--- 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
       """,
       )