Include resource directories

This commit is contained in:
thePR0M3TH3AN
2025-06-19 20:57:35 -04:00
parent 6dfc568928
commit f7dae49b61
3 changed files with 50 additions and 30 deletions

2
MANIFEST.in Normal file
View File

@@ -0,0 +1,2 @@
include templates/**
include src/**

View File

@@ -6,6 +6,12 @@ build-backend = "setuptools.build_meta"
where = ["."] where = ["."]
include = ["voxvera*"] include = ["voxvera*"]
[tool.setuptools]
include-package-data = true
[tool.setuptools.package-data]
voxvera = ["../templates/**", "../src/**"]
[project] [project]
name = "voxvera" name = "voxvera"
version = "0.1.0" version = "0.1.0"

View File

@@ -7,6 +7,8 @@ import subprocess
import sys import sys
import datetime import datetime
from pathlib import Path from pathlib import Path
from importlib import resources
from importlib.abc import Traversable
from InquirerPy import prompt, inquirer from InquirerPy import prompt, inquirer
from rich.console import Console from rich.console import Console
@@ -14,6 +16,14 @@ from rich.console import Console
ROOT = Path(__file__).resolve().parent.parent ROOT = Path(__file__).resolve().parent.parent
def _template_res(*parts) -> Traversable:
return resources.files(__package__).joinpath('..', 'templates', *parts)
def _src_res(*parts) -> Traversable:
return resources.files(__package__).joinpath('..', 'src', *parts)
def require_cmd(cmd: str): def require_cmd(cmd: str):
if shutil.which(cmd) is None: if shutil.which(cmd) is None:
print(f"Required command '{cmd}' not found. Please install it.", file=sys.stderr) print(f"Required command '{cmd}' not found. Please install it.", file=sys.stderr)
@@ -110,7 +120,8 @@ def update_from_pdf(config_path: str, pdf_path: str):
tmpdir = tempfile.mkdtemp() tmpdir = tempfile.mkdtemp()
os.makedirs(os.path.join(tmpdir, 'from_client'), exist_ok=True) os.makedirs(os.path.join(tmpdir, 'from_client'), exist_ok=True)
shutil.copy(pdf_path, os.path.join(tmpdir, 'from_client', 'submission_form.pdf')) shutil.copy(pdf_path, os.path.join(tmpdir, 'from_client', 'submission_form.pdf'))
shutil.copy(ROOT / 'templates' / 'blank' / 'extract_form_fields.sh', tmpdir) with resources.as_file(_template_res('blank', 'extract_form_fields.sh')) as p:
shutil.copy(p, tmpdir)
shutil.copy(config_path, os.path.join(tmpdir, 'config.json')) shutil.copy(config_path, os.path.join(tmpdir, 'config.json'))
run(['bash', 'extract_form_fields.sh'], cwd=tmpdir) run(['bash', 'extract_form_fields.sh'], cwd=tmpdir)
shutil.copy(os.path.join(tmpdir, 'config.json'), config_path) shutil.copy(os.path.join(tmpdir, 'config.json'), config_path)
@@ -120,40 +131,41 @@ def update_from_pdf(config_path: str, pdf_path: str):
def copy_template(name: str) -> str: def copy_template(name: str) -> str:
"""Copy a template directory into dist/ with a datestamped folder.""" """Copy a template directory into dist/ with a datestamped folder."""
date = datetime.date.today().strftime('%Y%m%d') date = datetime.date.today().strftime('%Y%m%d')
src = ROOT / 'templates' / name with resources.as_file(_template_res(name)) as src:
if not src.is_dir(): if not src.is_dir():
print(f"Template {name} not found", file=sys.stderr) print(f"Template {name} not found", file=sys.stderr)
sys.exit(1) sys.exit(1)
dest = ROOT / 'dist' / f"{name}-{date}" dest = ROOT / 'dist' / f"{name}-{date}"
os.makedirs(ROOT / 'dist', exist_ok=True) os.makedirs(ROOT / 'dist', exist_ok=True)
shutil.copytree(src, dest, dirs_exist_ok=True) shutil.copytree(src, dest, dirs_exist_ok=True)
print(f"Template copied to {dest}") print(f"Template copied to {dest}")
return str(dest) return str(dest)
def build_assets(config_path: str, pdf_path: str | None = None): def build_assets(config_path: str, pdf_path: str | None = None):
# generate QR codes with resources.as_file(_src_res()) as src_dir:
run(['bash', 'generate_qr.sh', config_path], cwd=ROOT / 'src') # generate QR codes
# obfuscate html run(['bash', 'generate_qr.sh', config_path], cwd=src_dir)
run(['bash', 'obfuscate_index.sh', config_path], cwd=ROOT / 'src') # obfuscate html
run(['bash', 'obfuscate_nostr.sh', config_path], cwd=ROOT / 'src') run(['bash', 'obfuscate_index.sh', config_path], cwd=src_dir)
data = load_config(config_path) run(['bash', 'obfuscate_nostr.sh', config_path], cwd=src_dir)
with open(ROOT / 'src/index.html', 'r') as fh: data = load_config(config_path)
html = fh.read() with open(src_dir / 'index.html', 'r') as fh:
pattern = r'<p class="binary" id="binary-message">.*?</p>' html = fh.read()
repl = f'<p class="binary" id="binary-message">{data.get("binary_message", "")}</p>' pattern = r'<p class="binary" id="binary-message">.*?</p>'
html = re.sub(pattern, repl, html, flags=re.S) repl = f'<p class="binary" id="binary-message">{data.get("binary_message", "")}</p>'
with open(ROOT / 'src/index.html', 'w') as fh: html = re.sub(pattern, repl, html, flags=re.S)
fh.write(html) with open(src_dir / 'index.html', 'w') as fh:
subdomain = data['subdomain'] fh.write(html)
dest = ROOT / 'host' / subdomain subdomain = data['subdomain']
os.makedirs(dest / 'from_client', exist_ok=True) dest = ROOT / 'host' / subdomain
shutil.copy(config_path, dest / 'config.json') os.makedirs(dest / 'from_client', exist_ok=True)
for fname in ['index.html', 'nostr.html', 'qrcode-content.png', 'qrcode-tear-offs.png', 'example.pdf', 'submission_form.pdf']: shutil.copy(config_path, dest / 'config.json')
shutil.copy(ROOT / 'src' / fname, dest) for fname in ['index.html', 'nostr.html', 'qrcode-content.png', 'qrcode-tear-offs.png', 'example.pdf', 'submission_form.pdf']:
if pdf_path: shutil.copy(src_dir / fname, dest)
shutil.copy(pdf_path, dest / 'from_client' / 'submission_form.pdf') if pdf_path:
print(f"Flyer files created under {dest}") shutil.copy(pdf_path, dest / 'from_client' / 'submission_form.pdf')
print(f"Flyer files created under {dest}")
def serve(config_path: str): def serve(config_path: str):