当前位置: 首页>>代码示例>>Python>>正文


Python Observer.is_alive方法代码示例

本文整理汇总了Python中watchdog.observers.Observer.is_alive方法的典型用法代码示例。如果您正苦于以下问题:Python Observer.is_alive方法的具体用法?Python Observer.is_alive怎么用?Python Observer.is_alive使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在watchdog.observers.Observer的用法示例。


在下文中一共展示了Observer.is_alive方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: process

# 需要导入模块: from watchdog.observers import Observer [as 别名]
# 或者: from watchdog.observers.Observer import is_alive [as 别名]
    def process(self, received_file_queue, exception_handler):
        """
        The file watcher process.

        :param received_file_queue:     queue transfering the detected changed files
        :type received_file_queue:      multiprocessing.queues.Queue
        :param exception_handler:       exception handler callback method receiving all exception from the process
        :type exception_handler:        method
        """
        filesystem_observer = Observer()
        try:
            # required for correct handling of Crtl + C in a multiprocess environment
            signal.signal(signal.SIGINT, signal.SIG_IGN)
            signal.signal(signal.SIGTERM, signal.SIG_IGN)

            self._processed_files_lock = threading.Lock()

            self._received_file_queue = received_file_queue
            if not os.path.isdir(self._data_directory):
                raise IOError("Data directory \"%s\" not found." % self._data_directory)

            filesystem_observer.schedule(self, self._data_directory, recursive=True)
            filesystem_observer.start()

            # wait until stop is signalled
            self._exception_event.wait()
        except Exception as e:
            exception_handler(DelayedException(e))
        finally:
            if filesystem_observer.is_alive():
                filesystem_observer.stop()
                filesystem_observer.join()
开发者ID:erl987,项目名称:remoteweatheraccess,代码行数:34,代码来源:ftpbroker.py

示例2: __init__

# 需要导入模块: from watchdog.observers import Observer [as 别名]
# 或者: from watchdog.observers.Observer import is_alive [as 别名]
class Merger:
    def __init__(self, ledger_file, rules_file, yes_append, csv_files):
        self._ledger_file = ledger_file
        self._rules_file = rules_file
        self._yes_append = yes_append
        self._csv_files = csv_files
        self._observer = None

        def on_rules_modified(event):
            print()
            self.reload()
            self.print_transactions()
            self._prompt_append(wait=False)
        self._event_handler = PatternMatchingEventHandler([rules_file])
        self._event_handler.on_modified = on_rules_modified
        self._observer = Observer()
        self._observer.schedule(self._event_handler,
                                os.path.dirname(rules_file))

    def __del__(self):
        if self._observer and self._observer.is_alive():
            self._observer.stop()
            self._observer.join()

    def _prompt_append(self, wait=True):
        click.secho('\nLedger file: %s' % self._ledger_file, fg='yellow')
        click.secho('Append these transactions to Ledger file?',
                    fg='yellow', nl=False)
        if wait:
            return click.confirm('')
        else:
            click.secho(' [y/N]: ', nl=False)
            return

    def reload(self):
        print('\rReloading...\n')
        rules = Rules(self._rules_file)
        self.transactions = []
        self.unknown = []

        for csv_file in self._csv_files:
            click.secho('>>> {}'.format(csv_file), fg='blue')
            ts = []
            self.transactions.append((csv_file, ts))
            for t in get_transactions(csv_file, rules, self._ledger_file):
                print(t)
                print()
                ts.append(t)
                if t.account2 == 'expenses:unknown':
                    self.unknown.append(t)

    def print_transactions(self):
        if not any(x for (_, x) in self.transactions):
            click.secho('No new transactions found')
            return False

        if self.unknown:
            click.secho('{} transactions with unknown account:'
                        .format(len(self.unknown)), fg='yellow')
            for t in self.unknown:
                click.secho('   {}: {} (£{:0.2f})'
                            .format(t.date, t.description, t.amount))

    def main(self):
        self.reload()
        if self.print_transactions() is False:
            return

        self._observer.start()
        if self._ledger_file and (self._yes_append or self._prompt_append()):
            print('Appending...', end=' ')
            now = datetime.now().replace(microsecond=0)
            with open(self._ledger_file, 'ta') as f:
                for filename, txns in self.transactions:
                    print('; Converted from {}'.format(filename), file=f)
                    print('; [{}]\n'.format(now), file=f)
                    for t in txns:
                        print(str(t) + '\n', file=f)
            print('done')
