Last active
September 6, 2015 23:09
-
-
Save datakurre/36fd22332baa70033c56 to your computer and use it in GitHub Desktop.
Chameleon Composer
This file contains 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
#! /usr/bin/env nix-shell | |
#! nix-shell -i python -p pythonPackages.chameleon pythonPackages.docopt pythonPackages.watchdog | |
"""Chameleon Composer | |
Copyright (c) 2015 Asko Soukka <asko.soukka@iki.fi> | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. | |
Usage: | |
./compose.py <filename> | |
./compose.py src/front-page.html | |
./compose.py <source> <destination> [--watch] | |
./compose.py src build | |
./compose.py src build --watch | |
""" | |
from __future__ import print_function | |
from chameleon import PageTemplateFile | |
from chameleon import PageTemplateLoader | |
from docopt import docopt | |
from watchdog.observers import Observer | |
from watchdog.observers.polling import PollingObserver | |
from watchdog.utils import platform | |
import os | |
import sys | |
import time | |
def render(template): | |
assert os.path.isfile(template) | |
# Add siblings as templates into compilation context for macro-use | |
context = {} | |
dirname = os.path.dirname(template) | |
for name in os.listdir(dirname): | |
path = os.path.join(dirname, name) | |
basename, suffix = os.path.splitext(name) | |
if os.path.isdir(path): | |
context[basename] = PageTemplateLoader(path, '.html') | |
elif suffix == '.html': | |
context[basename] = PageTemplateFile(path) | |
return PageTemplateFile(template)(**context).strip() | |
class Composer(object): | |
def __init__(self, source, destination): | |
self.source = source | |
self.destination = destination | |
self.mapping = {} | |
self.update() | |
def update(self): | |
source = self.source | |
destination = self.destination | |
mapping = {} | |
# File to file | |
if os.path.isfile(source) and os.path.splitext(destination)[-1]: | |
mapping[source] = destination | |
# File to directory | |
elif os.path.isfile(source) and not os.path.splitext(destination)[-1]: | |
mapping[source] = os.path.join( | |
destination, | |
os.path.splitext(os.path.basename(source))[0] + '.html' | |
) | |
# Directory to directory | |
elif os.path.isdir(source): | |
for filename in os.listdir(source): | |
path = os.path.join(source, filename) | |
if os.path.splitext(path)[-1] != '.html': | |
continue | |
mapping[path] = os.path.join( | |
destination, | |
os.path.splitext(os.path.basename(path))[0] + '.html' | |
) | |
self.mapping = mapping | |
def __call__(self): | |
for source, destination in self.mapping.items(): | |
if os.path.dirname(destination): | |
if not os.path.isdir(os.path.dirname(destination)): | |
os.makedirs(os.path.dirname(destination)) | |
with open(destination, 'w') as output: | |
print('{0:s} => {1:s}'.format(source, destination)) | |
output.write(render(source).strip().encode('utf-8')) | |
# noinspection PyUnusedLocal | |
def dispatch(self, event): | |
# TODO: Build only changed files | |
self.update() | |
self.__call__() | |
def watch(self): | |
if platform.is_darwin(): | |
observer = PollingObserver() # Seen FSEventsObserver to segfault | |
else: | |
observer = Observer() | |
observer.schedule(self, self.source, recursive=True) | |
observer.start() | |
try: | |
while True: | |
time.sleep(1) | |
except KeyboardInterrupt: | |
observer.stop() | |
observer.join() | |
sys.exit(0) | |
if __name__ == '__main__': | |
arguments = docopt(__doc__, version='Chameleon Composer 1.0') | |
if arguments.get('<filename>'): | |
print(render(arguments.get('<filename>'))) | |
sys.exit(0) | |
composer = Composer(arguments.get('<source>'), | |
arguments.get('<destination>')) | |
composer() | |
if arguments.get('--watch'): | |
print('Watching {0:s}'.format(arguments.get('<source>'))) | |
composer.watch() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment