annotate pyloader/invoke.py @ 68:65609d7ba63d

move around when we create the (local) section dict so that we can override it as desired
author Jeff Hammel <jhammel@mozilla.com>
date Thu, 09 Jun 2011 09:29:06 -0700
parents a825f00fe062
children efb7a8a189e8
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"""
6cb7696edc4d add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
8 inspected = obj
6cb7696edc4d add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
9 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
10 inspected = obj.__init__ # inspect the ctor
4
5ce55f6c8964 invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
11 try:
5ce55f6c8964 invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
12 argspec = inspect.getargspec(inspected)
5ce55f6c8964 invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
13 except TypeError:
5ce55f6c8964 invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
14 argspec = None
5ce55f6c8964 invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
15 if argspec is None:
5ce55f6c8964 invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
16 args = [cast(i) for i in args]
5ce55f6c8964 invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
17 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
18 else:
5ce55f6c8964 invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
19 # TODO: get values from argspec
5ce55f6c8964 invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
20 args = [cast(i) for i in args]
5ce55f6c8964 invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
21 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
22 return obj(*args, **kwargs)
5ce55f6c8964 invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
23
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
24 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
25 """
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
26 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
27 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
28 """
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
29 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
30 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
31 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
32 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
33 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
34 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
35 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
36 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
37 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
38 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
39 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
40
4
5ce55f6c8964 invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
41 def main(args=sys.argv[1:]):
5ce55f6c8964 invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
42 """CLI entry point"""
3
65d9a7c7ac63 other misc improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
43 from loader import load
65d9a7c7ac63 other misc improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
44 from optparse import OptionParser
65d9a7c7ac63 other misc improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
45 usage = '%prog Object arg1=value1 arg2=value2 [...]'
65d9a7c7ac63 other misc improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
46 description = 'invoke an Object given string arguments'
65d9a7c7ac63 other misc improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
47 parser = OptionParser(usage=usage, description=description)
65d9a7c7ac63 other misc improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
48 if not args:
65d9a7c7ac63 other misc improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
49 parser.print_usage()
65d9a7c7ac63 other misc improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
50 parser.exit()
65d9a7c7ac63 other misc improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
51 obj = load(args[0])
4
5ce55f6c8964 invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
52 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
53 print strcall(obj, *obj_args, **obj_kwargs)
4
5ce55f6c8964 invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
54
5ce55f6c8964 invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
55 if __name__ == '__main__':
5ce55f6c8964 invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
56 main()