开发者ID:ricklupton,项目名称:ledger-csv-merge,代码行数:81,代码来源:ledger_csv_merge.py

示例3: APIDocumentObserver

# 需要导入模块: from watchdog.observers import Observer [as 别名]
# 或者: from watchdog.observers.Observer import is_alive [as 别名]
class APIDocumentObserver(with_metaclass(SingletonMeta, object)):
    """
    ``APIDocumentObserver`` is observer of API Documents.
    """

    def __init__(self, doc_path=None, doc_index_path=None, doc_file_path_list=None):
        """
        Construct method of ``APIDocumentObserver``.

        :param doc_path: API Document Directory path
        :param doc_index_path: API Document index path
        :param doc_file_path_list: Document file list ``ORDER`` in ``index.json``
        :return:  APIDocumentObserver Singleton instance
        """
        from watchdog.observers import Observer
        from .document_trace_handler import DocumentTraceHandler
        from .document_trace_file import DocumentTraceFile

        if doc_path:
            if doc_file_path_list and isinstance(doc_file_path_list, list):
                tracing_files = [DocumentTraceFile(tracing_file_path=f) for f in doc_file_path_list]
            else:
                tracing_files = list()

            if doc_index_path:
                tracing_files.append(DocumentTraceFile(tracing_file_path=doc_index_path, is_index_file=True))

            event_handler = DocumentTraceHandler(tracing_files=tracing_files)
            self.observer = Observer()
            self.observer.schedule(event_handler, doc_path, recursive=True)
        else:
            raise Exception("doc_path is None.")

    def start_watch(self):
        """
        Start watch docs
        """

        if self.observer:
            self.observer.start()
            logger.info("Start watchdocs..")
        else:
            pass

    @property
    def is_started(self):
        """
        After run ``start_watch()``, ``is_started`` is True, or False.

        :return: True | False
        """

        if self.observer:
            return self.observer.is_alive()
        return False

    def stop_watch(self):
        """
        Stop watch docs
        """

        if self.observer and self.observer.is_alive():
            self.observer.stop()
            self.observer.join()

            logger.info("Stop watchdocs..")
开发者ID:gogit,项目名称:plate,代码行数:68,代码来源:api_document_observer.py

示例4: LocalSyncWorker

# 需要导入模块: from watchdog.observers import Observer [as 别名]
# 或者: from watchdog.observers.Observer import is_alive [as 别名]
class LocalSyncWorker(ConsolidatedEventHandler, metaclass=Singleton):
    def __init__(self):
        super().__init__()

        self.ignore = threading.Event()

        try:
            user = get_current_user()
        except NoResultFound:
            # TODO: This only happens when a user logs out and the db has
            # been cleared. The app tries to run again without a user being
            # being set. This error doesn't disrupt user experience, but we
            # might consider tracking down the specific source and preventing
            # it from happening.
            raise
        self.folder = user.folder

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

    def start(self):
        logger.debug('Starting watchdog observer')
        self.observer.start()

    def stop(self):
        logger.debug('Stopping LocalSyncWorker')
        # observer is actually a separate child thread and must be join()ed
        self.observer.stop()
        self.join()

    def is_alive(self):
        return self.observer.is_alive()

    def join(self):
        self.observer.join()
        logger.debug('LocalSyncWorker Stopped')

    def dispatch(self, event):
        if self.ignore.is_set():
            return logger.debug('Ignoring event {}'.format(event))
        super().dispatch(event)

    def on_moved(self, event):
        logger.info('Move event for {}: from {} to {}'.format('directory' if event.is_directory else 'file', event.src_path, event.dest_path))

        if is_ignored(event.dest_path):
            logger.info('Removing {} {} from DB, it was moved to an ignored file'.format('directory' if event.is_directory else 'file', event.src_path, event.dest_path))
            context = OperationContext(local=Path(event.src_path), check_is_folder=False)
            return self.put_event(operations.DatabaseDelete(context))

        # Note: OperationContext should extrapolate all attributes from what it is given
        if event.is_directory:
            try:
                # TODO: avoid a lazy context load in this case to catch the NodeNotFound exception?
                _ = OperationContext(local=Path(event.src_path), is_folder=True).remote
                return self.put_event(operations.RemoteMoveFolder(
                    OperationContext(local=Path(event.src_path), is_folder=True),
                    OperationContext(local=Path(event.dest_path), is_folder=True),
                ))
            except NodeNotFound:
                return self.put_event(operations.RemoteCreateFolder(
                    OperationContext(local=Path(event.dest_path), is_folder=True),
                ))

        try:
            # TODO: avoid a lazy context load in this case to catch the NodeNotFound exception?
            _ = OperationContext(local=Path(event.src_path)).remote  # noqa
            return self.put_event(operations.RemoteMoveFile(
                OperationContext(local=Path(event.src_path)),
                OperationContext(local=Path(event.dest_path)),
            ))
        except NodeNotFound:
            return self.put_event(operations.RemoteCreateFile(
                OperationContext(local=Path(event.dest_path)),
            ))

    def on_created(self, event):
        logger.info('Creation event for {}: {}'.format('directory' if event.is_directory else 'file', event.src_path))
        node = utils.extract_node(event.src_path)
        path = Path(event.src_path)

        # If the file exists in the database, this is a modification
        # This logic may not be the most correct, #TODO re-evaluate
        if utils.local_to_db(path, node):
            return self.on_modified(event)

        context = OperationContext(local=path, node=node)

        if event.is_directory:
            return self.put_event(operations.RemoteCreateFolder(context))
        return self.put_event(operations.RemoteCreateFile(context))

    def on_deleted(self, event, *args, is_folder=False, **kwargs):
        logger.info('Deletion event for {}: {}'.format('directory' if event.is_directory else 'file', event.src_path))
        # A hack: override checking if the passed path is a directory. Since Windows
        # emits folder deletion events as file deletes we need to ignore whether or not
        # a delete event is for a folder. Since the RemoteDelete operation works identically
        # for files and folders we can get away with this here.
        context = OperationContext(local=Path(event.src_path), check_is_folder=False)
        context.db  # noqa
