X-Git-Url: https://plomlompom.com/repos/?a=blobdiff_plain;f=plomlib.py;h=a94eb54af2646ed1079c5498af0cb0bb0aa59775;hb=53b84307929582af2aaeac2247cc57db4a7bedf4;hp=d1b4c4c9a4713cecb84d6c497e7e4f5505e9add0;hpb=e58262d6913522cf57d5317fbf0398e2d56b29a0;p=misc diff --git a/plomlib.py b/plomlib.py index d1b4c4c..a94eb54 100644 --- a/plomlib.py +++ b/plomlib.py @@ -1,5 +1,7 @@ import os -from http.server import BaseHTTPRequestHandler +from http.server import BaseHTTPRequestHandler +from http.cookies import SimpleCookie +import json class PlomException(Exception): @@ -37,6 +39,7 @@ class PlomDB: mtimes_to_paths = {} for path in [path for path in os.listdir(os.path.dirname(bak_prefix)) if path.startswith(os.path.basename(bak_prefix))]: + path = os.path.dirname(bak_prefix) + f'/{path}' mod_time = os.path.getmtime(path) print(f'DEBUG pre-exists: {path} {mod_time}') mtimes_to_paths[str(datetime.fromtimestamp(mod_time))] = path @@ -49,10 +52,10 @@ class PlomDB: # timedelta, keep the newest file that's older ages_to_keep = [timedelta(minutes=4**i) for i in range(0, 8)] print(f'DEBUG ages_to_keep: {ages_to_keep}') - now = datetime.now() + now = datetime.now() to_save = {} for age in ages_to_keep: - limit = now - age + limit = now - age for mtime in reversed(sorted(mtimes_to_paths.keys())): print(f'DEBUG checking if {mtime} < {limit} ({now} - {age})') if datetime.strptime(mtime, '%Y-%m-%d %H:%M:%S.%f') < limit: @@ -74,7 +77,7 @@ class PlomDB: shutil.move(source, target) i += 1 - # put copy of current state at end of bak list + # put copy of current state at end of bak list print(f'DEBUG saving current state to {bak_prefix}{i}') shutil.copy(self.db_file, f'{bak_prefix}{i}') @@ -86,11 +89,11 @@ class PlomDB: self.unlock() -class PlomHandler(BaseHTTPRequestHandler): +class PlomHandler(BaseHTTPRequestHandler): homepage = '/' html_head = '\n\n' html_foot = '\n' - + def fail_400(self, e): self.send_HTML(f'ERROR: {e}', 400) @@ -98,8 +101,39 @@ class PlomHandler(BaseHTTPRequestHandler): self.send_code_and_headers(code, [('Content-type', 'text/html')]) self.wfile.write(bytes(f'{self.html_head}\n{html}\n{self.html_foot}', 'utf-8')) + def ensure_cookies_to_set(self): + if not hasattr(self, 'cookies_to_set'): + self.cookies_to_set = [] + + def set_cookie(self, cookie_name, cookie_path, cookie_db): + self.ensure_cookies_to_set() + cookie = SimpleCookie() + cookie[cookie_name] = json.dumps(cookie_db) + cookie[cookie_name]['path'] = cookie_path + self.cookies_to_set += [cookie] + + def unset_cookie(self, cookie_name, cookie_path): + self.ensure_cookies_to_set() + cookie = SimpleCookie() + cookie[cookie_name] = '' + cookie[cookie_name]['path'] = cookie_path + cookie[cookie_name]['expires'] = 'Thu, 01 Jan 1970 00:00:00 GMT' + self.cookies_to_set += [cookie] + + def get_cookie_db(self, cookie_name): + cookie_db = {} + if 'Cookie' in self.headers: + cookie = SimpleCookie(self.headers['Cookie']) + if cookie_name in cookie: + cookie_db = json.loads(cookie[cookie_name].value) + return cookie_db + def send_code_and_headers(self, code, headers=[]): + self.ensure_cookies_to_set() self.send_response(code) + for cookie in self.cookies_to_set: + for morsel in cookie.values(): + self.send_header('Set-Cookie', morsel.OutputString()) for fieldname, content in headers: self.send_header(fieldname, content) self.end_headers() @@ -109,7 +143,7 @@ class PlomHandler(BaseHTTPRequestHandler): def try_do(self, do_method): try: - do_method() + do_method() except PlomException as e: self.fail_400(e)