Пример #1
0
class Maildir:
    def __init__(self, basedir):
        self.basedir = basedir
        self.newdir = os.path.join(basedir, "new")
        self.files = []
        self.pollinterval = 10  # only used if we don't have DNotify
        self.running = 0

    def start(self):
        """You must run start to receive any messages."""
        if self.running:
            return
        self.running = 1
        if not os.path.isdir(self.basedir) or not os.path.isdir(self.newdir):
            raise "invalid maildir '%s'" % basedir
        # we must hold an fd open on the directory, so we can get notified
        # when it changes.
        if have_dnotify:
            self.dnotify = DNotify(self.newdir, self.dnotify_callback,
                                   [DNotify.DN_CREATE])
        self.poll()

    def startTimeout(self):
        raise NotImplemented
    def stopTimeout(self):
        raise NotImplemented
    def dnotify_callback(self):
        print "callback"
        self.poll()
        raise NotImplemented

    def stop(self):
        if have_dnotify:
            self.dnotify.remove()
            del(self.dnotify)
        else:
            self.stopTimeout()
        self.running = 0

    def poll(self):
        # see what's new
        for f in self.files:
            if not os.path.isfile(os.path.join(self.newdir, f)):
                self.files.remove(f)
        newfiles = []
        for f in os.listdir(self.newdir):
            if not f in self.files:
                newfiles.append(f)
        self.files.extend(newfiles)
        for n in newfiles:
            self.messageReceived(os.path.join("new",n))
        if not have_dnotify:
            self.startTimeout()

    def messageReceived(self, filename):
        """Called when a new file is noticed. Override it in subclasses."""
        print filename
Пример #2
0
 def start(self):
     """You must run start to receive any messages."""
     if self.running:
         return
     self.running = 1
     if not os.path.isdir(self.basedir) or not os.path.isdir(self.newdir):
         raise "invalid maildir '%s'" % basedir
     # we must hold an fd open on the directory, so we can get notified
     # when it changes.
     if have_dnotify:
         self.dnotify = DNotify(self.newdir, self.dnotify_callback,
                                [DNotify.DN_CREATE])
     self.poll()