#.........这里部分代码省略.........
开发者ID:CenterForOpenScience,项目名称:osf-sync,代码行数:103,代码来源:local.py

示例5: Client

# 需要导入模块: from watchdog.observers import Observer [as 别名]
# 或者: from watchdog.observers.Observer import is_alive [as 别名]

#.........这里部分代码省略.........
        Performs a :func:`~select.select` on the components' error queue.
        When a notification is detected, the client will log the message and
        then calculate if the Postgres server is still a Master -  if not, the
        components are shutdown.
        """
        super(Client, self).run()
        self._start_observer()

        signal(SIGCHLD, self._handle_sigchld)

        self._should_run = True

        self.execute_role_based_procedure()
        while self._should_run:
            self._exception_raised = self._child_interrupted = False
            try:
                exit_pipe = self._exit_queue._reader

                ready_pipes, _, _ = select.select(
                    (exit_pipe, ), (), ()
                )

                if exit_pipe in ready_pipes:
                    self.terminate()

            except select.error:
                if not self._child_interrupted and not self._exception_raised:
                    self._should_run = False

    def _start_components(self, restart=False):
        """
        Starts the Processor and Listener if the client is not running
        """
        if not self._processor.is_alive():
            if restart and self._processor.ident:
                self._processor.join()
            self._processor.start()

        if not self._listener.is_alive():
            if restart and self._listener.ident:
                self._listener.join()
            self._listener.start()

    def _stop_components(self):
        """
        Stops the Processor and Listener if the client is running
        """
        if self._listener and self._listener.ident and self._listener.is_alive():
            self._listener.terminate()
            self._listener.join()

        if self._processor and self._processor.ident and self._processor.is_alive():
            self._processor.terminate()
            self._processor.join()

    def _start_observer(self):
        """
        Schedules the observer using 'settings.WATCH_PATH'
        """
        if self._watch_path:
            self.directory_observer.schedule(
                self, self._watch_path, recursive=False
            )
            self.directory_observer.start()

    def _stop_observer(self):
开发者ID:transifex,项目名称:hermes,代码行数:70,代码来源:client.py

示例6: FileMonitor

# 需要导入模块: from watchdog.observers import Observer [as 别名]
# 或者: from watchdog.observers.Observer import is_alive [as 别名]
class FileMonitor(FileSystemEventHandler):
	def __init__(self):
		self._observer = Observer()

		self._event_callback = {
			'modified': [],
			'moved': [],
			'created': [],
			'deleted': [],
		}

	def start(self, directory, recursive=False):
		self._observer.schedule(self, directory, recursive)
		self._observer.start()

	def reset(self, directory, recursive=False):
		if self._observer.is_alive():
			self._observer.unschedule_all()
			self._observer.stop()
			self._observer.join()
		
		self._observer.schedule(self, directory, recursive)
		self._observer.start()

	def stop(self):
		self._observer.unschedule_all()
		self._observer.stop()
		self._observer.join()

	def bind(self, event, func):
		if event in self._event_callback:
			self._event_callback[event].append(func)
			return True

		return False
			
	def on_moved(self, event):
		"""Called when a file or a directory is moved or renamed.

		:param event:
			Event representing file/directory movement.
		:type event:
			:class:`DirMovedEvent` or :class:`FileMovedEvent`
		"""
		print( 'moved', event.src_path, event.dest_path )
		for cb in self._event_callback['moved']:
			cb(event)


	def on_created(self, event):
		"""Called when a file or directory is created.

		:param event:
			Event representing file/directory creation.
		:type event:
			:class:`DirCreatedEvent` or :class:`FileCreatedEvent`
		"""
		print( 'created', event.src_path )
		for cb in self._event_callback['created']:
			cb(event)


	def on_deleted(self, event):
		"""Called when a file or directory is deleted.

		:param event:
			Event representing file/directory deletion.
		:type event:
			:class:`DirDeletedEvent` or :class:`FileDeletedEvent`
		"""
		print( 'deleted', event.src_path )
		for cb in self._event_callback['deleted']:
			cb(event)


	def on_modified(self, event):
		"""Called when a file or directory is modified.

		:param event:
			Event representing file/directory modification.
		:type event:
			:class:`DirModifiedEvent` or :class:`FileModifiedEvent`
		"""
		print( 'modified', event.src_path )
		for cb in self._event_callback['modified']:
			cb(event)
开发者ID:albertclass,项目名称:XStudio,代码行数:88,代码来源:monitor.py

示例7: FileModifyWatcher

# 需要导入模块: from watchdog.observers import Observer [as 别名]
# 或者: from watchdog.observers.Observer import is_alive [as 别名]
class FileModifyWatcher(object):
    """Use inotify to watch file-system for file modifications

    Usage:
    1) subclass the method handle_event, action to be performed
    2) create an object passing a list of files to be watched
    3) call the loop method
    """
    supported_platforms = ('Darwin', 'Linux')
    
    def __init__(self, path_list):
        """@param file_list (list-str): files to be watched"""
        self.file_list = set()
        self.watch_dirs = set() # all dirs to be watched
        self.notify_dirs = set() # dirs that generate notification whatever file
        for filename in path_list:
            path = os.path.abspath(filename)
            if os.path.isfile(path):
                self.file_list.add(path)
                self.watch_dirs.add(os.path.dirname(path))
            else:
                self.notify_dirs.add(path)
                self.watch_dirs.add(path)
        self.platform = get_platform_system()
        if self.platform not in self.supported_platforms:
            msg = "Unsupported platform '%s'\n" % self.platform
            msg += ("'auto' command is supported only on %s" %
                    (self.supported_platforms,))
            raise Exception(msg)
        self.observer = Observer()
        self.observer.start()
        while not self.observer.is_alive():
            time.sleep(0.1)


    def __del__(self):
        self.observer.stop()
        self.observer.join()
        
    def _handle(self, event):
        """calls platform specific handler"""
        filename = event.src_path

        if (filename in self.file_list or
            os.path.dirname(filename) in self.notify_dirs):
            self.handle_event(event)

    def handle_event(self, event):
        """this should be sub-classed """
        raise NotImplementedError

    def _loop(self):
        handler = self._handle
        observer = self.observer
        
        class EventHandler(FileSystemEventHandler):
            def on_modified(self, event):
                try:
                    handler(event)
                except KeyboardInterrupt:
                    pass

        event_handler = EventHandler()

        for watch_this in self.watch_dirs:
            observer.schedule(event_handler, watch_this, recursive=False)
        
    def loop(self):
        """Infinite loop watching for file modifications

        """

        self._loop()
开发者ID:barleyj,项目名称:doit,代码行数:75,代码来源:filewatch.py


注:本文中的watchdog.observers.Observer.is_alive方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。