-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
113 lines (83 loc) · 2.28 KB
/
app.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
from time import sleep
from threading import Lock
from flask import Flask, jsonify, request, abort
from bjdb import BJDB
from utils import log
app = Flask(__name__)
db = BJDB('bj.db')
lock = Lock()
# log('a process')
@app.route('/', methods=['GET'])
def tables():
return jsonify(db.tables())
@app.route('/tables/<table>/headers', methods=['GET'])
def headers(table):
return jsonify(db.headers(table))
@app.route('/new', methods=['POST'])
def new_tables():
data = request.get_json()
if data.get('table') and data.get('headers'):
with lock:
db.create_table(data['table'], data['headers'])
return '', 204
else:
return abort(400)
@app.route('/tables/<table>', methods=['POST'])
def insert(table):
data = request.get_json()
try:
with lock:
db.insert(table, data)
return '', 204
except Exception as e:
log(e)
return abort(400)
def make_cond(key_data):
def cond(record):
for k, v in key_data.items():
if (not record.get(k)) or (record[k] != v):
return False
else:
return True
return cond
@app.route('/tables/<table>/search', methods=['GET'])
def search(table):
key_data = request.args
return jsonify(db.search(table, make_cond(key_data)))
@app.route('/tables/<table>', methods=['PUT'])
def update(table):
data = request.get_json()
try:
new_data = data['newdata']
key_data = data['keydata']
with lock:
db.update(table, new_data, make_cond(key_data))
return '', 204
except Exception as e:
log(e)
return abort(400)
@app.route('/tables/<table>', methods=['DELETE'])
def delete(table):
data = request.get_json()
try:
with lock:
db.delete(table, make_cond(data))
return '', 204
except Exception as e:
log(e)
return abort(400)
@app.route('/tables/<table>/all', methods=['GET'])
def all_data(table):
return jsonify(db.all(table))
@app.route('/merge', methods=['POST'])
def merge():
with lock:
db.merge()
return '', 204
@app.route('/tables/<table>/all', methods=['DELETE'])
def purge(table):
with lock:
db.purge(table)
return '', 204
if __name__ == '__main__':
app.run()