Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async def test_awatch_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'}
async for v in awatch('xxx', watcher_cls=FakeWatcher, debounce=5, min_sleep=1):
assert v == {'r1'}
break
assert 'DEBUG xxx time=Xms debounced=Xms files=3 changes=1 (1)' in re.sub(r'\dms', 'Xms', caplog.text)
async def test_awatch_stop():
class FakeWatcher:
def __init__(self, path):
self._results = iter([
{'r1'},
set(),
{'r2'},
])
def check(self):
return next(self._results)
stop_event = asyncio.Event()
stop_event.set()
ans = []
async for v in awatch('xxx', watcher_cls=FakeWatcher, debounce=5, min_sleep=1, stop_event=stop_event):
ans.append(v)
assert ans == []
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'}]
loop.add_signal_handler(signal.SIGTERM, stop_event.set)
webpack_process = await start_webpack_watch(config)
config_path = str(config.config_path or config.source_dir)
# max_workers = 1 so the same config and som are always used to build the site
with ProcessPoolExecutor(max_workers=1) as executor:
await loop.run_in_executor(executor, set_config, config, verbose)
ret = await loop.run_in_executor(executor, update_site, UpdateArgs(config_path=config_path))
logger.info('\nStarting dev server, go to http://localhost:%s', port)
server = Server(config, port)
await server.start()
try:
async for changes in awatch(config.source_dir, stop_event=stop_event, watcher_cls=HarrierWatcher):
logger.debug('file changes: %s', changes)
args = UpdateArgs(config_path=config_path, pages=set())
for change, raw_path in changes:
path = Path(raw_path)
if is_within(path, config.pages_dir):
args.pages.add((change, path))
elif is_within(path, config.theme_dir / 'assets'):
args.assets = True
elif is_within(path, config.theme_dir / 'sass'):
args.sass = True
elif is_within(path, config.theme_dir / 'templates'):
args.templates = True
elif is_within(path, config.data_dir):
args.data = True
elif path == config.extensions.path:
args.extensions = True
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 watchgod_coro(path, action):
async for changes in awatch(path, watcher_cls=VLCSchedulerSourceWatcher, debounce=3600):
logger.info('Changes detected in %s.' % path)
action()
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()
asset_files = []
for g in asset_globs:
asset_files.extend(template_dir.glob(g))
for src in asset_files:
dest = src.relative_to(template_dir)
dest = docs_dest_dir.joinpath(dest)
shutil.copyfile(src, dest)
# Copy static files
shutil.copytree(
docs_src_dir.joinpath('asset'),
docs_dest_dir.joinpath('static/asset'),
dirs_exist_ok=True)
async for changes in awatch(str(template_dir)):
for change in changes:
print(change)
if (change[0] == Change.modified)\
or (change[0] == Change.added):
if re.match(r'.*\.html$', change[1]):
src = Path(change[1])
self.template_html(src)
elif re.match(r'.*\.md$', change[1]):
src = Path(change[1])
src = src.parent.joinpath(src.stem + '.html')
self.template_html(src)
elif re.match(r'.*\.styl$', change[1]):
src = Path(change[1])
await self.stylus(src)
elif re.match(r'.*\.js$', change[1]):
src = Path(change[1])