annotate pyloader/invoke.py @ 77:efb7a8a189e8

update comments/docstring
author Jeff Hammel <jhammel@mozilla.com>
date Thu, 09 Jun 2011 18:40:52 -0700
parents a825f00fe062
children 36f5d31c3ed6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
6cb7696edc4d add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 import inspect
4
5ce55f6c8964 invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
2 import sys
5ce55f6c8964 invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
3 from cast import cast
5ce55f6c8964 invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
4 from cast import str2args
2
6cb7696edc4d add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
5
5
a825f00fe062 add a different call method, rename the old one, and have a test for it
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
6 def strcall(obj, *args, **kwargs):
2
6cb7696edc4d add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
7 """call a thing with string arguments"""
77
efb7a8a189e8 update comments/docstring
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
8
efb7a8a189e8 update comments/docstring
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
9 # TODO:
efb7a8a189e8 update comments/docstring
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
10 # could have modes for casting, at least for intrinsic types
efb7a8a189e8 update comments/docstring
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
11 # - optimistic: try to cast everything '1' -> 1
efb7a8a189e8 update comments/docstring
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
12 # - argument-based: only cast what's in the arguments
efb7a8a189e8 update comments/docstring
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
13 # - you should also be able to provide your own casts
efb7a8a189e8 update comments/docstring
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
14 # This will have to be made an object in order to actually do this
efb7a8a189e8 update comments/docstring
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
15 # so that these parameters may be provided to __init__
efb7a8a189e8 update comments/docstring
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
16
2
6cb7696edc4d add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
17 inspected = obj
6cb7696edc4d add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
18 if inspect.isclass(obj):
6cb7696edc4d add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
19 inspected = obj.__init__ # inspect the ctor
4
5ce55f6c8964 invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
20 try:
5ce55f6c8964 invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
21 argspec = inspect.getargspec(inspected)
5ce55f6c8964 invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
22 except TypeError:
5ce55f6c8964 invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
23 argspec = None
5ce55f6c8964 invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
24 if argspec is None:
5ce55f6c8964 invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
25 args = [cast(i) for i in args]
5ce55f6c8964 invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
26 kwargs = dict([(key, cast(value)) for key, value in kwargs.items()])
5ce55f6c8964 invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
27 else:
5ce55f6c8964 invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
28 # TODO: get values from argspec
5ce55f6c8964 invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
29 args = [cast(i) for i in args]
5ce55f6c8964 invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
30 kwargs = dict([(key, cast(value)) for key, value in kwargs.items()])
5ce55f6c8964 invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
31 return obj(*args, **kwargs)
5ce55f6c8964 invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
32
5
a825f00fe062 add a different call method, rename the old one, and have a test for it
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
33 def call(obj, **kwargs):
a825f00fe062 add a different call method, rename the old one, and have a test for it
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
34 """
a825f00fe062 add a different call method, rename the old one, and have a test for it
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
35 call an object with the subset of kwargs appropriate to the object.
a825f00fe062 add a different call method, rename the old one, and have a test for it
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
36 this assumes that the obj does not take **kwargs
a825f00fe062 add a different call method, rename the old one, and have a test for it
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
37 """
a825f00fe062 add a different call method, rename the old one, and have a test for it
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
38 inspected = obj
a825f00fe062 add a different call method, rename the old one, and have a test for it
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
39 if inspect.isclass(obj):
a825f00fe062 add a different call method, rename the old one, and have a test for it
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
40 inspected = obj.__init__ # inspect the ctor
a825f00fe062 add a different call method, rename the old one, and have a test for it
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
41 args = inspect.getargspec(inspected).args[1:]
a825f00fe062 add a different call method, rename the old one, and have a test for it
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
42 else:
a825f00fe062 add a different call method, rename the old one, and have a test for it
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
43 args = inspect.getargspec(obj).args[:]
a825f00fe062 add a different call method, rename the old one, and have a test for it
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
44 kw = {} # kwargs to invoke obj with
a825f00fe062 add a different call method, rename the old one, and have a test for it
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
45 for arg in args:
a825f00fe062 add a different call method, rename the old one, and have a test for it
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
46 if arg in kwargs:
a825f00fe062 add a different call method, rename the old one, and have a test for it
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
47 kw[arg] = kwargs[arg]
a825f00fe062 add a different call method, rename the old one, and have a test for it
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
48 return obj(**kw)
a825f00fe062 add a different call method, rename the old one, and have a test for it
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
49
4
5ce55f6c8964 invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
50 def main(args=sys.argv[1:]):
5ce55f6c8964 invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
51 """CLI entry point"""
3
65d9a7c7ac63 other misc improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
52 from loader import load
65d9a7c7ac63 other misc improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
53 from optparse import OptionParser
65d9a7c7ac63 other misc improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
54 usage = '%prog Object arg1=value1 arg2=value2 [...]'
65d9a7c7ac63 other misc improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
55 description = 'invoke an Object given string arguments'
65d9a7c7ac63 other misc improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
56 parser = OptionParser(usage=usage, description=description)
65d9a7c7ac63 other misc improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
57 if not args:
65d9a7c7ac63 other misc improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
58 parser.print_usage()
65d9a7c7ac63 other misc improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
59 parser.exit()
65d9a7c7ac63 other misc improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
60 obj = load(args[0])
4
5ce55f6c8964 invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
61 obj_args, obj_kwargs = str2args(' '.join(args[1:]))
5
a825f00fe062 add a different call method, rename the old one, and have a test for it
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
62 print strcall(obj, *obj_args, **obj_kwargs)
4
5ce55f6c8964 invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
63
5ce55f6c8964 invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
64 if __name__ == '__main__':
5ce55f6c8964 invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
65 main()