Add simplistic reloading to notes rendering script.

This commit is contained in:
Sergio Martínez Portela 2022-08-20 14:27:45 +02:00
parent 399f00a54f
commit 9231013ea9

View File

@ -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: