-
Notifications
You must be signed in to change notification settings - Fork 0
/
gstore.py
283 lines (251 loc) · 14.4 KB
/
gstore.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
275
276
277
278
279
280
281
282
283
"""
# Filename: GStoreConnector.py
# Author: yangchaofan suxunbin
# Last Modified: 2019-5-15 18:10
# Description: http api for python
"""
import sys
import requests
version = sys.version[0]
if version == '3':
from urllib import parse
defaultServerIP = "127.0.0.1"
defaultServerPort = "9000"
class GstoreConnector:
def __init__(self, ip, port, username, password):
if (ip == "localhost"):
self.serverIP = defaultServerIP
else:
self.serverIP = ip
self.serverPort = port
self.Url = "http://" + self.serverIP + ":" + str(self.serverPort) + "/"
self.username = username
self.password = password
def UrlEncode(self, s):
ret = ""
if version == '2':
for i in range(len(s)):
c = s[i]
if ((ord(c)==42) or (ord(c)==45) or (ord(c)==46) or (ord(c)==47) or (ord(c)==58) or (ord(c)==95)):
ret += c
elif ((ord(c)>=48) and (ord(c)<=57)):
ret += c
elif ((ord(c)>=65) and (ord(c)<=90)):
ret += c
elif ((ord(c)>=97) and (ord(c)<=122)):
ret += c
elif (ord(c)==32):
ret += '+'
elif ((ord(c)!=9) and (ord(c)!=10) and (ord(c)!=13)):
ret += "{}{:X}".format("%", ord(c))
elif version == '3':
ret = parse.quote(s)
return ret
def Get(self, strUrl):
r = requests.get(self.Url + self.UrlEncode(strUrl))
return r.text
def Post(self, strPost):
r = requests.post(self.Url + strPost)
return r.text
def fGet(self, strUrl, filename):
r = requests.get(self.Url + self.UrlEncode(strUrl), stream=True)
with open(filename, 'wb') as fd:
for chunk in r.iter_content(4096):
fd.write(chunk)
return
def fPost(self, strPost, filename):
r = requests.post(self.Url + strPost, stream=True)
with open(filename, 'wb') as fd:
for chunk in r.iter_content(4096):
fd.write(chunk)
return
def build(self, db_name, db_path, request_type='GET'):
if request_type == 'GET':
strUrl = "?operation=build&db_name=" + db_name + "&db_path=" + db_path + "&username=" + self.username + "&password=" + self.password
res = self.Get(strUrl)
elif request_type == 'POST':
strPost = '{"operation": "build", "db_name": "' + db_name + '", "db_path": "' + db_path + '", "username": "' + self.username + '", "password": "' + self.password + '"}'
res = self.Post(strPost)
return res
def check(self, request_type='GET'):
if request_type == 'GET':
strUrl = "?operation=check"
res = self.Get(strUrl)
elif request_type == 'POST':
strPost = '{"operation": "check"}'
res = self.Post(strPost)
return res
def load(self, db_name, request_type='GET'):
if request_type == 'GET':
strUrl = "?operation=load&db_name=" + db_name + "&username=" + self.username + "&password=" + self.password
res = self.Get(strUrl)
elif request_type == 'POST':
strPost = '{"operation": "load", "db_name": "' + db_name + '", "username": "' + self.username + '", "password": "' + self.password + '"}'
res = self.Post(strPost)
return res
def monitor(self, db_name, request_type='GET'):
if request_type == 'GET':
strUrl = "?operation=monitor&db_name=" + db_name + "&username=" + self.username + "&password=" + self.password
res = self.Get(strUrl)
elif request_type == 'POST':
strPost = '{"operation": "monitor", "db_name": "' + db_name + '", "username": "' + self.username + '", "password": "' + self.password + '"}'
res = self.Post(strPost)
return res
def unload(self, db_name, request_type='GET'):
if request_type == 'GET':
strUrl = "?operation=unload&db_name=" + db_name + "&username=" + self.username + "&password=" + self.password
res = self.Get(strUrl)
elif request_type == 'POST':
strPost = '{"operation": "unload", "db_name": "' + db_name + '", "username": "' + self.username + '", "password": "' + self.password + '"}'
res = self.Post(strPost)
return res
def drop(self, db_name, is_backup, request_type='GET'):
if request_type == 'GET':
if is_backup:
strUrl = "?operation=drop&db_name=" + db_name + "&username=" + self.username + "&password=" + self.password + "&is_backup=true"
else:
strUrl = "?operation=drop&db_name=" + db_name + "&username=" + self.username + "&password=" + self.password + "&is_backup=false"
res = self.Get(strUrl)
elif request_type == 'POST':
if is_backup:
strPost = '{"operation": "drop", "db_name": "' + db_name + '", "username": "' + self.username + '", "password": "' + self.password + '", "is_backup": "true"}'
else:
strPost = '{"operation": "drop", "db_name": "' + db_name + '", "username": "' + self.username + '", "password": "' + self.password + '", "is_backup": "false"}'
res = self.Post(strPost)
return res
def show(self, request_type='GET'):
if request_type == 'GET':
strUrl = "?operation=show&username=" + self.username + "&password=" + self.password
res = self.Get(strUrl)
elif request_type == 'POST':
strPost = '{"operation": "show", "username": "' + self.username + '", "password": "' + self.password + '"}'
res = self.Post(strPost)
return res
def usermanage(self, type, op_username, op_password, request_type='GET'):
if request_type == 'GET':
strUrl = "?operation=usermanage&type=" + type + "&username=" + self.username + "&password=" + self.password + "&op_username=" + op_username + "&op_password=" + op_password
res = self.Get(strUrl)
elif request_type == 'POST':
strPost = '{"operation": "usermanage", "type": "' + type + '", "username": "' + self.username + '", "password": "' + self.password + '", "op_username": "' + op_username + '", "op_password": "' + op_password + '"}'
res = self.Post(strPost)
return res
def showuser(self, request_type='GET'):
if request_type == 'GET':
strUrl = "?operation=showuser&username=" + self.username + "&password=" + self.password
res = self.Get(strUrl)
elif request_type == 'POST':
strPost = '{"operation": "showuser", "username": "' + self.username + '", "password": "' + self.password + '"}'
res = self.Post(strPost)
return res
def userprivilegemanage(self, type, op_username, privileges, db_name, request_type='GET'):
if request_type == 'GET':
strUrl = "?operation=userprivilegemanage&type=" + type + "&username=" + self.username + "&password=" + self.password + "&op_username=" + op_username + "&privileges=" + privileges + "&db_name=" + db_name
res = self.Get(strUrl)
elif request_type == 'POST':
strPost = '{"operation": "userprivilegemanage", "type": "' + type + '", "username": "' + self.username + '", "password": "' + self.password + '", "op_username": "' + op_username + '", "privileges": "' + privileges + '", "db_name": "' + db_name + '"}'
res = self.Post(strPost)
return res
def backup(self, db_name, backup_path, request_type='GET'):
if request_type == 'GET':
strUrl = "?operation=backup&username=" + self.username + "&password=" + self.password + "&db_name=" + db_name + "&backup_path=" + backup_path
res = self.Get(strUrl)
elif request_type == 'POST':
strPost = '{"operation": "backup", "username": "' + self.username + '", "password": "' + self.password + '", "db_name": "' + db_name + '", "backup_path": "' + backup_path + '"}'
res = self.Post(strPost)
return res
def restore(self, db_name, backup_path, request_type='GET'):
if request_type == 'GET':
strUrl = "?operation=restore&username=" + self.username + "&password=" + self.password + "&db_name=" + db_name + "&backup_path=" + backup_path
res = self.Get(strUrl)
elif request_type == 'POST':
strPost = '{"operation": "restore", "username": "' + self.username + '", "password": "' + self.password + '", "db_name": "' + db_name + '", "backup_path": "' + backup_path + '"}'
res = self.Post(strPost)
return res
def query(self, db_name, format, sparql, request_type='GET'):
if request_type == 'GET':
strUrl = "?operation=query&username=" + self.username + "&password=" + self.password + "&db_name=" + db_name + "&format=" + format + "&sparql=" + sparql
res = self.Get(strUrl)
elif request_type == 'POST':
strPost = '{"operation": "query", "username": "' + self.username + '", "password": "' + self.password + '", "db_name": "' + db_name + '", "format": "' + format + '", "sparql": "' + sparql + '"}'
res = self.Post(strPost)
return res
def query_opt(self, db_name, format, sparql, plan, request_type='GET'):
if request_type == 'GET':
strUrl = "?operation=query&username=" + self.username + "&password=" + self.password + "&db_name=" + db_name + "&format=" + format + "&sparql=" + sparql + "&plan=" + plan
res = self.Get(strUrl)
elif request_type == 'POST':
strPost = '{"operation": "query", "username": "' + self.username + '", "password": "' + self.password + '", "db_name": "' + db_name + '", "format": "' + format + '", "sparql": "' + sparql + '", "plan": "' + plan + '"}'
res = self.Post(strPost)
return res
def fquery(self, db_name, format, sparql, filename, request_type='GET'):
if request_type == 'GET':
strUrl = "?operation=query&username=" + self.username + "&password=" + self.password + "&db_name=" + db_name + "&format=" + format + "&sparql=" + sparql
self.fGet(strUrl, filename)
elif request_type == 'POST':
strPost = '{"operation": "query", "username": "' + self.username + '", "password": "' + self.password + '", "db_name": "' + db_name + '", "format": "' + format + '", "sparql": "' + sparql + '"}'
self.fPost(strPost, filename)
return
def exportDB(self, db_name, db_path, request_type='GET'):
if request_type == 'GET':
strUrl = "?operation=export&username=" + self.username + "&password=" + self.password + "&db_name=" + db_name + "&db_path=" + db_path
res = self.Get(strUrl)
elif request_type == 'POST':
strPost = '{"operation": "export", "username": "' + self.username + '", "password": "' + self.password + '", "db_name": "' + db_name + '", "db_path": "' + db_path + '"}'
res = self.Post(strPost)
return res
def login(self, request_type='GET'):
if request_type == 'GET':
strUrl = "?operation=login&username=" + self.username + "&password=" + self.password
res = self.Get(strUrl)
elif request_type == 'POST':
strPost = '{"operation": "login", "username": "' + self.username + '", "password": "' + self.password + '"}'
res = self.Post(strPost)
return res
def begin(self, db_name, isolevel, request_type='GET'):
if request_type == 'GET':
strUrl = "?operation=begin&username=" + self.username + "&password=" + self.password + "&db_name=" + db_name + "&isolevel=" + isolevel
res = self.Get(strUrl)
elif request_type == 'POST':
strPost = '{"operation": "begin", "username": "' + self.username + '", "password": "' + self.password + '", "db_name": "' + db_name + '", "isolevel": "' + isolevel + '"}'
res = self.Post(strPost)
return res
def tquery(self, db_name, tid, sparql, request_type='GET'):
if request_type == 'GET':
strUrl = "?operation=tquery&username=" + self.username + "&password=" + self.password + "&db_name=" + db_name + "&tid=" + tid + "&sparql=" + sparql
res = self.Get(strUrl)
elif request_type == 'POST':
strPost = '{"operation": "tquery", "username": "' + self.username + '", "password": "' + self.password + '", "db_name": "' + db_name + '", "tid": "' + tid + '", "sparql": "' + sparql + '"}'
res = self.Post(strPost)
return res
def commit(self, db_name, tid, request_type='GET'):
if request_type == 'GET':
strUrl = "?operation=commit&username=" + self.username + "&password=" + self.password + "&db_name=" + db_name + "&tid=" + tid
res = self.Get(strUrl)
elif request_type == 'POST':
strPost = '{"operation": "commit", "username": "' + self.username + '", "password": "' + self.password + '", "db_name": "' + db_name + '", "tid": "' + tid + '"}'
res = self.Post(strPost)
return res
def rollback(self, db_name, tid, request_type='GET'):
if request_type == 'GET':
strUrl = "?operation=rollback&username=" + self.username + "&password=" + self.password + "&db_name=" + db_name + "&tid=" + tid
res = self.Get(strUrl)
elif request_type == 'POST':
strPost = '{"operation": "rollback", "username": "' + self.username + '", "password": "' + self.password + '", "db_name": "' + db_name + '", "tid": "' + tid + '"}'
res = self.Post(strPost)
return res
def getTransLog(self, request_type='GET'):
if request_type == 'GET':
strUrl = "?operation=txnlog&username=" + self.username + "&password=" + self.password
res = self.Get(strUrl)
elif request_type == 'POST':
strPost = '{"operation": "txnlog", "username": "' + self.username + '", "password": "' + self.password + '"}'
res = self.Post(strPost)
return res
def checkpoint(self, db_name, request_type='GET'):
if request_type == 'GET':
strUrl = "?operation=checkpoint&username=" + self.username + "&password=" + self.password + "&db_name=" + db_name
res = self.Get(strUrl)
elif request_type == 'POST':
strPost = '{"operation": "checkpoint", "username": "' + self.username + '", "password": "' + self.password + '", "db_name": "' + db_name + '"}'
res = self.Post(strPost)
return res