Created
December 29, 2014 17:40
-
-
Save cool-RR/a3e9b74a7b0ef9cedcf7 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
| #!python | |
| import time | |
| import sys | |
| import pathlib | |
| import traceback | |
| import shpaml | |
| import watchdog.observers | |
| import watchdog.events | |
| import send2trash | |
| class EventHandler(watchdog.events.FileSystemEventHandler): | |
| def on_modified(self, event): | |
| source_path = pathlib.Path(event.src_path) | |
| if source_path.suffix == '.shpaml': | |
| destination_path = pathlib.Path(str(source_path)[:-7]) | |
| if source_path.exists(): | |
| if destination_path.exists(): | |
| send2trash.send2trash(str(destination_path)) | |
| with source_path.open('r') as source_file: | |
| source_file_contents = source_file.read() | |
| try: | |
| compiled_shpaml = shpaml.convert_text(source_file_contents) | |
| except Exception: | |
| traceback.print_exc() | |
| else: | |
| with destination_path.open('w') as destination_file: | |
| destination_file.write(compiled_shpaml) | |
| print('Updated %s' % destination_path) | |
| else: # not path.exists() | |
| send2trash.send2trash(str(destination_path)) | |
| def watch(): | |
| current_folder = pathlib.Path('.').absolute() | |
| print('Starting Shapml dog, watching folder: %s' % current_folder) | |
| event_handler = EventHandler() | |
| observer = watchdog.observers.Observer() | |
| observer.schedule(event_handler, path=str(current_folder), recursive=True) | |
| observer.start() | |
| try: | |
| while True: | |
| time.sleep(1) | |
| except KeyboardInterrupt: | |
| observer.stop() | |
| observer.join() | |
| print('Finished Shpaml dog.') | |
| if __name__ == '__main__': | |
| watch() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment