-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebservicecommon.py
274 lines (221 loc) · 10.1 KB
/
webservicecommon.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#!/usr/bin/python
# coding=utf-8
import logging
import types
import web
from hacommon import SerializableQueueItem
# region Type definitions
class WebServiceDefinition:
def __init__(self, url, cl, jsurl, jsname, jsenums=None, methodname=None, argnames=None):
self.url = url # path
self.cl = cl # class (as str)
self.jsurl = jsurl # start URL for JS (arguments will be / separated behind this)
self.jsname = jsname # JS bind css class name
if jsenums is None:
jsenums = {}
self.jsenums = jsenums
self.methodname = methodname
if argnames is None:
argnames = []
self.argnames = argnames
def __str__(self):
return 'WebServiceDefinition(url=%s, cl=%s, jsurl=%s jsname=%s methodname=%s argnames=%s)' \
% (self.url, self.cl, self.jsurl, self.jsname, self.methodname, self.argnames)
def __repr__(self):
return self.__str__()
class WebServiceDefinitionList(list):
pass
class WSBinding(object):
def __init__(self, webservice_class, wsparameters=None):
if type(webservice_class) == types.ClassType:
webservice_class = webservice_class.__name__ # we only need the string representation
self.webservice_class = webservice_class
if wsparameters is None:
wsparameters = []
self.wsparameters = wsparameters
class WSParam(object):
def __init__(self, arg, arg_regex, arg_enums=None):
self.arg = arg
self.arg_regex = arg_regex
if arg_enums is None:
arg_enums = {}
self.arg_enums = arg_enums
def __str__(self):
return 'WSParam(' + self.arg + ', ' + self.arg_regex + ', ' + str(self.arg_enums) + ')'
def __repr__(self):
return self.__str__()
# endregion
def webservicedecorator_globals_add(**kwargs):
"""
This decorator helper method will change the incoming parameters for all modules in an
instance and is thus deprecated
It is not a decorator per se but shares global namespace with it and is thus able to append to its global variable
:param kwargs:
:return:
"""
global webservice_globals
for key in kwargs.keys():
webservice_globals[key] = kwargs[key]
def webservice_state_instances_add(name, inst):
"""
Module registration for the state decorator (helps webservice thread know which modules are relevant for
the overall system state output)
:param name:
:param inst:
:return:
"""
global webservice_state_instances
webservice_state_instances[name] = inst
def webservice_class_instances_add(classname, classinstance):
"""
Module registration to be done by the modules themselves after init
This is required to use the shared web methods WebService_Dynamic_Set & WebService_Dynamic_Get as callbacks
At runtime it will be passed on to the decorated GET call
:param classinstance:
:param classname:
:return:
"""
global webservice_module_class_instances
webservice_module_class_instances[classname.lower()] = classinstance
def webservice_hawebservice_init(**kwargs):
"""
Initializer & shorthand helper for webservice class similar to webservice_state_instances_add
Registers "common" global variables SharedQueue and ThreadList - now deprecated to decopule/unconfuse
application flow and variable sharing in general
:param kwargs:
:return:
"""
global webservice_globals # now deprecated
webservice_globals = {}
for key in kwargs.keys():
webservice_globals[key] = kwargs[key] # perhaps just clone/copy the kwargs dict directly?
global webservice_state_instances
webservice_state_instances = {}
global webservice_module_class_instances
webservice_module_class_instances = {}
def webservice_state_jsonp(f):
"""
Decorator for the module overall state (this will be polled by a browser to update all component states in the view)
:param f:
:return:
"""
def decorated(*args, **kwargs):
callback_name = web.input(callback='jsonCallback').callback
web.header('Content-Type', 'application/javascript')
for key in webservice_state_instances.keys():
kwargs[key] = webservice_state_instances[key]
return '%s(%s)' % (callback_name, f(*args, **kwargs))
return decorated
def webservice_json(f):
"""
decorator for GET methods in webservice classes
:param f:
:return: json
"""
def decorated(*args, **kwargs):
web.header('Content-Type', 'application/javascript')
decorator = args[0]
modulename = decorator.__module__
if modulename in webservice_module_class_instances.keys():
decorator.currentInstance = webservice_module_class_instances[modulename]
logging.info('modulename=%s instance=%s' %
(modulename, repr(webservice_module_class_instances[modulename])))
else:
logging.warn('! No class instance reference found for ' + modulename)
decorator.currentInstance = None
retval = f(*args, **kwargs)
return '%s' % retval
return decorated
def webservice_jsonp(f):
"""
decorator for GET methods in webservice classes (jsonp variant - cross site)
:param f:
:return: jsonp
"""
def decorated(*args, **kwargs):
callback_name = web.input(callback='jsonCallback').callback
web.header('Content-Type', 'application/javascript')
# shortcut? call above decorator to get returned data and format it
# with %s(%s) after.. data = webservice_json(f)
decorator = args[0]
modulename = decorator.__module__
modulename = modulename.replace('modules.', '')
# global webservice_module_class_instances
if modulename in webservice_module_class_instances.keys():
decorator.currentInstance = webservice_module_class_instances[modulename]
logging.debug('modulename=%s instance=%s' %
(modulename, repr(webservice_module_class_instances[modulename])))
else:
logging.warn('No class instance reference found for %s' % modulename)
logging.warn("keys: %s" % repr(webservice_module_class_instances.keys()))
decorator.currentInstance = None
retval = f(*args, **kwargs)
return '%s(%s)' % (callback_name, retval)
return decorated
def ws_register_class(cls):
# logging.info('class register called: ' + cls.webservice_register_class)
cls._webservice_definitions = []
for methodname in dir(cls):
method = getattr(cls, methodname)
if hasattr(method, '_prop'): # url, cl, jsurl, jsname, methodname=None, argnames
for wsbinding in method._prop:
jsenums = {}
for p in wsbinding.wsparameters:
jsenums[p.arg] = p.arg_enums
wsdi = WebServiceDefinition(
url='/' + cls.__name__ + '/' + methodname + '/' + '/'.join(
[i.arg_regex for i in wsbinding.wsparameters]),
cl=wsbinding.webservice_class, # cls.webservice_register_class,
jsurl='/' + cls.__name__ + '/' + methodname + '/',
jsname='WS_' + cls.__name__ + '_' + ''.join([i.capitalize() for i in methodname.split('_')]),
jsenums=jsenums, # [{i.arg: i.arg_enums} for i in wsbinding.wsparameters],
methodname=methodname,
argnames=[i.arg for i in wsbinding.wsparameters]
)
# logging.info(wsdi)
cls._webservice_definitions.append(wsdi)
return cls
def ws_register_definition(*args):
def wrapper(func):
func._prop = args
return func
return wrapper
class WebService_Dynamic_Set(object):
currentInstance = None
def __init__(self, *args, **kwargs):
# TODO: these are supposed to be given via OOP possible? its a minor repeat pattern
self.parentClass = None
self.methodname = None
@webservice_jsonp
def GET(self, *args, **kwargs):
path_parts = web.ctx['environ']['PATH_INFO'].split('/') # ['', 'HAJointSpace', 'set_input_key', 'Mute']
if len(path_parts) >= 3:
# TODO: i dont even know whats going on here anymore :S
self.parentClass = path_parts[1]
self.methodname = path_parts[2]
# logging.info('Dynamic WS Set call - %s %s %s' %(self.parentClass, self.methodname,
# getattr(self.currentInstance, self.methodname)) )
# SharedQueue.append(sqi) TODO: this is no longer happening at all
if hasattr(self.currentInstance, 'queue'):
logging.info('adding action to queue for ' + self.parentClass)
sqi = SerializableQueueItem(self.parentClass, getattr(self.currentInstance, self.methodname), *args)
self.currentInstance.queue.append(sqi)
else:
logging.info('exec action directly for ' + self.parentClass)
getattr(self.currentInstance, self.methodname)(*args)
return self.currentInstance.get_json_status()
# return state as it was before action is likely to have taken place.. may not be that useful
class WebService_Dynamic_Get(object):
currentInstance = None
def __init__(self, *args, **kwargs):
self.parentClass = None
self.methodname = None
@webservice_jsonp
def GET(self, *args, **kwargs):
path_parts = web.ctx['environ']['PATH_INFO'].split('/') # ['', 'HAJointSpace', 'set_input_key', 'Mute']
if len(path_parts) >= 3:
self.parentClass = path_parts[1]
self.methodname = path_parts[2]
# logging.info('Dynamic WS Get call - %s %s %s' %(self.parentClass, self.methodname,
# getattr(self.currentInstance, self.methodname)) )
return getattr(self.currentInstance, self.methodname)(*args)