Created
January 11, 2014 23:16
-
-
Save Beyamor/8378285 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import jsontemplate | |
import json | |
import os | |
import argparse | |
import time | |
from watchdog.observers import Observer | |
from watchdog.events import FileSystemEventHandler | |
parser = argparse.ArgumentParser() | |
parser.add_argument("--watch", "-w", action="store_true") | |
args = parser.parse_args() | |
def run(): | |
try: | |
with open("template.html", "r") as f: | |
template = f.read() | |
with open("data.json", "r") as f: | |
data = json.loads(f.read()) | |
result = jsontemplate.expand(template, data) | |
with open("tom-gibson-resume.html", "w") as f: | |
f.write(result) | |
os.system("wkhtmltopdf -B 5 -T 5 -R 5 -L 5 tom-gibson-resume.html tom-gibson-resume.pdf") | |
except Exception as e: | |
print(e) | |
class RecompileHandler(FileSystemEventHandler): | |
def on_modified(self, event): | |
index = event.src_path.rfind("/") + 1 | |
name = event.src_path[index:] | |
if name in ["data.json", "template.html", "style.css"]: | |
run() | |
run() | |
if args.watch: | |
event_handler = RecompileHandler() | |
observer = Observer() | |
observer.schedule(event_handler, ".") | |
observer.start() | |
try: | |
while True: | |
time.sleep(0.1) | |
except KeyboardInterrupt: | |
observer.stop() | |
observer.join() |
this is ridiculously cool.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how the hell is args.watch ever true. how is args.watch made to be a thing. if you had a --chince param would you then be able to check args.chince ??