Skip to content

Commit 3bc05b6

Browse files
committed
fix for version 10
1 parent e002691 commit 3bc05b6

File tree

2 files changed

+41
-37
lines changed

2 files changed

+41
-37
lines changed

Dockerfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ WORKDIR /opt
66

77
RUN apt-get update && \
88
apt-get install -y python3 python3-pip git && \
9-
pip3 install flask python-Levenshtein bibtexparser GitPython
9+
pip3 install flask python-Levenshtein bibtexparser GitPython && \
10+
git config --global user.email "bib@to.ol" && \
11+
git config --global user.name "BibTool"
1012

1113
ENTRYPOINT ["python3", "server.py", "/data/", "main.bib"]
1214

server.py

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,32 @@
66
import json
77
import sys
88

9+
VERSION = 10
10+
911
app = Flask(__name__)
1012
tokens = True
1113
no_commit = False
1214

1315
token_db = {
14-
"test": {"search": True, "read": True, "write": True, "delete": True}
16+
"test": {"search": True, "read": True, "write": True, "delete": True}
1517
}
1618

1719
def check_token(token, operation):
1820
if not tokens:
1921
return True
20-
22+
2123
if not token in token_db:
2224
return False
2325
if not operation in token_db[token]:
2426
return False
2527
return token_db[token][operation]
26-
28+
2729

2830
def entry_to_bibtex(entry):
2931
newdb = bibtexparser.bibdatabase.BibDatabase()
3032
newdb.entries = [ entry ]
3133
return bibtexparser.dumps(newdb)
32-
34+
3335

3436
def get_duplicates(entry):
3537
dups = []
@@ -46,8 +48,8 @@ def get_duplicates(entry):
4648
if (exact and sorted(e.keys()) != sorted(entry.keys())) or ((dist < max(5, length * 0.1) or exact) and dist > 0):
4749
dups.append((dist, entry["ID"], e))
4850
return dups
49-
50-
51+
52+
5153
def entry_by_key(key):
5254
for entry in bib_database.entries:
5355
if entry["ID"] == key:
@@ -114,11 +116,11 @@ def get_bibfile():
114116
return "Invalid request"
115117
if not check_token(request.json["token"], "read"):
116118
return "Access denied!"
117-
119+
118120
bib = ""
119121
for entry in request.json["entries"]:
120122
bib += entry_to_bibtex(entry_by_key(entry)) + "\n"
121-
123+
122124
return bib
123125

124126

@@ -128,11 +130,11 @@ def get_bibfile_as_json():
128130
return "Invalid request"
129131
if not check_token(request.json["token"], "read"):
130132
return jsonify({"success": False, "reason": "access_denied", "message": "Your token does not grant read access."})
131-
133+
132134
bib = []
133135
for entry in request.json["entries"]:
134136
bib.append(entry_by_key(entry))
135-
137+
136138
return jsonify(bib)
137139

138140

@@ -141,7 +143,7 @@ def get_bibfile_as_json():
141143
def suggest_entry(key, token):
142144
if not check_token(token, "search"):
143145
return jsonify({"success": False, "reason": "access_denied"})
144-
146+
145147
entry = entry_by_key(key)
146148
if not entry:
147149
entries = []
@@ -150,7 +152,7 @@ def suggest_entry(key, token):
150152
if key.lower() in entry["ID"].lower() or dist == 0:
151153
entries.append((1, entry))
152154
continue
153-
if dist < 5:
155+
if dist < 5:
154156
entries.append((1-dist/100.0, entry))
155157
continue
156158
common_prefix = 0
@@ -162,7 +164,7 @@ def suggest_entry(key, token):
162164
entries.append((common_prefix/float(max(len(entry["ID"]), len(key))), entry))
163165
else:
164166
entries = [ (1, entry) ]
165-
167+
166168
top = sorted(entries, key=lambda x: x[0], reverse=True)
167169
return jsonify({"success": True, "entries": top[:5]})
168170

@@ -172,7 +174,7 @@ def suggest_entry(key, token):
172174
def search_entry(query, token):
173175
if not check_token(token, "search"):
174176
return "Access denied!"
175-
177+
176178
query_parts = query.split(" ")
177179
for q in query_parts:
178180
if len(q) < 3:
@@ -190,22 +192,22 @@ def search_entry(query, token):
190192
if was_found:
191193
entries.append((entry_to_bibtex(entry)))
192194
return "\n".join(list(set(entries)))
193-
195+
194196

