This repository was archived by the owner on Jun 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
89 lines (74 loc) · 2.61 KB
/
Copy path__init__.py
File metadata and controls
89 lines (74 loc) · 2.61 KB
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
import json
import logging
import os
import reprlib
import sys
import traceback
from types import ModuleType
from . import build
logger = logging.getLogger('python-service')
SIGNATURE_FILE = os.path.join(os.getcwd(), "nstack-metadata.json")
INTROSPECTION_FILE = os.path.join(os.getcwd(), "dbus-module.xml")
nstack_module = sys.modules[__name__] = ModuleType(__name__)
class DBusWrapper(object):
""" Proxy object that intercepts all requests, unpacks/packs as needed, and
forwards on to the user service"""
@classmethod
def update_dbus(cls):
with open(INTROSPECTION_FILE, "r") as f:
cls.dbus = f.read()
def __init__(self, service):
self.update_dbus()
try:
self.service = nstack_module._wrapObject(service)
except AttributeError:
logger.exception("error wrapping service object")
self.service = service
def __getattr__(self, method_name):
def method(args):
return self._make_call(method_name, args)
return method
def _make_call(self, method_name, args):
"""dynamically call into the user service"""
if logger.isEnabledFor(logging.DEBUG):
logger.debug("{}, data in: {}".format(method_name, reprlib.repr(args)))
func = getattr(self.service, method_name)
try:
r = func(args)
except Exception:
logger.exception("error calling service method: {} with args: {}".format(
method_name, args))
raise
if logger.isEnabledFor(logging.DEBUG):
logger.debug("{}, data out: {}".format(method_name, reprlib.repr(r)))
return r
class BaseService(object):
def __init__(self):
self.args = json.loads(os.environ['NSTACK_ARGS']) if 'NSTACK_ARGS' in os.environ else {}
self.startup()
logger.info("Starting service...")
def Quit(self):
self.shutdown()
logger.info("...stopping service")
def startup(self):
pass
def shutdown(self):
pass
# update the nstack module dict
nstack_module.__dict__.update({
'__file__': __file__,
'__doc__': __doc__,
'__path__': __path__,
'__package__': __package__,
#'__all__': ['_types'],
'DBusWrapper': DBusWrapper,
'BaseService': BaseService,
})
# add the base types and wrapObject function from the signature to the nstack_module
if(os.path.exists(SIGNATURE_FILE)):
with open(SIGNATURE_FILE) as f:
data = json.load(f)
a, b = build.process_schema(data["api"])
for i, j in a.items():
setattr(nstack_module, i, j)
setattr(nstack_module, '_wrapObject', b)