Make a squash
This commit is contained in:
commit
113992f95b
21 changed files with 3339 additions and 0 deletions
58
web/db.py
Normal file
58
web/db.py
Normal file
|
@ -0,0 +1,58 @@
|
|||
import contextlib
|
||||
import json
|
||||
import pathlib
|
||||
import time
|
||||
|
||||
import click
|
||||
import flask
|
||||
import flask.cli
|
||||
|
||||
def lock(name):
|
||||
lockfile = pathlib.Path(f'{name}.lock')
|
||||
for i in range(5):
|
||||
try:
|
||||
lockfile.symlink_to('/dev/null')
|
||||
return
|
||||
except FileExistsError:
|
||||
time.sleep(1)
|
||||
raise TimeoutError(f'could not lock {name}')
|
||||
|
||||
def unlock(name):
|
||||
lockfile = pathlib.Path(f'{name}.lock')
|
||||
lockfile.unlink(missing_ok=True)
|
||||
|
||||
@contextlib.contextmanager
|
||||
def locked(name):
|
||||
lock(name)
|
||||
try:
|
||||
yield name
|
||||
finally:
|
||||
unlock(name)
|
||||
|
||||
def read(name):
|
||||
with open(f'{name}.json', 'a+', encoding='utf-8') as f:
|
||||
f.seek(0)
|
||||
return json.loads(f.read() or '{}')
|
||||
|
||||
def write(name, data):
|
||||
with open(f'{name}.json', 'w', encoding='utf-8') as f:
|
||||
json.dump(data, f, indent=2)
|
||||
f.close()
|
||||
|
||||
def load(name):
|
||||
with locked(name):
|
||||
return read(name)
|
||||
|
||||
def save(name, data):
|
||||
with locked(name):
|
||||
write(name, data)
|
||||
|
||||
def init_app(app):
|
||||
#app.teardown_appcontext(close_db)
|
||||
app.cli.add_command(init_db)
|
||||
|
||||
@click.command('init-db')
|
||||
@flask.cli.with_appcontext
|
||||
def init_db():
|
||||
"""Clear the existing data and create new tables."""
|
||||
pass # TODO, if needed
|
Loading…
Add table
Add a link
Reference in a new issue