smolcgi: add some utilities and format to PEP8

main
dece 3 years ago
parent 47650deb33
commit 279692d0a7

@ -1,4 +1,5 @@
#!/usr/bin/env python3
"""Wrapper around `check_if_email_exists`."""
import json
import pprint

22
lists

@ -1,28 +1,27 @@
#!/usr/bin/env python3
"""Smol listing manager."""
import smolcgi as scgi
if not (user_hash := scgi.tls_client_hash):
scgi.exit_with_header(60, "You need a certificate to use this app")
import string
import subprocess
from os import getenv
from pathlib import Path
STORAGE_PATH = Path(getenv("GEMINI_LISTS_PATH", "/home/gemini/storage/lists"))
import smolcgi as scgi
scgi.require_client_cert()
STORAGE_PATH = Path("/home/gemini/storage/lists")
if not STORAGE_PATH.exists():
STORAGE_PATH.mkdir(parents=True)
ALLOWED_LIST_NAME_CHARS = string.ascii_lowercase + string.digits + "_"
def get_user_dir():
user_dir = STORAGE_PATH / user_hash[7:]
user_dir = STORAGE_PATH / scgi.tls_client_hash[7:]
if not user_dir.is_dir():
user_dir.mkdir()
return user_dir
def show_lists():
user_dir = get_user_dir()
scgi.header(20, "text/gemini")
@ -32,6 +31,7 @@ def show_lists():
print(f"=> {scgi.script_name}/{child.name} 📄 {child.name}")
print(f"=> {scgi.script_name}/_/create ✨ Create a new list")
def show_list(list_name):
list_file = get_user_dir() / list_name
try:
@ -46,6 +46,7 @@ def show_list(list_name):
print(f"=> {scgi.script_name}/{list_name}/add Append item")
print(f"=> {scgi.script_name}/{list_name}/pop Pop first item")
def process_action(list_name, action):
if list_name == "_" and action == "create":
process_creation()
@ -56,6 +57,7 @@ def process_action(list_name, action):
else:
scgi.not_found()
def process_creation():
if not (new_list_name := scgi.query_string_dec):
meta = "List name (allowed chars: lowercase, digits, underscore)"
@ -68,6 +70,7 @@ def process_creation():
list_file.touch(mode=0o660)
redirect_to_list(new_list_name)
def process_add(list_name):
if not (item := scgi.query_string_dec):
scgi.require_input("Enter your item")
@ -84,14 +87,17 @@ def process_add(list_name):
else:
redirect_to_list(list_name)
def process_pop(list_name):
list_file = get_user_dir() / list_name
subprocess.run(["sed", "-i", "1d", str(list_file)])
redirect_to_list(list_name)
def redirect_to_list(list_name):
scgi.redirect_temp(scgi.script_name + "/" + list_name)
path_components = scgi.path_info.lstrip("/").split("/", maxsplit=1)
list_name = path_components[0]
action = path_components[1] if len(path_components) > 1 else ''

@ -1,9 +1,14 @@
"""Smol CGI utilities."""
from os import environ
from pathlib import Path
from urllib.parse import unquote
def getenv(name):
return environ.get(name, "")
gateway_interface = getenv("GATEWAY_INTERFACE")
remote_addr = getenv("REMOTE_ADDR")
remote_host = getenv("REMOTE_HOST")
@ -40,34 +45,68 @@ cgi_vars = [
"query_string_dec",
]
def get_storage_path():
script_filename = script_name.split("/")[-1]
path = getenv(f"GEMINI_{script_filename.upper()}_STORAGE")
if not path:
path = f"/home/gemini/storage/{script_filename}"
path = Path(path)
if not path.exists():
path.mkdir(parents=True)
return path
def get_user_dir():
user_dir = get_storage_path() / tls_client_hash[7:]
if not user_dir.is_dir():
user_dir.mkdir()
return user_dir
def header(code, meta):
print(f"{code} {meta}", end="\r\n")
def exit_with_header(code, meta):
header(code, meta)
exit()
def require_input(meta):
exit_with_header(10, meta)
def require_input(reason):
exit_with_header(10, reason)
def redirect_temp(url):
exit_with_header(30, url)
def redirect_perm(url):
exit_with_header(31, url)
def temp_error(meta):
exit_with_header(42, meta)
def not_found():
exit_with_header(51, "File not found.")
def temp_error(reason):
exit_with_header(42, reason)
def not_found(reason="Not found"):
exit_with_header(51, reason)
def bad_request(reason="Bad request"):
exit_with_header(59, reason)
def require_client_cert():
if auth_type != "CERTIFICATE":
exit_with_header(60, "You need a certificate to use this app")
def debug():
header(20, "text/plain")
print_env()
exit()
def print_env():
globz = globals()
for key in cgi_vars:
print(f"{key} = {repr(globz[key])}")
def link(path, text=""):
print(f"=> {script_name}{path} {text}")

Loading…
Cancel
Save