Python watchdog.observers.Observer() Examples

The following are 30 code examples of watchdog.observers.Observer(). 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.observers , or try the search function .
Example #1
Source File: cli.py    From psync with MIT License 18 votes vote down vote up
def watch():
    is_proj, root = get_project_root()

    state = {"dirty": False}

    if not is_proj:
        click.echo("Run psync to generate .psync config file.")
    else:
        click.echo("Start watching {} ...".format(root))
        event_handler = watcher.AnyEventHandler(state)
        observer = Observer()
        observer.schedule(event_handler, root, recursive=True)
        observer.start()
        try:
            while True:
                if state["dirty"]:
                    click.echo("Detect modification. Perform sync.")
                    perform_sync()
                    state["dirty"] = False
                time.sleep(1)
        except KeyboardInterrupt:
            observer.stop()
        observer.join() 
Example #2
Source File: monitor.py    From judge-server with GNU Affero General Public License v3.0 7 votes vote down vote up
def __init__(self):
        if Observer is not None and not judgeenv.no_watchdog:
            if judgeenv.env.update_pings:
                logger.info('Using thread to ping urls: %r', judgeenv.env.update_pings)
                self._refresher = RefreshWorker(judgeenv.env.update_pings)
            else:
                self._refresher = None

            self._handler = SendProblemsHandler(self._refresher)
            self._monitor = Observer()
            for dir in get_problem_watches():
                self._monitor.schedule(self._handler, dir, recursive=True)
                logger.info('Scheduled for monitoring: %s', dir)
        else:
            self._monitor = None
            self._refresher = None 
Example #3
Source File: generator.py    From tags with MIT License 7 votes vote down vote up
def _watch(root='.', dest='_site', pattern='**/*.html', exclude='_*/**'):

    try:
        from watchdog.observers import Observer
        from watchdog.events import FileSystemEventHandler
    except ImportError:
        msg = "The build --watch feature requires watchdog. \n"\
            + "Please install it with 'easy_install watchdog'."
        print(msg)
        return None

    class handler(FileSystemEventHandler):
        def on_any_event(self, event):
            exclude_path = os.path.join(os.getcwd(), exclude)
            if not utils.matches_pattern(exclude_path, event.src_path):
                build_files(root=root,
                            dest=dest,
                            pattern=pattern,
                            exclude=exclude)

    observer = Observer()
    observer.schedule(handler(), root, recursive=True)
    observer.start()

    print("Watching '{0}' ...".format(root))

    return observer 
Example #4
Source File: parser.py    From OrgNote with GNU General Public License v2.0 6 votes vote down vote up
def do_server(self,port="8080"):
        self.port = port

        self.monitor_path = self.source_dir
        
        observer = Observer()
        observer.schedule(OrgNoteFileSystemEventHander(self.monitor_restart,self.port), self.monitor_path, recursive=True)
        observer.start()
        
        self.monitor_log('Watching directory %s' % self.monitor_path)

        self.process = Process(target=self.monitor_start, args=(self.port,))
        self.process.daemon = True
        self.process.start()
        self.process.join()
        
        try:
            while True:
                time.sleep(1)
        except KeyboardInterrupt:
            observer.stop()
        observer.join() 
Example #5
Source File: regenerate.py    From sugardough with Apache License 2.0 6 votes vote down vote up
def main(watch):
    # Regenerate at least once.
    regenerate()

    if watch:
        observer = Observer()

        # Observe both the template directory and cookiecutter.json.
        observer.schedule(RegenerateSugardoughHandler(), TEMPLATEDIR, recursive=True)
        cookiecutter_json_handler = RegenerateSugardoughHandler(patterns=[
            os.path.join(BASEDIR, 'cookiecutter.json')
        ])
        observer.schedule(cookiecutter_json_handler, BASEDIR)

        print('Watching for changes...')
        observer.start()
        try:
            while True:
                time.sleep(1)
        except KeyboardInterrupt:
            observer.stop()
        observer.join() 
Example #6
Source File: events.py    From dyc with MIT License 6 votes vote down vote up
def start(cls, config):
        """
        The starter method for watching files.
        If invoked, it will start watching all files and see if documentation is needed

        Parameters
        ----------
        ConfigParser config: Config going to be used in DYC
        """
        logging.basicConfig(level=logging.INFO)
        observer = Observer()
        event_handler = WatchEvent()
        event_handler.config = config
        observer.schedule(event_handler, '.', recursive=True)
        observer.start()
        try:
            while True:
                time.sleep(1)
        except KeyboardInterrupt:
            observer.stop()
            print('Quitting..')
        observer.join() 
