Last active
February 18, 2019 22:11
-
-
Save araichev/8923682 to your computer and use it in GitHub Desktop.
A little command line Python 2.7 script to compile RapydScript and RapydCSS files upon save. See the docstring for more details.
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 python | |
""" | |
Description | |
----------- | |
This little Python 3 script watches RapydScript/PyScss files contained | |
within a subdirectory of the directory from which it's run and executes the | |
commands | |
``rapydscript <filename>.pyj -o <filename>.js`` and | |
``pyscss <filename>.sass -o <filename>.css`` | |
whenever a RapydScript file ``<filename>.pyj`` or a PyScss file | |
``<filename>.sass`` is modified. | |
Instructions | |
------------- | |
#. Install the required packages below. | |
#. Save this script in a directory on you path. | |
#. Open a terminal, change to a directory that contains your | |
RapydScript/PyScss within its subtree, | |
run the script via the command ``autocompile_rapydstuff.py``. | |
Requirements | |
------------- | |
- Python 3+ | |
- RapydScript, a NodeJS package | |
- PyScss, a Python package | |
- watchdog, a Python package | |
Todo | |
---- | |
- Allow option flags to be passed to the RapydScript/PyScss compilers. | |
- Add a RapydML compiler. | |
""" | |
import subprocess | |
import time | |
import sys | |
import os | |
import watchdog | |
from watchdog.observers import Observer | |
from watchdog.tricks import Trick | |
def get_ext(path): | |
return path.rsplit('.', 1)[-1] | |
def get_new_path(path, ext=''): | |
return path.rsplit('.', 1)[0] + '.' + ext | |
class RapydScriptTrick(Trick): | |
def __init__(self, **kwargs): | |
super(RapydScriptTrick, self).__init__(**kwargs) | |
def on_modified(self, event): | |
if get_ext(event.src_path) != 'pyj': | |
return | |
src_file = event.src_path | |
dest_file = get_new_path(src_file, ext='js') | |
process = subprocess.call(['rapydscript', src_file, '-p', | |
'-o', dest_file]) | |
class PyScssTrick(Trick): | |
def __init__(self, **kwargs): | |
super(PyScssTrick, self).__init__(**kwargs) | |
def on_modified(self, event): | |
if get_ext(event.src_path) != 'sass': | |
return | |
src_file = event.src_path | |
dest_file = get_new_path(src_file, ext='css') | |
process = subprocess.call(['pyscss', src_file, '-o', dest_file]) | |
class RapydMLTrick(Trick): | |
def __init__(self, **kwargs): | |
super(RapydMLTrick, self).__init__(**kwargs) | |
def on_modified(self, event): | |
if get_ext(event.src_path) != 'pyml': | |
return | |
src_file = event.src_path | |
process = subprocess.call(['rapydml', src_file]) | |
if __name__ == '__main__': | |
path = sys.argv[1] if len(sys.argv) > 1 else os.getcwd() | |
print('Watching {!s}...'.format(path)) | |
event_handler_pyj = RapydScriptTrick() | |
event_handler_sass = PyScssTrick() | |
event_handler_pyml = RapydMLTrick() | |
observer = Observer() | |
observer.schedule(event_handler_pyj, path, recursive=True) | |
observer.schedule(event_handler_sass, path, recursive=True) | |
observer.schedule(event_handler_pyml, 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