def main(state_machine): with open(OUTPUT_LOG) as f: for _ in watchgod.watch(OUTPUT_LOG): for line in f.readlines(): line = line.strip() if line: state_machine(line)
def config_watcher(self): # A slot takes no params for changes in watch('./'): for status, location in changes: if location == './config.json': self.update_config.emit() self.finished.emit()
def run(path, target, *, args=(), kwargs=None, callback=None, watcher_cls=None, debounce=400, min_sleep=100): """ Run a function in a subprocess using `multiprocessing.Process`. restart it whenever file changes in path. """ watcher = watch( path, watcher_cls=watcher_cls or DotPyWatcher, debounce=debounce, min_sleep=min_sleep, ) process = _start_process(target=target, args=args, kwargs=kwargs) reloads = 0 for changes in watcher: callback and callback(changes) _stop_process(process) process = _start_process(target=target, args=args, kwargs=kwargs) reloads += 1 return reloads
def start(self): for changes in watch(self.dir): print(changes) move_from, move_to = None, None modified = False for change, path in changes: basename = os.path.basename(path) if change == Change.added: move_to = (basename, path) elif change == Change.deleted: move_from = (basename, path) elif change == Change.modified: if not os.path.basename(path).startswith('.'): modified = path if not modified and (move_from is not None or move_to is not None): if move_from is None: self._handle_add(move_to[1]) elif move_to is None: self._handle_delete(move_from[1]) elif move_to[0] != move_from[0]: self._handle_rename(move_from[1], move_to[1]) else: self._handle_move(move_from[1], move_to[1]) if modified: self._handle_modified(modified)
def run(self): print("Watchdog thread started") for changes in watch(getcwd()): print(changes) if changes and basename(next(iter(changes))[1]) == dbfilename: self.database_updated.emit(True) print("database changed")
def run(article_root: str) -> None: search_index = get_search_index(article_root) paths = absolute_paths_to_articles_in(article_root) log.info(f"Started watching {article_root} for changes") log.info(f"Found {len(paths)} articles in {article_root}") if not search_index: search_index = create_search_index(article_root) update_index_incrementally( article_root, search_index, paths, ) for changes in watch( article_root, watcher_cls=RegExpWatcher, watcher_kwargs=dict(re_files=r"^.*(\.md)$"), ): update_index_incrementally( article_root, search_index, absolute_paths_to_articles_in(article_root), )
def watch(self): for changes in watchgod.watch( ".", watcher_cls=MudiWatcher, watcher_kwargs={"site": self.site} ): for change_type, path in changes: logging.info(f"{path} {change_type.name}") path = Path(path) self._dispatch(change_type, path)
def observe_playlists(): for changes in watch(CHUNKS_PATH, watcher_cls=RegExpWatcher, watcher_kwargs=dict(re_files=M3U8_PATTERN)): for change in changes: if change[0] != Change.deleted: threading.Thread(target=handle_playlist_update, kwargs=dict(src_path=change[1])).start()
def test_watch_keyboard_error(): class FakeWatcher: def __init__(self, path): pass def check(self): raise KeyboardInterrupt() iter = watch('xxx', watcher_cls=FakeWatcher, debounce=5, min_sleep=1) assert list(iter) == []
def watch_this(self): logging.info(f"Watching {self.path} for file {self.event}") for changes in watch(self.path): for change in changes: if ((self.event == "all") or (self.event == "modified" and change[0] == Change.modified) or (self.event == "added" and change[0] == Change.added)): logging.info(f"Detected file {self.event} in {self.path}") self.change_function()
def worker(): print("monitoring:", self.model_path) for changes in watchgod.watch(self.model_path): for _, file in changes: print(file, "changed") if re.compile(str(self.model_file),str(file)): classifier = BertClassifier() classifier.set_mode(tf.estimator.ModeKeys.PREDICT) print("reload model") with self.lock: self.classifier = classifier
def watch_briefings(briefing_path): # waits for new files in the directory and renames every new file # to "current-briefing.html", which can then be opened by a browser for changes in watch(briefing_path): for change, path in changes: # the rename (replace) itself generates an event (change); # the "not in path" filters those out, i.e. it only works on # briefing files (those in xxxx-xx-xx_xxxxxx_briefing format) if change == Change.added and "current-briefing.html" not in path: os.replace( path, os.path.join(briefing_path, "current-briefing.html")) print("Briefing saved — ready to be viewed")
def event_listener(sources, logger=None): for p in sources.keys(): logger.info(f'Set to watch {p}') # watch for changes to file and update for changes in watch( list(sources.keys()), watcher_cls=MultipleFilesWatcher): if logger: logger.info(changes) for c in changes: if c[0] == Change.modified: yield c[1]
def test_watch_log(mocker, caplog): mock_log_enabled = mocker.patch('watchgod.main.logger.isEnabledFor') mock_log_enabled.return_value = True class FakeWatcher: def __init__(self, path): self.files = [1, 2, 3] def check(self): return {'r1'} iter = watch('xxx', watcher_cls=FakeWatcher, debounce=5, min_sleep=10) assert next(iter) == {'r1'} assert 'DEBUG xxx time=Xms debounced=Xms files=3 changes=1 (1)' in re.sub( r'\dms', 'Xms', caplog.text)
def test_watch_min_sleep(mocker): class FakeWatcher: def __init__(self, path): pass def check(self): return {'x'} mocker.spy(watchgod.main, 'sleep') mocker.spy(watchgod.main.logger, 'debug') iter = watch('xxx', watcher_cls=FakeWatcher, debounce=5, min_sleep=10) assert next(iter) == {'x'} assert next(iter) == {'x'} assert watchgod.main.sleep.call_count == 1 assert watchgod.main.sleep.call_args[0][0] == 0.01 assert watchgod.main.logger.debug.called is False
def watch(ctx, match_patterns, skip_patterns, watch_dir): logging.info(f"Watching directory {watch_dir}") config = HiccupConfig(ctx.obj["config_filename"]) task_ctx = {"__globals": config.globals, "__root": watch_dir} task_list = TaskList(tasks=config.watch_tasks, ctx=task_ctx) watcher_kwargs = { "match_patterns": match_patterns if match_patterns else None, "skip_patterns": skip_patterns if skip_patterns else None, } for changes in watchgod.watch(watch_dir, watcher_cls=GlobWatcher, watcher_kwargs=watcher_kwargs): for change_type, filepath in changes: filepath = Path(filepath) logging.info(f"File {filepath} {change_type.name}!") task_list.run_tasks(filepath, change_to_str(change_type))
def test_watch(mocker): class FakeWatcher: def __init__(self, path): self._results = iter([ {'r1'}, set(), {'r2'}, ]) def check(self): return next(self._results) mocker.spy(watchgod.main, 'sleep') iter_ = watch('xxx', watcher_cls=FakeWatcher, debounce=5, min_sleep=1) assert next(iter_) == {'r1'} assert next(iter_) == {'r2'} assert watchgod.main.sleep.call_count == 2 assert watchgod.main.sleep.call_args_list[0][0][0] > 0.001 assert watchgod.main.sleep.call_args_list[1][0][0] > 0.001
def __call__(self, logger: logging.Logger, *args: Any, **kwds: Any) -> Any: logger.info("##### Start search for change in backend status") for changes in watch(self.location): logger.info("##### Change detected") if (list(changes)[0][1]).split('/')[-1] == 'debug.log': f = open(list(changes)[0][1], 'r') text = f.readlines()[-1] dct = dict( map( lambda pair: tuple(pair), map( lambda _split: _split.split("="), filter(lambda split: len(split.split("=")) == 2, text.split(" "))))) logger.info(dct) try: if round(float(dct["progress"]), 2) == 0.2: logger.info("##### Sync in 20%") if round(float(dct["progress"]), 2) == 0.4: logger.info("##### Sync in 40%") if round(float(dct["progress"]), 2) == 0.6: logger.info("##### Sync in 60%") if round(float(dct["progress"]), 2) == 0.8: logger.info("##### Sync in 80%") if float(dct["progress"]) == 1.0: logger.info("##### Sync done. Exiting") sys.exit() except Exception as e: logger.info("##### Watcher exception: {}".format(str(e)))
def listen(domain): ''' :param domain: the domain that you want to listen for, any message received will be reported to you yields message_id, message ''' listenDir = os.path.join(IO_BASE_DIR, domain) os.makedirs(listenDir, exist_ok=True) for changes in watch(listenDir): # changes is set of changes, every change is tuple of (changeClass, filePath) for c in changes: changeType, messageFilePath = c if changeType == Change.deleted: continue text = None with open(messageFilePath) as messageFile: text = messageFile.read() if text == None: raise RuntimeError("can't find read the message") messageID = os.path.basename(messageFilePath) yield messageID, text
def test_watch(mocker): class FakeWatcher: def __init__(self, path): self._results = iter([ {'r1'}, set(), {'r2'}, set(), ]) def check(self): return next(self._results) iter_ = watch('xxx', watcher_cls=FakeWatcher, debounce=5, normal_sleep=2, min_sleep=1) assert next(iter_) == {'r1'} assert next(iter_) == {'r2'}
def watchthis(dir=".",config=os.path.expanduser("~/.wtt.yaml")): print("Watching {dir}".format(dir=os.path.abspath(dir))) with open(config) as f: config = yaml.safe_load(f) formats = [e["filetype"] for e in config] print("Watching these formats:\n{formats}".format(formats = "\n".join(formats))) for c in watchgod.watch(dir): change, filepath = c.pop() _, ext = os.path.splitext(filepath) for e in config: if re.search(ext, e["filetype"]): call = e["call"].format(file=filepath) #os.system("clear") os.system(call)
def _watch(directory: str) -> None: # pragma: no cover try: from watchgod import watch # pylint: disable=import-outside-toplevel except ImportError: logging.error( 'Could not watch for file changes, is "watchgod" installed?') return logging.info('Watching for file changes in "%s"', directory) for changes in watch(directory): for change in changes: file_path = change[1] module_name = os.path.split(os.path.relpath(file_path, directory))[0] logging.debug('Changes in file "%s" cause "%s" module (re)load', file_path, module_name) import_or_reload(module_name)
def _watch(directory): # pragma: no cover try: from watchgod import watch except ImportError: logging.error( 'Could not watch for file changes, is "watchgod" installed?') return logging.info('Watching for file changes in "%s"' % directory) for changes in watch(directory): for change in changes: file_path = change[1] module_name = os.path.split(os.path.relpath(file_path, directory))[0] logging.debug('Changes in file "%s" cause "%s" module (re)load' % (file_path, module_name)) import_or_reload(module_name)
def test_watch_stop(): class FakeWatcher: def __init__(self, path): self._results = iter([ {'r1'}, set(), {'r2'}, ]) def check(self): return next(self._results) stop_event = threading.Event() stop_event.set() ans = [] for c in watch('xxx', watcher_cls=FakeWatcher, debounce=5, min_sleep=1, stop_event=stop_event): ans.append(c) assert ans == []
def watch(self): # Print echo_click(" * Watch for", self.BasePath, Color="green") echo_click(" * Press CTRL+C to quit", Color="green") # Config File ConfigFile = path.join(self.BasePath, self.ConfigFile) # Watch for Changes in watch(self.BasePath, watcher_cls=EleranWatcher): _Type, FileChange = list(Changes)[0] TypeChange = _Type.name.capitalize() if FileChange in self.WatchFiles: echo_click(" *", TypeChange, FileChange) ID = self.get_id(FileChange) if self.Mode == "debug": echo_click(" * Build ID:", ID) # Build self.build(ID) elif FileChange == ConfigFile: echo_click(" *", TypeChange, FileChange) self.reload_config()
def test_watch_watcher_kwargs(mocker): class FakeWatcher: def __init__(self, path, arg1=None, arg2=None): self._results = iter([ {arg1}, set(), {arg2}, set(), ]) def check(self): return next(self._results) kwargs = dict(arg1='foo', arg2='bar') iter_ = watch('xxx', watcher_cls=FakeWatcher, watcher_kwargs=kwargs, debounce=5, normal_sleep=2, min_sleep=1) assert next(iter_) == {kwargs['arg1']} assert next(iter_) == {kwargs['arg2']}
return "" if args.version: print("{} v{}".format(progname, version)) exit() if not (filenames or filepat or xfilepat): argparser.print_help() exit() print("(!) select window to send keypress events ...") targetWin = xdo.select_window_with_click() if targetWin: winname = getWindowName(targetWin) print("selected window: {}".format(winname)) for changes in watch("."): winname = getWindowName(targetWin) if winpat and not re.match(winpat, winname): continue reload = any([included(e[1]) for e in changes]) time.sleep(args.delay) if reload: curWin = xdo.get_active_window() xdo.focus_window(targetWin) xdo.send_keysequence_window(targetWin, key.encode("utf-8")) xdo.focus_window(curWin) print("sending {} to {}".format(key, targetWin))
moduleContent = file.read() files = os.listdir('./src/textareas') for file in files: replaceString = f'{{{{textareas/{file}}}}}' if not (replaceString in moduleContent): continue fileContent = '' with open(f"./src/textareas/{file}") as file: fileContent = "".join(line.rstrip() for line in file) fileContent = fileContent.replace("\t", "").replace(' ', '') moduleContent = moduleContent.replace(replaceString, fileContent) with open("./dist/compiled.lua", "w") as f: f.write(moduleContent) compile() print(f'Compiled successfully.') if (len(sys.argv) > 1 and sys.argv[1] == "--watch"): for changes in watch('./src'): print(f'Got change {changes}, compiling...') compile() print(f'Compiled successfully.')
# def process_IN_CREATE(self, event): # if event.pathname.endswith(".p8"): # create_lua_from_p8() # # def process_IN_DELETE(self, event): # if event.pathname.endswith(".lua"): # create_lua_from_p8() # # # normal text editor change to the lua file # def process_IN_MODIFY(self, event): # # print "MODIFY "+event.pathname # if event.pathname.endswith(".lua"): # on_lua_changed(event.pathname) # # # Intellij Idea moves stuff when changed # def process_IN_MOVED_TO(self, event): # # print "MOVED "+event.pathname # if event.pathname.endswith(".lua"): # on_lua_changed(event.pathname) # create lua files for p8 carts, which don't have corresponding lua files yet main_file = create_lua_from_p8() print("Found main_file {}".format(main_file)) if os.path.isfile(main_file): for changes in watch('.'): for change, file in changes: if file.endswith("lua"): print("change: {} file: {}".format(change, file)) on_lua_changed(main_file)
prawcore.exceptions.ServerError - If any Reddit server error. prawcore.TooLarge - If image file is over 300kb """ widgets = self.subreddit.widgets image_dicts = [ { 'width': 600, 'height': 450, 'linkUrl': '', 'url': widgets.mod.upload_image(img_path) } for img_path in [image_file] ] return widgets.mod.update(shortName='Sidebar', data=image_dicts) uploader = RedditUploader() upper_ticker = "/data/upper-ticker.png" lower_ticker = "/data/lower-ticker.png" banner = "/data/banner.png" for changes in watchgod.watch("/data"): log.info(f"File change detected: {changes}") upper_ticker_changed = any(upper_ticker in t for t in changes) lower_ticker_changed = any(lower_ticker in t for t in changes) if upper_ticker_changed and lower_ticker_changed: log.info("Updating old reddit.") uploader.upload_image("upper-ticker", upper_ticker) uploader.upload_image("lower-ticker", lower_ticker) uploader.touch_stylesheet() if any(banner in t for t in changes): log.info("Updating reddit redesign.") log.info("Blocked reddit redesign update, not implemented.") # uploader.upload_banner('/data/banner.png')