-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
87 lines (60 loc) · 1.88 KB
/
utils.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
import requests
from base64 import b64encode
from flask import json
from threading import Lock
from time import time as now
class BearerAuth:
def __init__(self, token):
self.token = token
def __call__(self, r):
r.headers['Authorization'] = "Bearer " + self.token
return r
class LockedDict:
def __init__(self, val = {}):
self.val = val
self.lock = Lock()
def __getitem__(self, key):
with self.lock:
return self.val.get(key)
def __setitem__(self, key, v):
with self.lock:
self.val[key] = v
def __delitem__(self, key):
with self.lock:
del self.val[key]
CACHE_TIMEOUT = 60 # 1 minute
class LockedCache(LockedDict):
def __getitem__(self, key):
with self.lock:
if key not in self.val: return None
return self.val[key]['img']
def __setitem__(self, key, v):
with self.lock:
curtime = now()
keys = [k for k in self.val if curtime - self.val[k]['time'] > CACHE_TIMEOUT]
for k in keys:
del self.val[k]
self.val[key] = dict(img = v, time = now())
# LockedDict but the dict is backed by a JSON file
class LockedJSON(LockedDict):
def __init__(self, filename):
self.filename = filename
super().__init__(json.load(open(filename)))
def __setitem__(self, key, v):
with self.lock:
self.val[key] = v
with open(self.filename, "w") as f:
f.write(json.dumps(self.val))
def __delitem__(self, key):
with self.lock:
del self.val[key]
with open(self.filename, "w") as f:
f.write(json.dumps(self.val))
def latexify(texstr):
# Mathoid server
r = requests.post("http://localhost:10044/png", data = dict(q = texstr))
if r.status_code == 400:
raise RuntimeError(r.json()['error'])
return r.content
img_cache = LockedCache()
users = LockedJSON("../users.json")