Example #7
Source File: premiumizer.py    From premiumizer with MIT License 6 votes vote down vote up
def watchdir():
    try:
        global watchdog_handler
        logger.debug('Initializing watchdog')
        observer = Observer()
        observer.schedule(watchdog_handler, path=cfg.watchdir_location, recursive=True)
        observer.start()
        logger.debug('Initializing watchdog complete')
        if cfg.watchdir_walk_enabled:
            scheduler.scheduler.add_job(walk_watchdir, 'interval', id='walk_watchdir', seconds=active_interval,
                                        replace_existing=True, max_instances=1, coalesce=True)
        else:
            walk_watchdir()

    except:
        raise


# Flask 
Example #8
Source File: plugin.py    From exopy with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _bind_observers(self):
        """Start the observers.

        """
        for contrib in ('users', 'starters', 'connections', 'settings'):
            callback = partial(self._update_contribs, contrib)
            getattr(self, '_'+contrib).observe('contributions', callback)

        def update():
            """Run the handler on the main thread to avoid GUI issues.

            """
            deferred_call(self._refresh_profiles)

        self._observer = Observer()
        for folder in self._profiles_folders:
            handler = SystematicFileUpdater(update)
            self._observer.schedule(handler, folder, recursive=True)

        self._observer.start() 
Example #9
Source File: pickle_core.py    From cachier with MIT License 6 votes vote down vote up
def wait_on_entry_calc(self, key):
        with self.lock:
            self._reload_cache()
            entry = self._get_cache()[key]
            if not entry['being_calculated']:
                return entry['value']
        event_handler = _PickleCore.CacheChangeHandler(
            filename=self._cache_fname(), core=self, key=key
        )
        observer = Observer()
        event_handler.inject_observer(observer)
        observer.schedule(
            event_handler, path=self.expended_cache_dir, recursive=True
        )
        observer.start()
        observer.join(timeout=1.0)
        if observer.is_alive():
            # print('Timedout waiting. Starting again...')
            return self.wait_on_entry_calc(key)
        # print("Returned value: {}".format(event_handler.value))
        return event_handler.value 
Example #10
Source File: watchmedo.py    From hacker-scripts with MIT License 6 votes vote down vote up
def shell_command(args):
    """
    Subcommand to execute shell commands in response to file system events.

    :param args:
        Command line argument options.
    """
    from watchdog.observers import Observer
    from watchdog.tricks import ShellCommandTrick

    if not args.command:
        args.command = None

    patterns, ignore_patterns = parse_patterns(args.patterns,
                                               args.ignore_patterns)
    handler = ShellCommandTrick(shell_command=args.command,
                                patterns=patterns,
                                ignore_patterns=ignore_patterns,
                                ignore_directories=args.ignore_directories,
                                wait_for_process=args.wait_for_process,
                                drop_during_process=args.drop_during_process)
    observer = Observer(timeout=args.timeout)
    observe_with(observer, handler, args.directories, args.recursive) 
Example #11
Source File: extras.py    From pympress with GNU General Public License v2.0 6 votes vote down vote up
def start_daemon(cls):
            """ Start the watchdog observer thread.
            """
            if cls.observer is None:
                cls.observer = Observer()
                cls.monitor = FileSystemEventHandler()

            if not cls.observer.is_alive():
                cls.observer.start() 
Example #12
Source File: assets.py    From ANALYSE with GNU Affero General Public License v3.0 6 votes vote down vote up
def watch_assets(options):
    """
    Watch for changes to asset files, and regenerate js/css
    """
    observer = Observer()

    CoffeeScriptWatcher().register(observer)
    SassWatcher().register(observer)
    XModuleSassWatcher().register(observer)

    print("Starting asset watcher...")
    observer.start()
    if not getattr(options, 'background', False):
        # when running as a separate process, the main thread needs to loop
        # in order to allow for shutdown by contrl-c
        try:
            while True:
                observer.join(2)
        except KeyboardInterrupt:
            observer.stop()
        print("\nStopped asset watcher.") 
Example #13
Source File: engineservice.py    From BerryNet with GNU General Public License v3.0 6 votes vote down vote up
def run(self, args):
        self.record_pid()

        # workaround the issue that SIGINT cannot be received (fork a child to
        # avoid blocking the main process in Thread.join()
        child_pid = os.fork()
        if child_pid == 0:  # child
            # observer handles event in a different thread
            observer = Observer()
            observer.schedule(self.event_handler, path=args['image_dir'])
            observer.start()
            self.server()
        else:  # parent
            try:
                os.wait()
            except KeyboardInterrupt:
                os.kill(child_pid, signal.SIGKILL)
                self.erase_pid() 
