English 中文(简体)
PyGTK/GIO: 反省变化监测名录
原标题:PyGTK/GIO: monitor directory for changes recursively
  • 时间:2010-06-18 10:29:59
  •  标签:
  • python
  • pygtk

采用以下标准(从GIO回答 to this question),该标准使用全球信息网络File Monitor监测某项变化的名录:

import gio

def directory_changed(monitor, file1, file2, evt_type):
    print "Changed:", file1, file2, evt_type

gfile = gio.File(".")
monitor = gfile.monitor_directory(gio.FILE_MONITOR_NONE, None)
monitor.connect("changed", directory_changed) 

import glib
ml = glib.MainLoop()
ml.run()

之后,我可以制定和修改儿童节点,并通报这些变化。 然而,这只是为眼前的儿童所为(我知道,没有其他说法)。 以下最后一批军舰将不通知:

touch one
mkdir two
touch two/three

是否容易使之失去活力? 我不是手工地编造一些期望制作目录和增加监测、删除内容等内容。

打算使用VCS文档浏览器,以便能够在一份工作复印件中处理档案状况,并单独更新。 因此,在任何地方,从数万至数千个(或更多)的名录都可以监测。 我只想找到工作复制件的根基,并在此添加档案监测器。

我知道pyinotification,但I m避免了,这样,在自由BSD或他人等非Brykernels下进行这项工作。 我知道,全球海洋研究组织档案管理员在有可用情况下使用无谓的地下室,我可以理解,不强调执行,以保持一定程度的抽象,但我向我建议,这样做是可能的。

(如果情况是,我最初在上张贴。

最佳回答

我不敢肯定,全球海洋研究组织是否允许过一次以上的监测,但如果它没有任何理由,你可以做这样的事情:

import gio
import os

def directory_changed(monitor, file1, file2, evt_type):
    if os.path.isdir(file2):    #maybe this needs to be file1?
        add_monitor(file2) 
    print "Changed:", file1, file2, evt_type

def add_monitor(dir):
    gfile = gio.File(dir)
    monitor = gfile.monitor_directory(gio.FILE_MONITOR_NONE, None)
    monitor.connect("changed", directory_changed) 

add_monitor( . )

import glib
ml = glib.MainLoop()
ml.run()

* 在我没有说明理由时,这有可能变成资源 h,尽管对全球海洋学会一号的几乎无所知。 完全有可能用几条指令(os.listdir等)。 也许会看像这样的情况。

import time
import os

class Watcher(object):
    def __init__(self):
        self.dirs = []
        self.snapshots = {}

    def add_dir(self, dir):
        self.dirs.append(dir)

    def check_for_changes(self, dir):
        snapshot = self.snapshots.get(dir)
        curstate = os.listdir(dir)
        if not snapshot:
            self.snapshots[dir] = curstate
        else:
            if not snapshot == curstate:
                print  Changes:  ,
                for change in set(curstate).symmetric_difference(set(snapshot)):
                    if os.path.isdir(change):
                        print "isdir"
                        self.add_dir(change)
                    print change,

                self.snapshots[dir] = curstate
                print

    def mainloop(self):
        if len(self.dirs) < 1:
            print "ERROR: Please add a directory with add_dir()"
            return

        while True:
            for dir in self.dirs:
                self.check_for_changes(dir)
            time.sleep(4) # Don t want to be a resource hog

w = Watcher()
w.add_dir( . )


w.mainloop()
问题回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签