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 jinja2 import Environment, FileSystemLoader, ChoiceLoader, select_autoescape
|
||||||
from hamlpy.hamlpy import Compiler
|
from hamlpy.hamlpy import Compiler
|
||||||
from markdown import markdown
|
from markdown import markdown
|
||||||
|
from watchdog.observers import Observer
|
||||||
|
from watchdog.events import FileSystemEventHandler
|
||||||
|
|
||||||
from . import logging
|
from . import logging
|
||||||
from .color import *
|
from .color import *
|
||||||
|
@ -66,21 +68,16 @@ def delBuildPath(name):
|
||||||
|
|
||||||
|
|
||||||
def getBuildPath(name=None):
|
def getBuildPath(name=None):
|
||||||
paths = list()
|
|
||||||
template = build_path_pairs.get(name)
|
template = build_path_pairs.get(name)
|
||||||
|
|
||||||
if name:
|
if name:
|
||||||
if template:
|
if template:
|
||||||
paths.append((template['source'], template['destination']))
|
return template
|
||||||
|
|
||||||
else:
|
else:
|
||||||
raise ValueError(f'"{name}" not in build paths')
|
raise ValueError(f'"{name}" not in build paths')
|
||||||
|
|
||||||
else:
|
return build_path_pairs
|
||||||
for k, tpl in build_path_pairs.items():
|
|
||||||
paths.append((tpl['source'], tpl['destination']))
|
|
||||||
|
|
||||||
return paths
|
|
||||||
|
|
||||||
|
|
||||||
def addEnv(data):
|
def addEnv(data):
|
||||||
|
@ -128,7 +125,7 @@ def aiohttpTemplate(*args, **kwargs):
|
||||||
def buildTemplates(name=None):
|
def buildTemplates(name=None):
|
||||||
paths = getBuildPath(name)
|
paths = getBuildPath(name)
|
||||||
|
|
||||||
for tplPaths in paths:
|
for k, tplPaths in paths.items():
|
||||||
src = tplPaths['source']
|
src = tplPaths['source']
|
||||||
dest = tplPaths['destination']
|
dest = tplPaths['destination']
|
||||||
|
|
||||||
|
@ -148,7 +145,7 @@ def buildTemplates(name=None):
|
||||||
else:
|
else:
|
||||||
times = {}
|
times = {}
|
||||||
|
|
||||||
for filename in listdir(f'{src}/templates'):
|
for filename in listdir(src):
|
||||||
fullPath = f'{src}/{filename}'
|
fullPath = f'{src}/{filename}'
|
||||||
modtime = getmtime(fullPath)
|
modtime = getmtime(fullPath)
|
||||||
base, ext = filename.split('.')
|
base, ext = filename.split('.')
|
||||||
|
@ -183,4 +180,26 @@ def buildTemplates(name=None):
|
||||||
filename.write(json.dumps(times))
|
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