Python watchdog.events.RegexMatchingEventHandler() Examples

The following are 1 code examples of watchdog.events.RegexMatchingEventHandler(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module watchdog.events , or try the search function .
Example #1
Source File: develop-module.py    From cjworkbench with GNU Affero General Public License v3.0 4 votes vote down vote up
def main(directory):
    cjwstate.modules.init_module_system()
    path = pathlib.Path(directory)

    def reload():
        if os.path.isdir(path / "locale"):
            try:
                logger.info(f"Extracting i18n messages...")
                extract_module_messages(path)
            except Exception:
                logger.exception("Error extracting module translations")
                return

        try:
            logger.info(f"Importing module...")
            import_module_from_directory(path)
        except Exception:
            logger.exception("Error loading module")

    want_reload = False

    class ReloadEventHandler(RegexMatchingEventHandler):
        def on_any_event(self, ev):
            nonlocal want_reload
            want_reload = True

    regexes = [".*\\.(py|json|yaml|html|po|pot)"]

    event_handler = ReloadEventHandler(regexes=regexes)
    observer = Observer()
    observer.schedule(event_handler, directory, recursive=True)
    observer.start()

    reload()

    try:
        while True:
            time.sleep(0.05)  # 50ms
            # Call `reload()` if Watchdog has notified us in the past 50ms.
            if want_reload:
                reload()
                want_reload = False
    except KeyboardInterrupt:
        observer.stop()
    observer.join()