def __init__(self, path: str, loop: asyncio.AbstractEventLoop): self._loop = loop self._app = None self._task = None assert path self.stopper = asyncio.Event(loop=self._loop) self._awatch = awatch(path, stop_event=self.stopper) commonql_path = os.path.normpath( os.path.join( path, os.pardir, os.pardir, "commonql", "commonql" ) ) self._commonql_awatch = awatch(commonql_path, stop_event=self.stopper)
async def raw_watcher(path, regex, raw_handler): ''' Async file watcher for raw raman spectrum files in a directory. Calls raw_handler when an event matching regex in path occurs Parameters ---------- path : string Full path to directory to watch for raw spectra files regex : string Regex to match raw spectra file names to raw_handler : classes.RawFileProcessor The RawFileProcessor that will process the raw file into an Instep format ''' watch_kwargs = {'re_files': regex, 're_dirs': None} # Check for events every 1000ms async for changes in awatch(path, watcher_cls=RegExpWatcher, watcher_kwargs=watch_kwargs, min_sleep=1000): # For every change for event, path in changes: await raw_handler.on_any_event(event, path) # Pass to raw_handler
async def _watch(args, proc=None): """ Run the executable on changes :param args: The full path and arguments to be run :param proc: A process object that might already be running """ script_path = sys.argv[1] script_mode = os.path.isfile(script_path) # use root path of the script (or cwd as a fall-back) root_path = os.getcwd() if script_mode: script_path = os.path.abspath(script_path) root_path = os.path.dirname(script_path) # only watch the script (or the entire cwd as a fall-back) watcher = watchgod.watcher.DefaultWatcher kwargs = None if script_mode: watcher = watchers.FileWatcher kwargs = dict(filename=os.path.basename(script_path)) print("Watching for changes in", script_path if script_mode else root_path, "...") print() # watch for changes async for _ in watchgod.awatch(root_path, watcher_cls=watcher, watcher_kwargs=kwargs): if proc is not None: proc.kill() proc = _run(args)
async def watch_reload(path: str, worker_settings: 'WorkerSettingsType') -> None: try: from watchgod import awatch except ImportError as e: # pragma: no cover raise ImportError( 'watchgod not installed, use `pip install watchgod`') from e loop = asyncio.get_event_loop() stop_event = asyncio.Event() def worker_on_stop(s: Signals) -> None: if s != Signals.SIGUSR1: # pragma: no cover stop_event.set() worker = create_worker(worker_settings) try: worker.on_stop = worker_on_stop loop.create_task(worker.async_run()) async for _ in awatch(path, stop_event=stop_event): print('\nfiles changed, reloading arq worker...') worker.handle_sig(Signals.SIGUSR1) await worker.close() loop.create_task(worker.async_run()) finally: await worker.close()
def __init__(self, path: str, loop: asyncio.AbstractEventLoop): self._loop = loop self._app = None self._task = None assert path self.stopper = asyncio.Event(loop=self._loop) self._awatch = awatch(path, stop_event=self.stopper)
async def _watch(self) -> None: async for changes in watchgod.awatch(self._path): changeset: ChangeSet = {} for event, group in itertools.groupby(changes, key=lambda item: item[0]): label = CHANGE_EVENT_LABELS[event] changeset[label] = [path for _, path in group] await self._on_change(changeset)
async def watch_folder(apps_folder): click.echo('watching python file changes in: %s' % apps_folder) async for changes in awatch(os.path.join(apps_folder)): for app in set([p.relative_to(apps_folder).parts[0] for p in [pathlib.Path(pair[1]) for pair in changes] if p.suffix == '.py']): Reloader.import_app(app)
async def _watch(self): """the actual watcher. schedule to run in the loop""" self._stop = Event() async for changes in awatch(self.path, watcher_cls=self._watcher_cls, stop_event=self._stop): IOLoop.current().add_callback(self._changes, changes)
def awatch(self, loop) -> wg.awatch: return wg.awatch(self.path, loop=loop, **{ 'watcher_kwargs': self._kwargs, 'watcher_cls': self.cls })
async def _watch_and_sync_dir(self, dir_info, task): async for _ in awatch(dir_info['local']): with self._set_host(task.host), self._set_user(task.user): await self._upload(dir_info['local'], dir_info['remote'], exclude=dir_info['exclude']) await self.restart()
async def watch_project(self, callback): async for changes in watchgod.awatch(self.safe_project.template_dir): await callback( (change for change in ((change[0], pathlib.Path(change[1])) for change in changes) if self.safe_project.is_template_file( change[1], include_descriptor=True, include_readme=True)))
async def watch_new_domains(): print("[*] Watching for new domains") async for _ in awatch(cli_args.file_domains): async with aiofiles.open(cli_args.file_domains, mode='r') as f: file_content = await f.read() clean_content_file = set(file_content.splitlines()) # Select only new domains new_domains = clean_content_file.difference(domains_processed) if not new_domains: print(f"[DOMAIN>>>>] Added new domain to " f"'{cli_args.file_domains}' but there're already " f"in file. So skipping") # Append new domains to processed domains and to the queue # domains_processed.update(new_domains) for d in new_domains: if not d: continue if not quiet: print(f"[DOMAIN>>>>] Added for processing: '{d}'") await input_domain_queue.put( (d, cli_args.http_max_recursion))
async def test_awatch(mocker): class FakeWatcher: def __init__(self, path): self._results = iter([ set(), set(), {'r1'}, set(), {'r2'}, set(), ]) def check(self): return next(self._results) ans = [] async for v in awatch('xxx', watcher_cls=FakeWatcher, debounce=5, normal_sleep=2, min_sleep=1): ans.append(v) if len(ans) == 2: break assert ans == [{'r1'}, {'r2'}]
async def watch_to_reload(check_dir: str): """ Coro which see changes in your code and restart him. :return: """ async for _ in awatch(check_dir): logger.info("Changes were found. Restarting...") restart()
def __init__(self, work_dir: dir, factory_loader): self._reload_count = 0 self._server_task = None self._app_factory = factory_loader self._awatch_stopped_event = asyncio.Event() self._awatch = awatch(work_dir, stop_event=self._awatch_stopped_event)
async def copy_adwaita_icon(): adwaita_icon_path = base_dir.joinpath('dist', 'adwaita.svg') dest_dir = docs_dir.joinpath('static') copy_to_dir(adwaita_icon_path, dest_dir) async for changes in awatch(adwaita_icon_path): for change, path in changes: path = Path(path) if change == Change.modified: copy_to_dir(path, dest_dir)
async def _auto_reload(): """ Coro which see changes in your code and restart him. :return: """ async for _ in awatch("."): logging.info("Changes founded. Restarting...") restart()
async def listen_events(self, root): watcher = awatch(root, watcher_cls=self.watcher_cls) init_files = self.watcher_cls(root).files.keys() for file in init_files: self.queue.put_nowait(self.Event(self.added, Path(file).resolve())) async for events in watcher: for event_type, path in events: self.queue.put_nowait( self.Event(event_type, Path(path).resolve()))
async def produce(queue): # read updates from the log asynchronously async for changes in awatch(newest): log_line = await get_last_line(newest) share_result = parse_line(log_line) if share_result: if LOGLEVEL == "DEBUG": print(f"producing {log_line}") await queue.put(share_result)
async def on_ready(): print('Logged in as') print(client.user.name) print(client.user.id) print('------') async for changes in awatch(path): inviteCodeFile = open(path + filename, 'r') await client.send_message( discord.Object(id="483765411490431007"), "Current Invite Code: " + inviteCodeFile.read())
async def async_watch(path, file_name, callback): async for changes in awatch(path): for change in changes: nType, szPath = change if is_target(szPath, file_name) is False: return; elif Change.deleted == nType: await callback(None) else: await callback(read(szPath))
async def watch_folder(apps_folder): click.echo('watching (%s-mode) python file changes in: %s' % (mode, apps_folder)) async for changes in awatch(os.path.join(apps_folder)): for app in set([p.relative_to(apps_folder).parts[0] for p in [pathlib.Path(pair[1]) for pair in changes] if p.suffix == '.py']): if mode == 'lazy': DIRTY_APPS[app] = True else: Reloader.import_app(app)
async def file_watcher(): """ End this script on .py file changes """ logger.info("Started file watcher") async for changes in awatch(".", watcher_cls=PythonWatcher, normal_sleep=5000): logger.info( f"Killing bot because of the following file changes: {changes}") runner.kill_bot() logger.info(f"Killed bot. Ending run.py.") exit()
async def async_watch(path, file_name, callback): async for changes in awatch(path): for change in changes: nType, szPath = change # print(nType, szPath) if is_target(szPath, file_name) is False: """do nothing""" elif Change.deleted == nType: await callback(None) else: await callback(read(szPath))
async def rebuild(app: web.Application): exec_file_path: Path = app['exec_file_path'] output_dir: Path = app['output_dir'] watcher = awatch(exec_file_path, watcher_cls=PythonWatcher) async for _ in watcher: print(f're-running {exec_file_path}...') start = time() await watcher.run_in_executor(build_in_subprocess, exec_file_path, output_dir) for ws in app[WS]: await ws.send_str('reload') c = len(app[WS]) print(f'run completed in {time() - start:0.3f}s, {c} browser{"" if c == 1 else "s"} updated')
async def watch(self): try: async for changes in watchgod.awatch(self._path): # noinspection PyTypeChecker for change in changes: if change[1] == str( self._path ) and change[0] == watchgod.Change.modified: # noinspection PyUnresolvedReferences self.sketchFileChanged.emit() except asyncio.CancelledError: pass
async def check_new_files(self): try: async for changes in watchgod.awatch(str(self.svg_dir)): # noinspection PyTypeChecker for change in changes: # TODO: implement removal as well if change[0] == watchgod.Change.added: path = pathlib.Path(change[1]) if path.suffix.lower() == ".svg": self.add_path(path, end=True) except asyncio.CancelledError: pass
async def watch(self, path): self.watched[path] = True self.log.info("watching %s", path) async for changes in awatch(path): self.log.info("%s changed:\n%s", path, changes) for change_type, change_path in changes: for path, handlers in self.path_handlers.items(): if Path(change_path) == path: for handler in handlers: await handler.write_message( {"content": json.loads(path.read_text())})
async def file_monitor(): async for changes in awatch("/Users/wangxiaofeng/Github-Thinkman/Tornado5Test"): for change in changes: nType, szPath = change print(nType) print(szPath) if Change.added == nType: print("added") elif Change.deleted == nType: print("deleted") elif Change.modified == nType: print("modified")
async def web_components(): src_dir = base_dir.joinpath('src', 'web-components') dest_dir = docs_dir.joinpath('static', 'ui') for path in src_dir.glob('**/*.js'): copy_to_dir(path, dest_dir, src_dir) async for changes in awatch(src_dir): for change, path in changes: path = Path(path) if path.suffix != '.js': continue if not change == Change.deleted: copy_to_dir(path, dest_dir, src_dir)
async def watch_reload(path, worker_settings, loop): try: from watchgod import awatch except ImportError as e: # pragma: no cover raise ImportError('watchgod not installed, use `pip install watchgod`') from e stop_event = asyncio.Event() worker = create_worker(worker_settings) try: worker.on_stop = lambda s: s != Signals.SIGUSR1 and stop_event.set() loop.create_task(worker.async_run()) async for _ in awatch(path, stop_event=stop_event): print('\nfiles changed, reloading arq worker...') worker.handle_sig(Signals.SIGUSR1) await worker.close() loop.create_task(worker.async_run()) finally: await worker.close()