add template watcher and fix template builder
This commit is contained in:
parent
61a48bd8b6
commit
c68e256cf3
|
@ -7,6 +7,8 @@ from os.path import isfile, isdir, getmtime, abspath
|
|||
from jinja2 import Environment, FileSystemLoader, ChoiceLoader, select_autoescape
|
||||
from hamlpy.hamlpy import Compiler
|
||||
from markdown import markdown
|
||||
from watchdog.observers import Observer
|
||||
from watchdog.events import FileSystemEventHandler
|
||||
|
||||
from . import logging
|
||||
from .color import *
|
||||
|
@ -66,21 +68,16 @@ def delBuildPath(name):
|
|||
|
||||
|
||||
def getBuildPath(name=None):
|
||||
paths = list()
|
||||
template = build_path_pairs.get(name)
|
||||
|
||||
if name:
|
||||
if template:
|
||||
paths.append((template['source'], template['destination']))
|
||||
return template
|
||||
|
||||
else:
|
||||
raise ValueError(f'"{name}" not in build paths')
|
||||
|
||||
else:
|
||||
for k, tpl in build_path_pairs.items():
|
||||
paths.append((tpl['source'], tpl['destination']))
|
||||
|
||||
return paths
|
||||
return build_path_pairs
|
||||
|
||||
|
||||
def addEnv(data):
|
||||
|
@ -128,7 +125,7 @@ def aiohttpTemplate(*args, **kwargs):
|
|||
def buildTemplates(name=None):
|
||||
paths = getBuildPath(name)
|
||||
|
||||
for tplPaths in paths:
|
||||
for k, tplPaths in paths.items():
|
||||
src = tplPaths['source']
|
||||
dest = tplPaths['destination']
|
||||
|
||||
|
@ -148,7 +145,7 @@ def buildTemplates(name=None):
|
|||
else:
|
||||
times = {}
|
||||
|
||||
for filename in listdir(f'{src}/templates'):
|
||||
for filename in listdir(src):
|
||||
fullPath = f'{src}/{filename}'
|
||||
modtime = getmtime(fullPath)
|
||||
base, ext = filename.split('.')
|
||||
|
@ -183,4 +180,26 @@ def buildTemplates(name=None):
|
|||
filename.write(json.dumps(times))
|
||||
|
||||
|
||||
__all__ = ['addSearchPath', 'delSearchPath', 'addBuildPath', 'delSearchPath', 'addEnv', 'delEnv', 'setup', 'renderTemplate', 'aiohttp', 'buildTemplates']
|
||||
def templateWatcher():
|
||||
watchPaths = [path['source'] for k, path in build_path_pairs.items()]
|
||||
logging.info('Starting template watcher')
|
||||
observer = Observer()
|
||||
|
||||
for tplpath in watchPaths:
|
||||
logging.debug(f'Watching template dir for changes: {tplpath}')
|
||||
observer.schedule(templateWatchHandler(), tplpath, recursive=False)
|
||||
|
||||
observer.start()
|
||||
return observer
|
||||
|
||||
|
||||
class templateWatchHandler(FileSystemEventHandler):
|
||||
def on_any_event(self, event):
|
||||
filename, ext = os.path.splitext(os.path.relpath(event.src_path))
|
||||
|
||||
if event.event_type in ['modified', 'created'] and ext[1:] == 'haml':
|
||||
logging.info('Rebuilding templates')
|
||||
build_templates()
|
||||
|
||||
|
||||
__all__ = ['addSearchPath', 'delSearchPath', 'addBuildPath', 'delSearchPath', 'addEnv', 'delEnv', 'setup', 'renderTemplate', 'aiohttp', 'buildTemplates', 'templateWatcher']
|
||||
|
|
Loading…
Reference in a new issue