Merge pull request #59 from PR0M3TH3AN/codex/configure-resource-access-with-importlib.resources

Update packaging and resource loading
This commit is contained in:
thePR0M3TH3AN
2025-06-19 20:57:54 -04:00
committed by GitHub
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,7 +131,7 @@ 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)
@@ -132,25 +143,26 @@ def copy_template(name: str) -> str:
def build_assets(config_path: str, pdf_path: str | None = None): def build_assets(config_path: str, pdf_path: str | None = None):
with resources.as_file(_src_res()) as src_dir:
# generate QR codes # generate QR codes
run(['bash', 'generate_qr.sh', config_path], cwd=ROOT / 'src') run(['bash', 'generate_qr.sh', config_path], cwd=src_dir)
# obfuscate html # obfuscate html
run(['bash', 'obfuscate_index.sh', config_path], cwd=ROOT / 'src') run(['bash', 'obfuscate_index.sh', config_path], cwd=src_dir)
run(['bash', 'obfuscate_nostr.sh', config_path], cwd=ROOT / 'src') run(['bash', 'obfuscate_nostr.sh', config_path], cwd=src_dir)
data = load_config(config_path) data = load_config(config_path)
with open(ROOT / 'src/index.html', 'r') as fh: with open(src_dir / 'index.html', 'r') as fh:
html = fh.read() html = fh.read()
pattern = r'<p class="binary" id="binary-message">.*?</p>' pattern = r'<p class="binary" id="binary-message">.*?</p>'
repl = f'<p class="binary" id="binary-message">{data.get("binary_message", "")}</p>' repl = f'<p class="binary" id="binary-message">{data.get("binary_message", "")}</p>'
html = re.sub(pattern, repl, html, flags=re.S) html = re.sub(pattern, repl, html, flags=re.S)
with open(ROOT / 'src/index.html', 'w') as fh: with open(src_dir / 'index.html', 'w') as fh:
fh.write(html) fh.write(html)
subdomain = data['subdomain'] subdomain = data['subdomain']
dest = ROOT / 'host' / subdomain dest = ROOT / 'host' / subdomain
os.makedirs(dest / 'from_client', exist_ok=True) os.makedirs(dest / 'from_client', exist_ok=True)
shutil.copy(config_path, dest / 'config.json') shutil.copy(config_path, dest / 'config.json')
for fname in ['index.html', 'nostr.html', 'qrcode-content.png', 'qrcode-tear-offs.png', 'example.pdf', 'submission_form.pdf']: for fname in ['index.html', 'nostr.html', 'qrcode-content.png', 'qrcode-tear-offs.png', 'example.pdf', 'submission_form.pdf']:
shutil.copy(ROOT / 'src' / fname, dest) shutil.copy(src_dir / fname, dest)
if pdf_path: if pdf_path:
shutil.copy(pdf_path, dest / 'from_client' / 'submission_form.pdf') shutil.copy(pdf_path, dest / 'from_client' / 'submission_form.pdf')
print(f"Flyer files created under {dest}") print(f"Flyer files created under {dest}")