forked from ISISComputingGroup/lewis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
control.py
executable file
·89 lines (67 loc) · 2.74 KB
/
control.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python
import argparse
from core.control_client import ControlClient
def list_objects(remote):
for obj in remote.keys():
print(obj)
def show_api(remote, object_name):
if not object_name in remote.keys():
raise RuntimeError(
'Object \'{}\' is not exposed by remote. Use -l to get a list of objects.'.format(object_name))
obj = remote[object_name]
properties = list(obj._properties)
methods = []
for member_name in dir(obj):
if not member_name[0] in ('_', ':'):
member = getattr(obj, member_name)
if callable(member):
methods.append(member_name)
print('Type: {}'.format(type(obj).__name__))
print('Properties:')
for prop in sorted(properties):
print('\t{}'.format(prop))
print('Methods:')
for method in sorted(methods):
print('\t{}'.format(method))
def convert_type(value):
try:
return int(value)
except ValueError:
try:
return float(value)
except ValueError:
return value
def call_method(remote, object_name, method, arguments):
if not method:
raise RuntimeError('Missing object member. Use -a to get a list of members.')
attr = getattr(remote[object_name], method)
args = [convert_type(arg) for arg in arguments]
if callable(attr):
return attr(*args)
else:
if not arguments:
return attr
else:
setattr(remote[object_name], method, *args)
parser = argparse.ArgumentParser(
description='A client to manipulate the simulated device remotely through a separate channel. The simulation must be started with the --rpc-host option.')
parser.add_argument('-r', '--rpc-host', default='127.0.0.1:10000',
help='HOST:PORT string specifying control server to connect to.')
parser.add_argument('-n', '--print-none', action='store_true',
help='Print None return value.')
parser.add_argument('object', nargs='?', default=None, help='Object to control. If left out, all objects are listed.')
parser.add_argument('member', nargs='?', default=None,
help='Object-member to access. If omitted, API of the object is listed.')
parser.add_argument('arguments', nargs='*',
help='Arguments to method call. For setting a property, supply the property value. ')
args = parser.parse_args()
remote = ControlClient(*args.rpc_host.split(':')).get_object_collection()
if not args.object:
list_objects(remote)
else:
if not args.member:
show_api(remote, args.object)
else:
response = call_method(remote, args.object, args.member, args.arguments)
if response is not None or args.print_none:
print(response)