From 3fa73178baeb43cf5583cbb0bdceb4257cd7e737 Mon Sep 17 00:00:00 2001 From: Juergen Bocklage-Ryannel Date: Fri, 3 Feb 2017 09:22:25 +0100 Subject: Added reload option to qtcpp generator. Allows to watch recursive the source directories and run the generator script when changes appear --- qface/builtin/qtcpp/qtcpp.py | 10 ++++++-- qface/watch.py | 55 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 qface/watch.py diff --git a/qface/builtin/qtcpp/qtcpp.py b/qface/builtin/qtcpp/qtcpp.py index 2e53edb..ed2d1f4 100755 --- a/qface/builtin/qtcpp/qtcpp.py +++ b/qface/builtin/qtcpp/qtcpp.py @@ -10,6 +10,7 @@ from path import Path from qface.generator import FileSystem, Generator from qface.helper.qtcpp import Filters from qface.helper.doc import parse_doc +from qface.watch import monitor here = Path(__file__).dirname() @@ -59,12 +60,17 @@ def run(src, dst): @click.command() +@click.option('--reload/--no-reload', default=False) @click.argument('src', nargs=-1, type=click.Path(exists=True)) @click.argument('dst', nargs=1, type=click.Path(exists=True)) -def app(src, dst): +def app(src, dst, reload): """Takes several files or directories as src and generates the code in the given dst directory.""" - run(src, dst) + if reload: + script = '{0} {1} {2}'.format(Path(__file__).abspath(), ' '.join(src), dst) + monitor(src, script) + else: + run(src, dst) if __name__ == '__main__': diff --git a/qface/watch.py b/qface/watch.py new file mode 100644 index 0000000..89d07e4 --- /dev/null +++ b/qface/watch.py @@ -0,0 +1,55 @@ +from watchdog.events import FileSystemEventHandler +from watchdog.observers import Observer +import click +from subprocess import call +from path import Path +import time + + +def sh(cmd, all=False, **kwargs): + click.secho('$ {0}'.format(cmd), fg='green') + return call(cmd, shell=True, **kwargs) + + +class RunScriptChangeHandler(FileSystemEventHandler): + def __init__(self, script): + super().__init__() + self.script = script + self.is_running = False + + def on_modified(self, event): + self.run() + + def run(self): + if self.is_running: + return + self.is_running = True + sh(self.script, cwd=Path.getcwd()) + self.is_running = False + + +def monitor(src, script): + """ + reloads the script when src files changes + """ + script = Path(script).expand().abspath() + src = src if isinstance(src, (list, tuple)) else [src] + src = [Path(entry).expand().abspath() for entry in src] + event_handler = RunScriptChangeHandler(script) + observer = Observer() + path = script.dirname().expand().abspath() + click.secho('watch recursive: {0}'.format(path), fg='blue') + observer.schedule(event_handler, path, recursive=True) + for entry in src: + entry = entry.dirname().expand().abspath() + click.secho('watch recursive: {0}'.format(entry), fg='blue') + observer.schedule(event_handler, entry, recursive=True) + event_handler.run() # run always once + observer.start() + + try: + while True: + time.sleep(1) + except KeyboardInterrupt: + observer.stop() + observer.join() -- cgit v1.2.1