From 9231013ea9bb0614caec914c97408c587c63e281 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Mart=C3=ADnez=20Portela?= Date: Sat, 20 Aug 2022 14:27:45 +0200 Subject: [PATCH] Add simplistic reloading to notes rendering script. --- scripts/generate.py | 55 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 3 deletions(-) diff --git a/scripts/generate.py b/scripts/generate.py index 37b685b..086e7a5 100644 --- a/scripts/generate.py +++ b/scripts/generate.py @@ -1,5 +1,6 @@ #!/usr/bin/env python3 +import time import json import html import logging @@ -7,6 +8,9 @@ import os import sys import uuid from datetime import datetime +import traceback + +import inotify.adapters import org_rw from org_rw import OrgTime, dom, Link @@ -22,6 +26,24 @@ EXTENSIONS = [ MIN_HIDDEN_HEADLINE_LEVEL = 2 INDEX_ID = "ea48ec1d-f9d4-4fb7-b39a-faa7b6e2ba95" +MONITORED_EVENT_TYPES = ( + 'IN_CREATE', + # 'IN_MODIFY', + 'IN_CLOSE_WRITE', + 'IN_DELETE', + 'IN_MOVED_FROM', + 'IN_MOVED_TO', + 'IN_DELETE_SELF', + 'IN_MOVE_SELF', +) + +ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + +STATIC_PATH = os.path.join(ROOT_DIR, 'static') + +def is_git_path(path): + return any([chunk == ".git" for chunk in os.path.split(path)]) + def load_all(top_dir_relative): top = os.path.abspath(top_dir_relative) @@ -47,11 +69,10 @@ def load_all(top_dir_relative): logging.info("Collected {} files".format(len(docs))) return docs - -def main(src_top, dest_top): - docs = load_all(src_top) +def regen_all(src_top, dest_top, docs=None): files_generated = 0 + docs = load_all(src_top) doc_to_headline_remapping = {} os.makedirs(dest_top, exist_ok=True) @@ -183,6 +204,34 @@ def main(src_top, dest_top): logging.info("Generated {} files".format(files_generated)) +def main(src_top, dest_top): + notifier = inotify.adapters.InotifyTrees([src_top, STATIC_PATH]) + + ## Initial load + t0 = time.time() + + docs = regen_all(src_top, dest_top) + logging.info("Initial load completed in {:.2f}s".format(time.time() - t0)) + + ## Updating + for event in notifier.event_gen(yield_nones=False): + (ev, types, directory, file) = event + if not any([type in MONITORED_EVENT_TYPES for type in types]): + continue + if is_git_path(directory): + continue + filepath = os.path.join(directory, file) + print("CHANGED: {}".format(filepath)) + t0 = time.time() + try: + docs = regen_all(src_top, dest_top, docs) + except: + logging.error(traceback.format_exc()) + logging.error("Loading new templates failed 😿") + continue + logging.info("Updated all in {:.2f}s".format(time.time() - t0)) + + def print_tree(tree, indentation=0): return for element in tree: