Created
January 1, 2018 19:24
-
-
Save sowbug/11e0b72cb2d642987481e85981b6d010 to your computer and use it in GitHub Desktop.
Automatically moves BBC micro:bit files from the browser to the device
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
# usage: | |
# | |
# auto_download_microbit.py dir_to_watch dir_of_microbit | |
import os.path, shutil, sys, time | |
from watchdog.observers import Observer | |
from watchdog.events import PatternMatchingEventHandler | |
src = sys.argv[1] if len(sys.argv) > 1 else '.' | |
dst = sys.argv[2] if len(sys.argv) > 2 else '/Volumes/MICROBIT/' | |
def handle_event(event): | |
filename = None | |
if event.event_type == "created": | |
filename = event.src_path | |
if event.event_type == "moved": | |
filename = event.dest_path | |
if filename is None: | |
return | |
dst_filename = os.path.basename(filename) | |
dst_path = os.path.join(dst, dst_filename) | |
print "moving", dst_filename | |
shutil.move(filename, dst_path) | |
event_handler = PatternMatchingEventHandler(patterns=["*.hex"], | |
ignore_patterns=["*.crdownload"], | |
ignore_directories=True) | |
event_handler.on_any_event = handle_event | |
observer = Observer() | |
observer.schedule(event_handler, src) | |
print "\nLooking in %s\nto move to %s..." % (src, dst) | |
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