Example #14
Source File: realtime.py    From picopore with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, runner):
        self.runner = runner

        self.event_handler = PatternMatchingEventHandler(patterns=["*.fast5"],
                ignore_patterns=[],
                ignore_directories=True)
        self.event_handler.on_created = self.on_created
        self.event_handler.on_moved = self.on_moved

        self.observer = Observer()

        self.observedPaths = []
        for path in self.runner.input:
            if os.path.isdir(path):
                self.observer.schedule(self.event_handler, path, recursive=True)
                self.observedPaths.append(path)
        log("Monitoring {} in real time. Press Ctrl+C to exit.".format(", ".join(self.observedPaths))) 
Example #15
Source File: helpers.py    From pbs with GNU Affero General Public License v3.0 6 votes vote down vote up
def _update_project_watch(config, task_presenter, results,
                          long_description, tutorial):  # pragma: no cover
    """Update a project in a loop."""
    logging.basicConfig(level=logging.INFO,
                        format='%(asctime)s - %(message)s',
                        datefmt='%Y-%m-%d %H:%M:%S')
    path = os.getcwd()
    event_handler = PbsHandler(config, task_presenter, results,
                               long_description, tutorial)
    observer = Observer()
    # We only want the current folder, not sub-folders
    observer.schedule(event_handler, path, recursive=False)
    observer.start()
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join() 
Example #16
Source File: lesscd.py    From datacats with GNU Affero General Public License v3.0 6 votes vote down vote up
def main():
    opts = docopt(__doc__, version=__version__)
    environment = Environment.load(opts['ENVIRONMENT'] or '.')
    env_path = environment.target
    less_paths = [
        path_join(env_path, 'ckan', 'ckan', 'public', 'base', 'less'),
        path_join(env_path, 'ckan', 'ckan', 'public', 'base', 'vendor')
    ]

    if not env_path or not all(exists(less_path) for less_path in less_paths):
        print 'No source code to watch found'
        return

    observer = Observer()
    event_handler = LessCompileEventHandler(environment)
    for less_path in less_paths:
        observer.schedule(event_handler, less_path, recursive=True)
    observer.start()

    # HACK: We make it so that the OS doesn't consult us and just kills us.
    signal.signal(signal.SIGINT, signal.SIG_DFL)

    observer.join() 
Example #17
Source File: lambda_simulator.py    From serapis with MIT License 6 votes vote down vote up
def watch():
    from serapis import tasks
    global tasks_map
    tasks_map = {
        "search": tasks.search,
        "detect": tasks.detect,
        "save": tasks.save
    }

    observer = Observer()
    observer.schedule(TaskHandler(), config.local_s3, recursive=False)
    observer.start()
    print("Watching local bucket in '{}'...".format(config.local_s3))
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join() 
Example #18
Source File: watchdog.py    From script.service.kodi.callbacks with GNU General Public License v3.0 5 votes vote down vote up
def start(self):
        for item in self.observersettings:
            observer = Observer()
            observer.schedule(item[0], item[1], recursive=item[2])
            observer.start()
            self.observers.append(observer) 
Example #19
Source File: sqlite_event_log.py    From dagster with Apache License 2.0 5 votes vote down vote up
def __init__(self, base_dir, inst_data=None):
        '''Note that idempotent initialization of the SQLite database is done on a per-run_id
        basis in the body of connect, since each run is stored in a separate database.'''
        self._base_dir = os.path.abspath(check.str_param(base_dir, 'base_dir'))
        mkdir_p(self._base_dir)

        self._watchers = defaultdict(dict)
        self._obs = Observer()
        self._obs.start()
        self._inst_data = check.opt_inst_param(inst_data, 'inst_data', ConfigurableClassData) 
Example #20
Source File: consolidated_sqlite_event_log.py    From dagster with Apache License 2.0 5 votes vote down vote up
def __init__(self, base_dir, inst_data=None):
        self._base_dir = check.str_param(base_dir, 'base_dir')
        self._conn_string = create_db_conn_string(base_dir, SQLITE_EVENT_LOG_FILENAME)
        self._inst_data = check.opt_inst_param(inst_data, 'inst_data', ConfigurableClassData)
        self._watchdog = None
        self._watchers = defaultdict(dict)
        self._obs = Observer()
        self._obs.start()

        if not os.path.exists(self.get_db_path()):
            self._init_db() 
Example #21
Source File: config.py    From ogame-bot with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def __init__(self, *args, **kwargs):

        self._config = ConfigParser.RawConfigParser()
        self.reload_config()
        self._config_valid = False
        self.observer = Observer()
        self.observer.schedule(self, path='.', recursive=False)
        self.observer.start() 
