-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhub.py
38 lines (29 loc) · 1.18 KB
/
hub.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
import importlib
IMPLEMENTATION_PATH = 'wot_api.models'
print('hub imported')
PERSISTENCE = {}
INIT_FUNCTION_NAME = 'init'
def invoke_implementation(function_name, params, kwargs, request, device):
import_path = IMPLEMENTATION_PATH + '.' + device
implementation_spec = importlib.util.find_spec(import_path)
found = implementation_spec is not None
if found:
implementation = importlib.import_module(import_path)
if hasattr(implementation, INIT_FUNCTION_NAME):
plugin_init_function = getattr(implementation, INIT_FUNCTION_NAME)
plugin_init_function()
if not hasattr(implementation, function_name):
return 'Implementation required for %s of device %s' % (function_name, device)
method = getattr(implementation, function_name)
if 'body' in kwargs:
body = kwargs['body']
for param in params:
if param not in kwargs:
kwargs[param] = body[param]
kwargs.pop('body')
if len(kwargs) > 0:
return method(**kwargs)
else:
return method()
else:
return 'Implementation required for device %s' % device