Skip to content

Instantly share code, notes, and snippets.

@brejoc
Last active August 29, 2015 14:27
Show Gist options
  • Select an option

  • Save brejoc/0c07d091af31cbcf9812 to your computer and use it in GitHub Desktop.

Select an option

Save brejoc/0c07d091af31cbcf9812 to your computer and use it in GitHub Desktop.
Quick and dirty (seriously) hack to recursively watch for .plantuml files to generate png images in that folder.
#!/usr/bin/python
"""\
Recursively watches for .plantuml files and generates png images in that folder.
Dependencies:
* watchdog
* sh
* plantuml.jar from plantuml.com
There also needs to be a 'plantumlpipe' shell script somewhere in $PATH with
the this code:
```
#! /usr/bin/env sh
java -Djava.awt.headless=true -jar plantuml.jar -pipe
```
"""
import os
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
from sh import plantumlpipe
class PlantUmlHandler(FileSystemEventHandler):
def on_created(self, event):
self._check_and_create(event)
def on_modified(self, event):
self._check_and_create(event)
def _check_and_create(self, event):
filename, file_extension = os.path.splitext(event.src_path)
if file_extension == ".plantuml":
with open (event.src_path, "r") as myfile:
uml=myfile.read()
plantumlpipe(_in=uml, _out="%s.png" % filename, _bg=True)
if __name__ == "__main__":
event_handler = PlantUmlHandler()
observer = Observer()
observer.schedule(event_handler, path='.', recursive=True)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment