-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathweb.py
104 lines (75 loc) · 2.46 KB
/
web.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
from flask import Flask, send_from_directory, request
from flask.json import jsonify
from os import path, remove
from glob import glob
import json
import patch_lib
DIR = dir_path = path.dirname(path.realpath(__file__))
app = Flask(__name__)
def _get_pathfile(name):
return path.join(DIR, 'files', name + '.json')
def _load_file(name):
try:
with open(_get_pathfile(name)) as fp:
return json.load(fp)
except FileNotFoundError:
return jsonify(error='file not found'), 404
except IOError:
return jsonify(error='problem with read file'), 500
except ValueError as exp:
return jsonify(error=str(exp)), 500
def _write_file(name, data):
with open(_get_pathfile(name), 'w') as fp:
json.dump(data, fp)
def _remove_file(name):
try:
remove(_get_pathfile(name))
except FileNotFoundError:
return jsonify(error='file not found'), 404
except IOError:
return jsonify(error='problem with removing file'), 500
else:
return None
@app.route('/')
def index():
return send_from_directory('templates', 'index.html')
@app.route('/documents/', methods=['GET'])
def get_all_documents():
files = glob(path.join(DIR, 'files', '*.json'))
documents = [path.basename(filepath) for filepath in files]
return jsonify(documents)
@app.route('/documents/<name>.json', methods=['GET'])
def get_document(name):
data = _load_file(name)
if isinstance(data, tuple): # error
return data
return jsonify(data)
@app.route('/documents/<name>.json', methods=['PUT'])
def put_document(name):
try:
data = json.loads(request.data)
except ValueError as exp:
return jsonify(error=str(exp)), 400
_write_file(name, data)
return jsonify(data)
@app.route('/documents/<name>.json', methods=['PATCH'])
def patch_document(name):
data = _load_file(name)
if isinstance(data, tuple): # error
return data
try:
commands = json.loads(request.data)
except ValueError as exp:
return jsonify(error=str(exp)), 400
try:
new_data = patch_lib.patch_document(data, commands)
except patch_lib.PatchError as exp:
return jsonify(error=str(exp)), 400
_write_file(name, new_data)
return jsonify(new_data)
@app.route('/documents/<name>.json', methods=['DELETE'])
def delete_document(name):
data = _remove_file(name)
if isinstance(data, tuple): # error
return data
return "", 204