/
json_connection.py
154 lines (128 loc) · 5.41 KB
/
json_connection.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
# (c) Copyright 2013, 2014, University of Manchester
#
# HydraLib is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# HydraPlatform is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with HydraPlatform. If not, see <http://www.gnu.org/licenses/>
#
# -*- coding: utf-8 -*-
#JSONConnection are synonyums, and used for backward compatibility
__all__ = ['JSONConnection']
import logging
log = logging.getLogger(__name__)
from datetime import datetime
import hydra_base as hb
import six
import collections
from .base_connection import BaseConnection
import json
class JSONConnection(BaseConnection):
""" Local connection to a Hydra database using hydra_base directly."""
def __init__(self, *args, **kwargs):
super(JSONConnection, self).__init__(*args, **kwargs)
#Hydra Base needs a user ID in its function calls. setting self.user_id
#allows this client to set this implicitly
self.user_id = kwargs.get('user_id', None)
self.db_url = None
#The user ID can be accessed from the session if it's not set explicitly.
self.session_id = kwargs.get('session_id', None)
if self.user_id is None and self.session_id is not None:
self.user_id = hb.get_session_user(self.session_id)
if kwargs.get('session') is None:
self.db_url = kwargs.get('db_url', None)
self.autocommit = kwargs.get('autocommit', True)
self.db_url = hb.db.connect(self.db_url)
def call(self, func_name, *args, **kwargs):
func = getattr(hb, func_name)
# Add user_id to the kwargs if not given and logged in.
if 'user_id' not in kwargs and self.user_id is not None:
kwargs['user_id'] = self.user_id
else:
self.login()
# Convert the arguments to JSON objects
json_obj_args = list(self.args_to_json_object(*args))
k = list(kwargs.keys())
v = list(self.args_to_json_object(*list(kwargs.values())))
json_obj_kwargs = {k[i]:v[i] for i in range(len(v))}
try:
ret = func(*json_obj_args, **json_obj_kwargs)
except Exception as e:
hb.db.DBSession.rollback()
hb.rollback_transaction()
raise
# Call the HB function
try:
json_resp = list(self.args_to_json_object(ret))
except ValueError as e:
log.warning(e)
json_resp = [ret]
for o in json_resp:
if self.autocommit is True:
try:
hb.commit_transaction()
except Exception as e:
hb.db.DBSession.rollback()
hb.rollback_transaction()
raise
finally:
hb.db.close_session()
hb.db.engine.dispose()
return o
def connect(self):
try:
hb.util.hdb.create_default_users_and_perms()
hb.util.hdb.create_default_units_and_dimensions()
hb.util.hdb.make_root_user()
hb.util.hdb.create_default_net()
hb.commit_transaction()
except:
hb.rollback_transaction()
def close_session(self):
hb.db.close_session()
hb.db.engine.dispose()
def login(self, username=None, password=None):
parsed_username, parsed_password = self.get_username_and_password(username, password)
self.user_id, self.session_id = hb.login(parsed_username, parsed_password)
return self.user_id, self.session_id
def logout(self):
hb.logout(self.session_id)
self.user_id, self.session_id = None, None
return 'OK'
def args_to_json_object(self, *args):
for arg in args:
if arg is None:
yield None
elif isinstance(arg, six.string_types):
yield arg
elif isinstance(arg, (int, float)):
yield arg
elif isinstance(arg, datetime):
yield datetime.strftime(arg, self.dateformat)
elif isinstance(arg, hb.JSONObject):
yield arg
elif isinstance(arg, collections.abc.Mapping):
yield hb.JSONObject(arg)
elif isinstance(arg, collections.abc.Iterable):
arg = list(arg) ## in case it's a set or something that doesnt support indexing
if len(arg) > 0 and isinstance(arg[0], (six.string_types, int, float, datetime)):
json_friendly_arg = []
for a in arg:
if isinstance(a, datetime):
json_friendly_arg.append(datetime.strftime(a, self.dateformat))
else:
json_friendly_arg.append(a)
yield json_friendly_arg
elif len(arg) > 0 and isinstance(arg[0], hb.JSONObject):
yield arg
else:
yield [hb.JSONObject(v) for v in arg]
else:
yield hb.JSONObject(arg)