async def watch_continously(root): try: while True: try: async for event in watch(root, (EVENTS.CREATE, EVENTS.MOVED_TO)): # EVENTS.CLOSE, EVENTS.MODIFY)): if event.is_dir: if event.tp & EVENTS.CREATE: logger.info("DIR Created: {}".format(event.name)) watchdir = os.path.join(root, event.name) await watchdir_queue.put(watchdir) else: # Events act like bit masks if event.tp & EVENTS.CREATE: logger.info("Created: {}".format(event.name)) msg = create_msg(root, event.name) if msg is not None: await msg_queue.put(msg) elif event.tp & EVENTS.MOVED_TO: logger.info("Moved: {}".format(event.name)) msg = create_msg(root, event.name) if msg is not None: await msg_queue.put(msg) except OSError as e: logger.warn("OSError: {}".format(e.args)) except: raise except curio.CancelledError: logger.info('Stop watching: {}'.format(root)) raise
async def do_watch(): count = 0 async for event in watch(p, EVENTS.CLOSE): count += 1 (p / str(count)).touch() if count == 5: break assert count == 5
async def watch_data_dir(data_dir, index): tg = curio.TaskGroup() waiting = set() async for ev in asyncwatch.watch( data_dir, asyncwatch.EVENTS.CREATE | asyncwatch.EVENTS.CLOSE): if not ev.name: continue p = data_dir / ev.name if p.is_dir() and p not in waiting: log.info(f"Waiting for {p}") await tg.spawn(index_after_commit, p, index, waiting) waiting.add(p)
async def index_after_commit(p, index, waiting): try: async for e in asyncwatch.watch(p, asyncwatch.EVENTS.CLOSE_WRITE): if e.name == 'COMMIT': log.info(f"Indexing {p}") index.index_folder(p) await write_index(index) waiting.discard(p) return except asyncwatch.NoMoreWatches: log.warn(f"Discarded folder {p}") return
def test_filters(tmp): p = tmp m = watch(p, EVENTS.ALL_EVENTS, filter="abc*d") async def do_watch(): async for event in m.next_events(): assert event.name == "abcXXd" async def main(): t = await curio.spawn(curio.timeout_after(1, do_watch())) (p / "xyz").touch() (p / "abcXXd").touch() await t.join() (p / "xyz").touch() #Cover the case where all the first events are filtered out t = await curio.spawn(do_watch()) (p / 'XXX').touch() await curio.sleep(0) (p / "abcXXd").touch() await t.join() curio.run(main())
def test_filters(tmp): p = tmp m = watch(p, EVENTS.ALL_EVENTS, filter="abc*d") async def do_watch(): async for event in m.next_events(): assert event.name == "abcXXd" async def main(): t = await curio.spawn(curio.timeout_after(1,do_watch())) (p/"xyz").touch() (p/"abcXXd").touch() await t.join() (p/"xyz").touch() #Cover the case where all the first events are filtered out t = await curio.spawn(do_watch()) (p/'XXX').touch() await curio.sleep(0) (p/"abcXXd").touch() await t.join() curio.run(main())
async def do_watch(): monitor = watch(str(tmpdir), EVENTS.DELETE, oneshot=True) async with monitor as event: assert event.tp == EVENTS.DELETE with pytest.raises(OSError): os.fstat(monitor._fd)
async def do_watch(): monitor = watch(tmp, EVENTS.DELETE, oneshot=True) async with monitor as event: assert event.tp == EVENTS.DELETE with pytest.raises(OSError): os.fstat(monitor._fd)