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


Python Observer.run方法代码示例

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


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

示例1: watch

# 需要导入模块: from watchdog.observers import Observer [as 别名]
# 或者: from watchdog.observers.Observer import run [as 别名]
def watch(path, pipe):
    path = path
    pipe = pipe
    observer = Observer()
    handler = FSHandler(pipe)
    observer.schedule(handler, path=path, recursive=True)
    # Write a null to the pipe so we know we're ready
    os.write(pipe, chr(0))
    observer.run()
开发者ID:Byzantium,项目名称:groundstation,代码行数:11,代码来源:fs_watcher.py

示例2: run

# 需要导入模块: from watchdog.observers import Observer [as 别名]
# 或者: from watchdog.observers.Observer import run [as 别名]
 def run(self, *args, **kwargs):
     try:
         Observer.run(self, *args, **kwargs)
     except:
         pass
开发者ID:davislg,项目名称:ninja-ide,代码行数:7,代码来源:file_manager.py

示例3: Command

# 需要导入模块: from watchdog.observers import Observer [as 别名]
# 或者: from watchdog.observers.Observer import run [as 别名]
class Command(BaseCommand):
    monitor = None

    option_list = BaseCommand.option_list + (
        make_option("--path",
            action="store",
            dest="path",
            help="Music library path to watch"
        ),
        make_option("--start",
            action="store_true",
            dest="start",
            help="Start watching directory for changes"
        ),
        make_option("--stop",
            action="store_true",
            dest="stop",
            help="Stop watching directory for changes"
        ),
    )

    def handle(self, *args, **options):
        if options["path"] is None:
            print "Required arguments: path"
            return

        if not os.path.exists(options["path"]):
            print "Path does not exist"
            return

        pidFile = os.path.dirname(
            os.path.abspath(__file__)
        ) + "/../../jukebox_watch.pid"

        if options["start"]:
            if os.path.exists(pidFile):
                print "Watch daemon already running, pid file exists"
                return

            self.watch(pidFile, options['path'])
        elif options["stop"]:
            if not os.path.exists(pidFile):
                print "Daemon not running"
                return

            print "Stopping daemon..."
            pid = int(open(pidFile).read())
            os.kill(pid, SIGTSTP)

    def watch(self, pidFile, path):
        pid = daemon.pidfile.TimeoutPIDLockFile(
            pidFile,
            10
        )

        print "Starting jukebox_watch daemon..."
        self.daemon = daemon.DaemonContext(
            uid=os.getuid(),
            gid=os.getgid(),
            pidfile=pid,
            working_directory=os.getcwd(),
            detach_process=True,
            signal_map={
                SIGTSTP: self.shutdown
            }
        )

        with self.daemon:
            # create watchmanager, eventhandler and notifier
            handler = EventHandler()

            self.monitor = Observer()
            self.monitor.schedule(handler, path, recursive=True)
            self.monitor.run()

    def shutdown(self, signal, action):
        if self.monitor is not None:
            self.monitor.stop()
        self.daemon.close()
        sys.exit(0)
开发者ID:lociii,项目名称:jukebox_live_indexer,代码行数:82,代码来源:jukebox_live_indexer.py

示例4: AutoReloadWatchdog

# 需要导入模块: from watchdog.observers import Observer [as 别名]
# 或者: from watchdog.observers.Observer import run [as 别名]
class AutoReloadWatchdog(object):
    def __init__(self, server):
        from watchdog.observers import Observer
        from watchdog.events import FileSystemEventHandler
        from watchdog.events import FileCreatedEvent
        from watchdog.events import FileModifiedEvent
        
        self.server = server
        self.files = {}
        self.modules = {}
        class EventHandler(FileSystemEventHandler):
            def __init__(self, autoreload):
                self.autoreload = autoreload
            
            def process(self):
                l = self.autoreload.files.keys()
                self.autoreload.files.clear()
                self.autoreload.process_data(l)
                self.autoreload.process_python(l)
            
            def on_created(self, event):
                if isinstance(event, FileCreatedEvent):
                    _logger.debug('File created: %s', event.src_path)
                    self.autoreload.files[event.src_path] = 1
                    self.process()
            
            def on_modified(self, event):
                if isinstance(event, FileModifiedEvent):
                    _logger.debug('File modified: %s', event.src_path)
                    self.autoreload.files[event.src_path] = 1
                    self.process()

        #self.wm = pyinotify.WatchManager()
        self.handler = EventHandler(self)
        self.observer = Observer()
        #self.notifier = pyinotify.Notifier(self.wm, self.handler, timeout=0)
        #mask = pyinotify.IN_MODIFY | pyinotify.IN_CREATE  # IN_MOVED_FROM, IN_MOVED_TO ?
        for path in openerp.tools.config.options["addons_path"].split(','):
            _logger.info('Watching addons folder %s', path)
            #self.wm.add_watch(path, mask, rec=True)
            self.observer.schedule(self.handler, path=path, recursive=True)

    def process_data(self, files):
        xml_files = [i for i in files if i.endswith('.xml')]
        addons_path = openerp.tools.config.options["addons_path"].split(',')
        for i in xml_files:
            for path in addons_path:
                if i.startswith(path):
                    # find out which addons path the file belongs to
                    # and extract it's module name
                    right = i[len(path) + 1:].split('/')
                    if len(right) < 2:
                        continue
                    module = right[0]
                    self.modules[module]=1
        if self.modules:
            _logger.info('autoreload: xml change detected, autoreload activated')
            openerp.service.server.restart()

    def process_python(self, files):
        # process python changes
        py_files = [i for i in files if i.endswith('.py')]
        py_errors = []
        # TODO keep python errors until they are ok
        if py_files:
            for i in py_files:
                try:
                    source = open(i, 'rb').read() + '\n'
                    compile(source, i, 'exec')
                except SyntaxError:
                    py_errors.append(i)
            if py_errors:
                _logger.info('autoreload: python code change detected, errors found')
                for i in py_errors:
                    _logger.info('autoreload: SyntaxError %s',i)
            else:
                _logger.info('autoreload: python code updated, autoreload activated')
                openerp.service.server.restart()

    def check_thread(self):
        # Check if some files have been touched in the addons path.
        # If true, check if the touched file belongs to an installed module
        # in any of the database used in the registry manager.
        self.observer.run()

    def run(self):
        t = threading.Thread(target=self.check_thread)
        t.setDaemon(True)
        t.start()
        _logger.info('AutoReload watcher running')
开发者ID:Nucleoos,项目名称:saas3,代码行数:92,代码来源:autoreload_watchdog.py


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