Mercurial > hg > pyloader
annotate pyloader/invoke.py @ 98:d59f0425d705 default tip
version bump
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Mon, 27 May 2024 16:20:52 -0700 |
parents | 93cf6ebe8a94 |
children |
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 |
93 | 3 from .cast import cast |
4 from .cast import str2args | |
5 | |
2
6cb7696edc4d
add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
6 |
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
|
7 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
|
8 """call a thing with string arguments""" |
77 | 9 |
10 # TODO: | |
11 # could have modes for casting, at least for intrinsic types | |
12 # - optimistic: try to cast everything '1' -> 1 | |
13 # - argument-based: only cast what's in the arguments | |
14 # - you should also be able to provide your own casts | |
15 # This will have to be made an object in order to actually do this | |
16 # so that these parameters may be provided to __init__ | |
92 | 17 |
2
6cb7696edc4d
add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
18 inspected = obj |
6cb7696edc4d
add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
19 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
|
20 inspected = obj.__init__ # inspect the ctor |
4
5ce55f6c8964
invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
21 try: |
5ce55f6c8964
invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
22 argspec = inspect.getargspec(inspected) |
5ce55f6c8964
invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
23 except TypeError: |
5ce55f6c8964
invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
24 argspec = None |
5ce55f6c8964
invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
25 if argspec is None: |
5ce55f6c8964
invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
26 args = [cast(i) for i in args] |
5ce55f6c8964
invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
27 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
|
28 else: |
5ce55f6c8964
invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
29 # TODO: get values from argspec |
5ce55f6c8964
invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
30 args = [cast(i) for i in args] |
5ce55f6c8964
invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
31 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
|
32 return obj(*args, **kwargs) |
5ce55f6c8964
invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
33 |
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
|
34 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
|
35 """ |
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 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
|
37 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
|
38 """ |
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 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
|
40 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
|
41 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
|
42 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
|
43 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
|
44 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
|
45 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
|
46 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
|
47 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
|
48 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
|
49 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
|
50 |
92 | 51 |
4
5ce55f6c8964
invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
52 def main(args=sys.argv[1:]): |
5ce55f6c8964
invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
53 """CLI entry point""" |
92 | 54 |
3 | 55 from loader import load |
56 from optparse import OptionParser | |
57 usage = '%prog Object arg1=value1 arg2=value2 [...]' | |
58 description = 'invoke an Object given string arguments' | |
59 parser = OptionParser(usage=usage, description=description) | |
60 if not args: | |
61 parser.print_usage() | |
62 parser.exit() | |
63 obj = load(args[0]) | |
4
5ce55f6c8964
invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
64 obj_args, obj_kwargs = str2args(' '.join(args[1:])) |
92 | 65 print(strcall(obj, *obj_args, **obj_kwargs)) |
4
5ce55f6c8964
invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
66 |
5ce55f6c8964
invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
67 if __name__ == '__main__': |
5ce55f6c8964
invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
68 main() |