Mercurial > hg > pyloader
annotate pyloader/invoke.py @ 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 |
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 |
6cb7696edc4d
add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
6 def call(obj, *args, **kwargs): |
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 |
5ce55f6c8964
invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
24 def main(args=sys.argv[1:]): |
5ce55f6c8964
invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
25 """CLI entry point""" |
3 | 26 from loader import load |
27 from optparse import OptionParser | |
28 usage = '%prog Object arg1=value1 arg2=value2 [...]' | |
29 description = 'invoke an Object given string arguments' | |
30 parser = OptionParser(usage=usage, description=description) | |
31 if not args: | |
32 parser.print_usage() | |
33 parser.exit() | |
34 obj = load(args[0]) | |
4
5ce55f6c8964
invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
35 obj_args, obj_kwargs = str2args(' '.join(args[1:])) |
5ce55f6c8964
invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
36 print call(obj, *obj_args, **obj_kwargs) |
5ce55f6c8964
invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
37 |
5ce55f6c8964
invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
38 if __name__ == '__main__': |
5ce55f6c8964
invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
39 main() |