def test_regexp_no_args(tmpdir): mktree(tmpdir, tree) watcher_no_args = RegExpWatcher(str(tmpdir)) changes = watcher_no_args.check() assert changes == set() sleep(0.01) tmpdir.join('foo/spam.py').write('xxx') tmpdir.join('foo/bar.txt').write('change') tmpdir.join('foo/recursive_dir/foo.js').write('change') assert watcher_no_args.check() == { (Change.modified, str(tmpdir.join('foo/spam.py'))), (Change.modified, str(tmpdir.join('foo/bar.txt'))), (Change.added, str(tmpdir.join('foo/recursive_dir/foo.js'))) }
def test_regexp_no_re_files(tmpdir): mktree(tmpdir, tree) re_dirs = r'^(?:(?!recursive_dir).)*$' watcher_no_re_files = RegExpWatcher(str(tmpdir), re_dirs=re_dirs) changes = watcher_no_re_files.check() assert changes == set() sleep(0.01) tmpdir.join('foo/spam.py').write('xxx') tmpdir.join('foo/bar.txt').write('change') tmpdir.join('foo/recursive_dir/foo.js').write('change') assert watcher_no_re_files.check() == { (Change.modified, str(tmpdir.join('foo/spam.py'))), (Change.modified, str(tmpdir.join('foo/bar.txt'))) }
def test_regexp(tmpdir): mktree(tmpdir, tree) re_files = r'^.*(\.txt|\.js)$' re_dirs = r'^(?:(?!recursive_dir).)*$' watcher = RegExpWatcher(str(tmpdir), re_files, re_dirs) changes = watcher.check() assert changes == set() sleep(0.01) tmpdir.join('foo/spam.py').write('xxx') tmpdir.join('foo/bar.txt').write('change') tmpdir.join('foo/borec.txt').write('ahoy') tmpdir.join('foo/borec-js.js').write('peace') tmpdir.join('foo/recursive_dir/b.js').write('borec') assert watcher.check() == { (Change.modified, str(tmpdir.join('foo/bar.txt'))), (Change.added, str(tmpdir.join('foo/borec.txt'))), (Change.added, str(tmpdir.join('foo/borec-js.js'))) }
def process(changetype, file, **kwargs): print("process:", changetype, file, kwargs) def cleanup(**kwargs): print("cleanup:", kwargs) def now(): return datetime.utcnow().timestamp() def null(changetype, file, **kwargs): print("null action called:", kwargs) pass for c in cf.watchconfig: c['watch'] = RegExpWatcher(root_path=c['dir'], re_files=c['pattern']) c['lastcheck'] = now() while True: for c in cf.watchconfig: change = c['watch'].check() if change != set(): for ch in change: changetype, file = ch if changetype in c['trigger']: result = getattr(main, c['action'])(changetype, file, **c) if (c['lastcheck'] + c['cleanup_every']) < now(): result = getattr(main, c['cleanup'])(changetype, file, **c) c['lastcheck'] = now()