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
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
93
93cf6ebe8a94 more -> python3
Jeff Hammel <k0scist@gmail.com>
parents: 92
diff changeset
3 from .cast import cast
93cf6ebe8a94 more -> python3
Jeff Hammel <k0scist@gmail.com>
parents: 92
diff changeset
4 from .cast import str2args
93cf6ebe8a94 more -> python3
Jeff Hammel <k0scist@gmail.com>
parents: 92
diff changeset
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
efb7a8a189e8 update comments/docstring
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
9
efb7a8a189e8 update comments/docstring
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
10 # TODO:
efb7a8a189e8 update comments/docstring
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
11 # could have modes for casting, at least for intrinsic types
efb7a8a189e8 update comments/docstring
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
12 # - optimistic: try to cast everything '1' -> 1
efb7a8a189e8 update comments/docstring
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
13 # - argument-based: only cast what's in the arguments
efb7a8a189e8 update comments/docstring
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
14 # - you should also be able to provide your own casts
efb7a8a189e8 update comments/docstring
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
15 # 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
16 # so that these parameters may be provided to __init__
92
36f5d31c3ed6 partial upgrade to py3.7
Jeff Hammel <k0scist@gmail.com>
parents: 77
diff changeset
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
36f5d31c3ed6 partial upgrade to py3.7
Jeff Hammel <k0scist@gmail.com>
parents: 77
diff changeset
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
36f5d31c3ed6 partial upgrade to py3.7
Jeff Hammel <k0scist@gmail.com>
parents: 77
diff changeset
54
3
65d9a7c7ac63 other misc improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
55 from loader import load
65d9a7c7ac63 other misc improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
56 from optparse import OptionParser
65d9a7c7ac63 other misc improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
57 usage = '%prog Object arg1=value1 arg2=value2 [...]'
65d9a7c7ac63 other misc improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
58 description = 'invoke an Object given string arguments'
65d9a7c7ac63 other misc improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
59 parser = OptionParser(usage=usage, description=description)
65d9a7c7ac63 other misc improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
60 if not args:
65d9a7c7ac63 other misc improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
61 parser.print_usage()
65d9a7c7ac63 other misc improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
62 parser.exit()
65d9a7c7ac63 other misc improvements
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
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
36f5d31c3ed6 partial upgrade to py3.7
Jeff Hammel <k0scist@gmail.com>
parents: 77
diff changeset
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()