cleaned up the code
This commit is contained in:
@@ -12,10 +12,11 @@ import socket
|
||||
import os
|
||||
import sys
|
||||
import json
|
||||
revfrom urllib.parse import urlparse, parse_qs
|
||||
from urllib.parse import urlparse, parse_qs
|
||||
|
||||
PORT = 5044
|
||||
ROOT = os.path.dirname(os.path.abspath(__file__))
|
||||
ROOT = os.path.dirname(os.path.abspath(__file__))
|
||||
DATA_DIR = os.path.join(ROOT, 'viewer', 'data')
|
||||
|
||||
|
||||
class QuietHandler(http.server.SimpleHTTPRequestHandler):
|
||||
@@ -53,7 +54,8 @@ class QuietHandler(http.server.SimpleHTTPRequestHandler):
|
||||
return
|
||||
|
||||
if parsed.path == '/save-flags':
|
||||
dest = os.path.join(ROOT, 'flags-' + diary + '.json')
|
||||
os.makedirs(DATA_DIR, exist_ok=True)
|
||||
dest = os.path.join(DATA_DIR, 'flags-' + diary + '.json')
|
||||
with open(dest, 'w', encoding='utf-8') as f:
|
||||
json.dump(data, f, indent=2)
|
||||
self.send_response(200)
|
||||
@@ -62,7 +64,8 @@ class QuietHandler(http.server.SimpleHTTPRequestHandler):
|
||||
self.wfile.write(b'{"ok":true}')
|
||||
|
||||
elif parsed.path == '/save-corrections':
|
||||
dest = os.path.join(ROOT, 'corrections-' + diary + '.json')
|
||||
os.makedirs(DATA_DIR, exist_ok=True)
|
||||
dest = os.path.join(DATA_DIR, 'corrections-' + diary + '.json')
|
||||
existing = {}
|
||||
if os.path.exists(dest):
|
||||
try:
|
||||
@@ -84,7 +87,8 @@ class QuietHandler(http.server.SimpleHTTPRequestHandler):
|
||||
self.wfile.write(b'{"ok":true}')
|
||||
|
||||
elif parsed.path == '/save-pass2-flags':
|
||||
dest = os.path.join(ROOT, 'pass2_flags-' + diary + '.json')
|
||||
os.makedirs(DATA_DIR, exist_ok=True)
|
||||
dest = os.path.join(DATA_DIR, 'pass2_flags-' + diary + '.json')
|
||||
try:
|
||||
with open(dest, 'w', encoding='utf-8') as f:
|
||||
json.dump(data, f, indent=2)
|
||||
@@ -98,7 +102,7 @@ class QuietHandler(http.server.SimpleHTTPRequestHandler):
|
||||
|
||||
elif parsed.path == '/clear-all-data':
|
||||
for prefix in ['flags-', 'corrections-', 'pass2_flags-']:
|
||||
fpath = os.path.join(ROOT, prefix + diary + '.json')
|
||||
fpath = os.path.join(DATA_DIR, prefix + diary + '.json')
|
||||
if os.path.exists(fpath):
|
||||
try:
|
||||
os.remove(fpath)
|
||||
@@ -124,7 +128,7 @@ def start_server():
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
url = 'http://localhost:{}/p44-landing.html'.format(PORT)
|
||||
url = 'http://localhost:{}/viewer/p44-landing.html'.format(PORT)
|
||||
|
||||
if port_in_use(PORT):
|
||||
print('\n P44 OCR Viewer (server already running)')
|
||||
|
||||
Reference in New Issue
Block a user