From 64bed4bbb1b2f73b153487d9d75b67b59670e795 Mon Sep 17 00:00:00 2001 From: evilchili Date: Sat, 17 Sep 2022 16:48:16 -0700 Subject: [PATCH] Adding import command --- deadsands/pyproject.toml | 1 + deadsands/site_tools/cli.py | 44 +++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/deadsands/pyproject.toml b/deadsands/pyproject.toml index 76ee8fa..fe9b45d 100644 --- a/deadsands/pyproject.toml +++ b/deadsands/pyproject.toml @@ -17,6 +17,7 @@ rich = "latest" pyyaml = "latest" livereload = "latest" jinja2-simple-tags = "latest" +pyinotify = "latest" # static website pelican = "^4.7.2" diff --git a/deadsands/site_tools/cli.py b/deadsands/site_tools/cli.py index 673f2d9..4188b8e 100644 --- a/deadsands/site_tools/cli.py +++ b/deadsands/site_tools/cli.py @@ -11,7 +11,11 @@ import site_tools as st from enum import Enum from livereload import Server +from livereload.watcher import INotifyWatcher +from pathlib import Path from pelican import main as pelican_main +from time import sleep + from site_tools.content_manager import create @@ -30,6 +34,10 @@ CONFIG = { 'port': 8000, # content manager config 'templates_path': 'markdown-templates', + # directory to watch for new assets + 'import_path': 'imports', + # where new asseets will be made available + 'production_host': 'deadsands.froghat.club', } app = typer.Typer() @@ -61,6 +69,42 @@ def build() -> None: pelican_run() +@app.command() +def watch() -> None: + + import_path = Path(CONFIG['import_path']) + content_path = Path(st.SETTINGS['PATH']) + + def do_import(): + assets = [] + for src in import_path.rglob('*'): + relpath = src.relative_to(import_path) + target = content_path / relpath + if src.is_dir(): + target.mkdir(parents=True, exist_ok=True) + continue + if target.exists(): + print(f"{target}: exists; skipping.") + continue + print(f"{target}: importing...") + src.link_to(target) + uri = target.relative_to('content') + assets.append(f"https://{CONFIG['production_host']}/{uri}") + src.unlink() + if assets: + publish() + print('\n\t'.join(["\nImported Asset URLS:"] + assets)) + print("\n") + + watcher = INotifyWatcher() + watcher.watch(import_path, do_import) + watcher.start(do_import) + print(f"Watching {import_path}. CTRL+C to exit.") + while True: + watcher.examine() + sleep(1) + + @app.command() def serve() -> None: