2021-08-31 17:52:18 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""Smol listing manager."""
|
|
|
|
|
|
|
|
import string
|
|
|
|
import subprocess
|
|
|
|
from pathlib import Path
|
|
|
|
|
2021-11-25 11:29:02 +01:00
|
|
|
import smolcgi as scgi
|
|
|
|
scgi.require_client_cert()
|
|
|
|
|
|
|
|
STORAGE_PATH = Path("/home/gemini/storage/lists")
|
2021-08-31 17:52:18 +02:00
|
|
|
if not STORAGE_PATH.exists():
|
|
|
|
STORAGE_PATH.mkdir(parents=True)
|
|
|
|
|
|
|
|
ALLOWED_LIST_NAME_CHARS = string.ascii_lowercase + string.digits + "_"
|
|
|
|
|
2021-11-25 11:29:02 +01:00
|
|
|
|
2021-08-31 17:52:18 +02:00
|
|
|
def show_lists():
|
2021-11-27 18:49:15 +01:00
|
|
|
user_dir = scgi.get_user_dir()
|
2021-08-31 17:52:18 +02:00
|
|
|
scgi.header(20, "text/gemini")
|
|
|
|
print("# Lists")
|
|
|
|
for child in user_dir.iterdir():
|
|
|
|
if child.is_file():
|
2021-11-27 18:49:15 +01:00
|
|
|
scgi.link(f"/{child.name}", f"📄 {child.name}")
|
|
|
|
scgi.link("/_/create", "✨ Create a new list")
|
2021-08-31 17:52:18 +02:00
|
|
|
|
2021-11-25 11:29:02 +01:00
|
|
|
|
2021-08-31 17:52:18 +02:00
|
|
|
def show_list(list_name):
|
2021-11-27 18:49:15 +01:00
|
|
|
list_file = scgi.get_user_dir() / list_name
|
2021-08-31 17:52:18 +02:00
|
|
|
try:
|
|
|
|
with open(list_file, "rt") as f:
|
|
|
|
content = f.read()
|
|
|
|
except FileNotFoundError:
|
|
|
|
scgi.not_found()
|
|
|
|
else:
|
|
|
|
scgi.header(20, "text/gemini")
|
|
|
|
print("# " + list_name)
|
|
|
|
print(content)
|
2021-11-27 18:49:15 +01:00
|
|
|
scgi.link(f"/{list_name}/add", "Append item")
|
|
|
|
scgi.link(f"/{list_name}/pop", "Pop first item")
|
|
|
|
scgi.link(f"/{list_name}/clear", "Clear list")
|
|
|
|
scgi.link(f"/{list_name}/delete", "Delete list")
|
2021-11-24 22:12:36 +01:00
|
|
|
|
2021-11-25 11:29:02 +01:00
|
|
|
|
2021-08-31 17:52:18 +02:00
|
|
|
def process_action(list_name, action):
|
2021-11-27 18:49:15 +01:00
|
|
|
if list_name == "_":
|
|
|
|
if action == "create":
|
|
|
|
process_creation()
|
|
|
|
else:
|
|
|
|
scgi.not_found()
|
2021-08-31 17:52:18 +02:00
|
|
|
else:
|
2021-11-27 18:49:15 +01:00
|
|
|
if action == "add":
|
|
|
|
process_add(list_name)
|
|
|
|
elif action == "pop":
|
|
|
|
process_pop(list_name)
|
|
|
|
elif action == "clear":
|
|
|
|
process_clear(list_name)
|
|
|
|
elif action == "delete":
|
|
|
|
process_delete(list_name)
|
|
|
|
else:
|
|
|
|
scgi.not_found()
|
2021-08-31 17:52:18 +02:00
|
|
|
|
2021-11-25 11:29:02 +01:00
|
|
|
|
2021-08-31 17:52:18 +02:00
|
|
|
def process_creation():
|
2021-11-24 22:12:36 +01:00
|
|
|
if not (new_list_name := scgi.query_string_dec):
|
2021-08-31 17:52:18 +02:00
|
|
|
meta = "List name (allowed chars: lowercase, digits, underscore)"
|
|
|
|
scgi.require_input(meta)
|
|
|
|
else:
|
|
|
|
for char in new_list_name:
|
|
|
|
if char not in ALLOWED_LIST_NAME_CHARS:
|
|
|
|
scgi.temp_error(f"Illegal character in list name: {char}")
|
2021-11-27 18:49:15 +01:00
|
|
|
list_file = scgi.get_user_dir() / new_list_name
|
2021-08-31 17:52:18 +02:00
|
|
|
list_file.touch(mode=0o660)
|
|
|
|
redirect_to_list(new_list_name)
|
|
|
|
|
2021-11-25 11:29:02 +01:00
|
|
|
|
2021-08-31 17:52:18 +02:00
|
|
|
def process_add(list_name):
|
2021-11-24 22:12:36 +01:00
|
|
|
if not (item := scgi.query_string_dec):
|
2021-08-31 17:52:18 +02:00
|
|
|
scgi.require_input("Enter your item")
|
|
|
|
else:
|
|
|
|
if "\n" in item:
|
|
|
|
scgi.temp_error("No new line allowed in item.")
|
|
|
|
line = f"* {item}\n"
|
2021-11-27 18:49:15 +01:00
|
|
|
list_file = scgi.get_user_dir() / list_name
|
2021-08-31 17:52:18 +02:00
|
|
|
try:
|
|
|
|
with open(list_file, "at") as f:
|
|
|
|
f.write(line)
|
|
|
|
except FileNotFoundError:
|
|
|
|
scgi.not_found()
|
|
|
|
else:
|
|
|
|
redirect_to_list(list_name)
|
|
|
|
|
2021-11-25 11:29:02 +01:00
|
|
|
|
2021-08-31 17:52:18 +02:00
|
|
|
def process_pop(list_name):
|
2021-11-27 18:49:15 +01:00
|
|
|
list_file = scgi.get_user_dir() / list_name
|
2021-08-31 17:52:18 +02:00
|
|
|
subprocess.run(["sed", "-i", "1d", str(list_file)])
|
|
|
|
redirect_to_list(list_name)
|
|
|
|
|
2021-11-25 11:29:02 +01:00
|
|
|
|
2021-11-27 18:49:15 +01:00
|
|
|
def process_clear(list_name):
|
|
|
|
if scgi.query_string_dec:
|
|
|
|
if scgi.query_string_dec != list_name:
|
|
|
|
redirect_to_list(list_name)
|
|
|
|
list_file = scgi.get_user_dir() / list_name
|
|
|
|
try:
|
|
|
|
with open(list_file, "wt") as f:
|
|
|
|
f.truncate()
|
|
|
|
except OSError:
|
|
|
|
scgi.temp_error()
|
|
|
|
redirect_to_list(list_name)
|
|
|
|
else:
|
|
|
|
scgi.require_input("Type the list name to confirm its clearing.")
|
|
|
|
|
|
|
|
|
|
|
|
def process_delete(list_name):
|
|
|
|
if scgi.query_string_dec:
|
|
|
|
if scgi.query_string_dec != list_name:
|
|
|
|
redirect_to_list(list_name)
|
|
|
|
list_file = scgi.get_user_dir() / list_name
|
|
|
|
list_file.unlink()
|
|
|
|
scgi.redirect_temp(scgi.script_name)
|
|
|
|
else:
|
|
|
|
scgi.require_input("Type the list name to confirm its deletion.")
|
|
|
|
|
|
|
|
|
2021-08-31 17:52:18 +02:00
|
|
|
def redirect_to_list(list_name):
|
2021-11-24 22:12:36 +01:00
|
|
|
scgi.redirect_temp(scgi.script_name + "/" + list_name)
|
2021-08-31 17:52:18 +02:00
|
|
|
|
2021-11-25 11:29:02 +01:00
|
|
|
|
2021-11-24 22:12:36 +01:00
|
|
|
path_components = scgi.path_info.lstrip("/").split("/", maxsplit=1)
|
2021-08-31 17:52:18 +02:00
|
|
|
list_name = path_components[0]
|
|
|
|
action = path_components[1] if len(path_components) > 1 else ''
|
|
|
|
if not list_name:
|
|
|
|
show_lists()
|
|
|
|
elif not action and list_name != "_":
|
|
|
|
show_list(list_name)
|
|
|
|
else:
|
|
|
|
process_action(list_name, action)
|