nomadnet-website/pages/library.mu

50 lines
1.5 KiB
Plaintext
Executable File

#!/usr/bin/env python3
import hswro
import os
from pathlib import PurePosixPath
from typing import List
from os import environ
LIBRARY_DIR = "/home/reticulum/library"
library_contents = [""]
def get_from_list(l: List[str], i: int) -> str:
try:
return l[i]
except IndexError:
return ""
with os.scandir(LIBRARY_DIR) as it:
for entry in it:
if not entry.name.startswith('.') and entry.is_file():
library_contents.append(PurePosixPath(entry.path).stem)
content_lines = ["", "Select a file to begin reading."]
if "var_view" in environ:
document_name = environ["var_view"]
if len(document_name) > 20:
raise ValueError("Document name is too long, rejecting")
if not all(map(lambda c: c.isalnum(), document_name)):
raise ValueError("Document name contains not allowed characters, rejecting")
content_lines = []
with open(f"{LIBRARY_DIR}/{document_name}.txt") as file:
content_lines = [line.rstrip() for line in file]
page_len = max(len(library_contents), len(content_lines)) + 2
hswro.header("Library")
for i in range(page_len):
menuitem = " "*23
item_name = get_from_list(library_contents, i)
if item_name != "":
menuitem = f" `[{item_name:<20}`:/page/library.mu`view={item_name}] "
content = get_from_list(content_lines, i)
print(f"{hswro.MENUBGCOLOR}{menuitem}{hswro.BGCOLOR} {content}")
print("-.")
hswro.footer()