Example #22
Source File: config.py    From OpenCryptoBot with GNU Affero General Public License v3.0 5 votes vote down vote up
def _watch_changes():
        observer = Observer()

        file = ConfigManager._CFG_FILE
        method = ConfigManager._read_cfg
        change_handler = ChangeHandler(file, method)

        observer.schedule(change_handler, ".", recursive=True)
        observer.start() 
Example #23
Source File: dashboard.py    From EDMarketConnector with GNU General Public License v2.0 5 votes vote down vote up
def start(self, root, started):
        self.root = root
        self.session_start = started

        logdir = config.get('journaldir') or config.default_journal_dir
        if not logdir or not isdir(logdir):
            self.stop()
            return False

        if self.currentdir and self.currentdir != logdir:
            self.stop()
        self.currentdir = logdir

        # Set up a watchdog observer.
        # File system events are unreliable/non-existent over network drives on Linux.
        # We can't easily tell whether a path points to a network drive, so assume
        # any non-standard logdir might be on a network drive and poll instead.
        polling = platform != 'win32'
        if not polling and not self.observer:
            self.observer = Observer()
            self.observer.daemon = True
            self.observer.start()
        elif polling and self.observer:
            self.observer.stop()
            self.observer = None

        if not self.observed and not polling:
            self.observed = self.observer.schedule(self, self.currentdir)

        if __debug__:
            print '%s Dashboard "%s"' % (polling and 'Polling' or 'Monitoring', self.currentdir)

        # Even if we're not intending to poll, poll at least once to process pre-existing
        # data and to check whether the watchdog thread has crashed due to events not
        # being supported on this filesystem.
        self.root.after(self._POLL * 1000/2, self.poll, True)

        return True 
Example #24
Source File: Modules.py    From Timeline with GNU General Public License v3.0 5 votes vote down vote up
def autoReloadModules(self, a):
		Observer = ModuleObserver()
		ModuleEventHandler = self

		Observer.schedule(ModuleEventHandler, path=self.module_parent.__path__[0], recursive=True)
		Observer.start() 
Example #25
Source File: pymonitor.py    From awesome-python3-webapp with GNU General Public License v2.0 5 votes vote down vote up
def start_watch(path, callback):
    observer = Observer()
    observer.schedule(MyFileSystemEventHander(restart_process), path, recursive=True)
    observer.start()
    log('Watching directory %s...' % path)
    start_process()
    try:
        while True:
            time.sleep(0.5)
    except KeyboardInterrupt:
        observer.stop()
    observer.join() 
Example #26
Source File: app.py    From revelation with MIT License 5 votes vote down vote up
def on_open(self):
        if self.tracking_path:
            event_handler = PresentationReloadWebSocketSendEvent(self.ws)
            self.observer = Observer()
            self.observer.schedule(event_handler, self.tracking_path)
            self.observer.start() 
Example #27
Source File: kwdb.py    From robotframework-hub with Apache License 2.0 5 votes vote down vote up
def __init__(self, dbfile=":memory:", poll=False):
        self.db = sqlite3.connect(dbfile, check_same_thread=False)
        self.log = logging.getLogger(__name__)
        self._create_db()
#        self.log.warning("I'm warnin' ya!")

        # set up watchdog observer to monitor changes to
        # keyword files (or more correctly, to directories
        # of keyword files)
        self.observer =  PollingObserver() if poll else Observer()
        self.observer.start() 
Example #28
Source File: __main__.py    From starbelly with MIT License 5 votes vote down vote up
def run(self):
        ''' Run the reloader. '''

        self._logger.info('Running with reloader...')
        self._watchdog = ProcessWatchdog()
        self._watchdog.start_process()

        self._observer = Observer()
        self._observer.schedule(
            self._watchdog, str(get_path('starbelly')), recursive=True)
        self._observer.start()

        while True:
            time.sleep(1) 
Example #29
Source File: folder.py    From ffplayout-engine with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, media):
        self._media = media

        self.event_handler = PatternMatchingEventHandler(
            patterns=_storage.extensions)
        self.event_handler.on_created = self.on_created
        self.event_handler.on_moved = self.on_moved
        self.event_handler.on_deleted = self.on_deleted

        self.observer = Observer()
        self.observer.schedule(self.event_handler, self._media.folder,
                               recursive=True)

        self.observer.start() 
Example #30
Source File: xml2mysql.py    From luscan-devel with GNU General Public License v2.0 5 votes vote down vote up
def monitor(path):
    event_handler = CrawlFileMonitor()
    observer = Observer()
    observer.schedule(event_handler, path, recursive=True)
    observer.start()
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()