summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJuergen Bocklage-Ryannel <juergen.bocklage-ryannel@pelagicore.com>2017-02-03 09:22:25 +0100
committerJuergen Bocklage-Ryannel <juergen.bocklage-ryannel@pelagicore.com>2017-02-03 09:22:25 +0100
commit3fa73178baeb43cf5583cbb0bdceb4257cd7e737 (patch)
tree9781f8d281366b57c55344faa39cf7fb92507dba
parentc66f5b25e5601522c60afcab32577282f2234861 (diff)
downloadqtivi-qface-3fa73178baeb43cf5583cbb0bdceb4257cd7e737.tar.gz
Added reload option to qtcpp generator. Allows to watch recursive the source directories and run the generator script when changes appear
-rwxr-xr-xqface/builtin/qtcpp/qtcpp.py10
-rw-r--r--qface/watch.py55
2 files changed, 63 insertions, 2 deletions
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()