Skip to content

Instantly share code, notes, and snippets.

@dimitrov
Last active August 29, 2015 14:06
Show Gist options
  • Save dimitrov/41bab866b51c92dcacbb to your computer and use it in GitHub Desktop.
Save dimitrov/41bab866b51c92dcacbb to your computer and use it in GitHub Desktop.
watch.py
#!/usr/bin/env python
import os
import time
import logging
import subprocess
from watchdog.observers import Observer
from watchdog.events import (
PatternMatchingEventHandler, EVENT_TYPE_MODIFIED, EVENT_TYPE_MOVED,
EVENT_TYPE_CREATED
)
LOG = logging.getLogger(__name__)
SASS_COMPILER = 'sassc'
class SASSHandler(PatternMatchingEventHandler):
patterns = ['*.scss', '*.sass']
ignore_directories = True
def compile_sass(self, event):
input_file = getattr(event, 'dest_path', event.src_path)
file_name, extension = os.path.splitext(os.path.basename(input_file))
if extension in ('.scss', '.sass'):
output_path = os.path.dirname(input_file).replace('scss', 'css') \
.replace('sass', 'css')
output_file = os.path.join(output_path, '{}.css'.format(file_name))
if not os.path.exists(output_path):
os.makedirs(output_path)
subprocess.call([SASS_COMPILER, input_file, output_file])
LOG.info('Compiled {} -> {}'.format(input_file, output_file))
def on_any_event(self, event):
if event.event_type in (EVENT_TYPE_MODIFIED, EVENT_TYPE_MOVED,
EVENT_TYPE_CREATED):
self.compile_sass(event)
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
observer = Observer()
observer.schedule(SASSHandler(), '.', 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