195197
@app.route("/v1/entry/<string:key>", methods=["POST"])
196198
def add_entry(key):
197199
if not request.json or not "entry" in request.json or not "token" in request.json:
198200
return jsonify({"success": False, "reason": "missing_entry"})
199201
if not check_token(request.json["token"], "write"):
200202
return jsonify({"success": False, "reason": "access_denied", "message": "Your token does not allow adding new bibliography entries."})
201-
203+
202204
if "ID" not in request.json["entry"]:
203205
request.json["entry"]["ID"] = key
204-
206+
205207
existing = entry_by_key(request.json["entry"]["ID"])
206208
if existing:
207209
return jsonify({"success": False, "reason": "exists", "entry": existing})
208-
210+
209211
bib_database.entries.append(request.json["entry"])
210212
save_bib("Added %s" % request.json["entry"]["ID"], request.json["token"])
211213
return jsonify({"success": True})
@@ -217,38 +219,38 @@ def replace_entry(key):
217219
return jsonify({"success": False, "reason": "missing_entry"})
218220
if not check_token(request.json["token"], "write"):
219221
return jsonify({"success": False, "reason": "access_denied", "message": "Your token does not allow changing bibliography entries."})
220-
222+
221223
for (idx, entry) in enumerate(bib_database.entries):
222224
if entry["ID"] == key:
223225
bib_database.entries[idx] = request.json["entry"]
224226
save_bib("Changed %s" % key, request.json["token"])
225227
return jsonify({"success": True})
226-
228+
227229
return jsonify({"success": False, "reason": "not_found"})
228-
229-
230+
231+
230232
@app.route("/v1/entry/<string:key>", defaults={"token": None}, methods=["DELETE"])
231233
@app.route("/v1/entry/<string:key>/<string:token>", methods=["DELETE"])
232234
def remove_entry(key, token):
233235
if not check_token(token, "delete"):
234236
return jsonify({"success": False, "reasons": "access_denied", "message": "Your token does not allow deleting bibliography entries."})
235-
237+
236238
for (idx, entry) in enumerate(bib_database.entries):
237239
if entry["ID"] == key:
238240
del bib_database.entries[idx]
239241
save_bib("Deleted %s" % key, token)
240242
return jsonify({"success": True})
241-
243+
242244
return jsonify({"success": False, "reason": "not_found"})
243-
245+
244246

245247
@app.route("/v1/update", methods=["POST"])
246248
def add_entries():
247249
if not request.json or not "entries" in request.json or not "token" in request.json:
248250
return jsonify({"success": False, "reason": "missing_entry"})
249251
if not check_token(request.json["token"], "write"):
250252
return jsonify({"success": False, "reason": "access_denied", "message": "Your token does not allow modifying the bibliography. Remove the bib file to get a fresh one from the server"})
251-
253+
252254
dups = []
253255
changes = False
254256
changelog = []
@@ -272,7 +274,7 @@ def add_entries():
272274

273275
if len(dups) > 0:
274276
return jsonify({"success": False, "reason": "duplicate", "entries": dups})
275-
277+
276278
return jsonify({"success": True})
277279

278280

@@ -290,21 +292,21 @@ def sync():
290292

291293
with open(repo_path + "/" + repo_name) as bibtex_file:
292294
bib_database = bibtexparser.load(bibtex_file, parser)
293-
295+
294296
try:
295297
with open(repo_path + "/tokens.json") as tokens:
296298
token_db = json.load(tokens)
297299
except:
298300
tokens = False
299-
301+
300302
return "Synced!"
301303

302304

303305
@app.route("/v1/webhook", methods=["POST"])
304306
def webhook():
305307
if not request.json or not "commits" in request.json:
306308
return jsonify({"success": False, "reason": "missing_entry"})
307-
309+
308310
was_internal = True
309311
for commit in request.json["commits"]:
310312
if "title" in commit and "[BibTool]" not in commit["title"]:
@@ -313,27 +315,27 @@ def webhook():
313315
if "message" in commit and "[BibTool]" not in commit["message"]:
314316
was_internal = False
315317
break
316-
317-
if not was_internal:
318+
319+
if not was_internal:
318320
return sync()
319321
else:
320322
return "OK"
321-
323+
322324

323325
@app.route("/v1/version", methods=["GET"])
324326
def version():
325-
return jsonify({"version": 9, "url": "client.py"})
327+
return jsonify({"version": VERSION, "url": "client.py"})
326328

327329

328330
if __name__ == "__main__":
329331
global repo_path, repo_name
330-
332+
331333
if len(sys.argv) < 3:
332334
print("Usage: %s <repo path> <bib filename>" % sys.argv[0])
333335
sys.exit(1)
334336
repo_path = sys.argv[1]
335337
repo_name = sys.argv[2]
336338

337339
sync()
338-
340+
339341
app.run(debug=False, host='0.0.0.0')

0 commit comments

Comments
 (0)