-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
301 lines (256 loc) · 7.99 KB
/
main.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# Copyright (c) 2018, Shimoda <kuri65536 at hotmail dot com>
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
from typing import (Any, AsyncGenerator, Dict,
Iterable, Iterator, Optional, Text, Tuple)
import pickle
import json
from leveldb import LevelDB # type: ignore
from quart import Quart, abort, render_template, request # type: ignore
import configs as cfg
Any, AsyncGenerator, Dict, Iterable, Iterator, Optional, Text, Tuple
app = Quart(__name__)
db = None # type: Optional[LevelDB]
def json_dbcfg(): # {{{1
# type: () -> Text
ret = dict(key_encoding=cfg.key_encoding,
val_encoding=cfg.val_encoding,
compression=cfg.compression,
cache_size=cfg.cache_size,
create_if_missing=cfg.create_if_missing,
error_if_exists=cfg.error_if_exists)
return json.dumps(ret)
def valdec_text(src): # {{{1
# type: (bytes) -> Text
if cfg.val_encoding.lower().endswith("latin-1"):
enc = "latin-1"
else:
enc = "utf-8"
return src.decode(enc)
def valdec_bytes(src): # {{{1
# type: (bytes) -> Any
if cfg.val_encoding.lower().startswith("json"):
_ret = valdec_text(src)
try:
ret = json.loads(_ret)
except json.decoder.JSONDecodeError:
ret = _ret
return ret
elif cfg.val_encoding.lower().startswith("raw"):
pass
elif cfg.val_encoding.lower().startswith("pickle"):
ret = pickle.loads(src)
return ret
return valdec_text(src)
def valenc_text(src): # {{{1
# type: (Text) -> bytes
if cfg.val_encoding.lower().endswith("latin-1"):
enc = "latin-1"
else:
enc = "utf-8"
return src.encode(enc)
def valenc_raw(src): # {{{1
# type: (Text) -> bytes
return valenc_text(src)
def valenc_json(src): # {{{1
# type: (Text) -> bytes
try:
_src = json.loads(src)
except:
_src = src
return valenc_text(_src)
def valenc_pickle(src): # {{{1
# type: (Text) -> bytes
try:
dat = valenc_text(src)
return pickle.pickle(dat) # type: ignore
except:
pass
return valenc_text(src)
@app.route('/conn.local')
async def conn_local():
# type: () -> Text
global db
path = request.args.get("dir")
path = path.replace("%2F", "/")
db = LevelDB(path, cfg.create_if_missing)
print("LevelDB opened: {}".format(db))
return json_dbcfg()
@app.route('/conn.network')
async def conn_network():
# type: () -> Text
path = request.args.get("url")
if path == "":
abort(404)
# TODO: implement, I do not have server of LevelDB.
ret = dict(key_encoding="utf-8",
val_encoding="json",
cache_size=2,
create_if_missing=True,
error_if_exists=False,
compression=False)
print("LevelDB opened(as network): ...")
return json.dumps(ret)
@app.route('/settings')
async def settings(): # route {{{1
# type: () -> Text
if request.args.get("query", "false") == "true":
pass
else:
_dat = request.args.get("data", "")
dat = json.loads(_dat)
ke = dat["key_encoding"]
ve = dat["val_encoding"]
cs = dat["cache_size"]
cm = dat["compression"]
ci = dat["create_if_missing"]
ei = dat["error_if_exists"]
if "" in (ke, ve, cs, cm, ci, ei):
abort(404)
return ""
cm = True if cm == "on" else False
ci = True if ci == "on" else False
ei = True if ei == "on" else False
# TODO: error check more strictly
cfg.key_encoding = ke
cfg.val_encoding = ve
cfg.compression = cm
cfg.cache_size = cs
cfg.create_if_missing = ci
cfg.error_if_exists = ei
return json_dbcfg()
@app.route('/get')
async def get_record(): # route {{{1
# type: () -> Text
global db
if db is None:
abort(404)
return ""
k = request.args.get("key", "")
if k == "":
abort(404)
return ""
_k = k.encode(cfg.key_encoding)
try:
v = db.Get(_k)
except KeyError:
abort(404)
return ""
_v = valdec_bytes(v)
ret = dict(k=k, v=str(_v))
print("LevelDB put: {}".format(ret))
return json.dumps(ret)
def query_fetch_args(args): # {{{1
# type: (Dict[Text, Text]) -> Tuple[Text, Text, int, bool]
u = str(args.get("u", ""))
l = str(args.get("l", ""))
_f_rev = str(args.get("r", ""))
n = args.get("n", "100")
if l == "":
l = chr(255)
try:
_n = int(n)
except ValueError:
_n = 100
f_rev = True if _f_rev == "on" else False
return u, l, _n, f_rev
@app.route('/put')
async def put_record(): # route {{{1
# type: () -> Text
global db
if db is None:
abort(404)
return ""
k = request.args.get("key", "")
v = request.args.get("val", "")
if k == "" or v == "":
abort(404)
return ""
if cfg.val_encoding.lower().startswith("json"):
_v = valenc_json(v)
elif cfg.val_encoding.lower().startswith("pickle"):
_v = valenc_pickle(v)
else:
_v = valenc_text(v)
_k = k.encode(cfg.key_encoding)
db.Put(_k, _v)
ret = dict(k=k, v=valdec_text(_v))
print("LevelDB put: {}".format(ret))
return json.dumps(ret)
@app.route('/query_part')
async def query_part(): # route {{{1
# type: () -> Text
'''TODO: hold iterator, return the token, continue iterator with token.
'''
global db
if db is None:
abort(404)
return ""
u, l, _n, f_rev = query_fetch_args(request.args)
ret = '{"u": "' + u + '", "l": "' + l + '", "n":' + str(_n) + \
', "records": ['
i = 0
_u = u.encode(cfg.key_encoding)
_l = l.encode(cfg.key_encoding)
for k in db.RangeIter(_u, _l, include_value=False, reverse=f_rev):
assert isinstance(k, bytearray), "??? key type is {}".format(type(k))
i += 1
if i > _n:
continue
if i > 1:
ret += ", "
ret += '{"key": "' + k.decode(cfg.key_encoding) + '"}'
ret += "]}"
print(ret)
return ret
@app.route('/query_stream')
async def query_stream(): # route {{{1
# type: () -> Tuple[AsyncGenerator[bytes, None], int, Dict[Text, Text]]
global db
u, l, n, f_rev = query_fetch_args(request.args)
async def stream(u, l, n, f_rev):
# type: (Text, Text, int, bool) -> AsyncGenerator[bytes, None]
if db is None:
return
ret = ('{"u": "' + u + '", "l": "' + l + '", "n":' + str(n) +
', "records": [')
yield ret.encode("utf-8")
i = 0
_u = u.encode(cfg.key_encoding)
_l = l.encode(cfg.key_encoding)
for k, v in db.RangeIter(_u, _l, reverse=f_rev):
i += 1
if i > n:
break
if i > 1:
yield ",".encode("utf-8")
ret = '{"key": "' + k.decode(cfg.key_encoding) + '", "val": '
_v = valdec_bytes(v)
ret += '"{}"}'.format(str(_v))
yield ret.encode("utf-8")
yield "]}".encode("utf-8")
ret = 200 if db is not None else 500
return stream(u, l, n, f_rev), ret, {'X-Something': 'value'}
@app.route('/')
async def root(): # type: ignore # mypy error: syntax error ? {{{1
return await render_template("index.html")
@app.route('/readme')
async def readme(): # {{{1
# type: () -> Text
import markdown # type: ignore # no type stub
ret = ""
with open("templates/index.html", "rt") as fp:
for line in fp:
ret += line
if "<body" in line:
break
with open("README.md", "rt") as fp:
src = fp.read() # TODO: await?
ret += markdown.markdown(src) # TODO: await?
ret += "</body> </html>"
return ret
app.run()
# vi: ft=python:et:ts=4:nowrap:fdm=marker