Created
October 13, 2012 15:59
-
-
Save jan-matejka/3885116 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #! /usr/bin/env python | |
| # -*- coding: utf-8 -*- | |
| from twisted.trial import unittest | |
| from twisted.python.filepath import FilePath | |
| from twisted.internet import inotify | |
| from twisted.python import filepath | |
| from abc import ABCMeta, abstractmethod | |
| from tempfile import mkdtemp, mkstemp | |
| from os.path import join | |
| def humanizeMask(fn): | |
| def wrap(_, filepath, mask): | |
| return fn(filepath, inotify.humanReadableMask(mask)) | |
| return wrap | |
| class AbstractMonitor(object): | |
| __metaclass__ = ABCMeta | |
| def __init__(self, path): | |
| self.filepath = filepath.FilePath(path) | |
| notifier = inotify.INotify() | |
| notifier.startReading() | |
| notifier.watch(self.filepath, | |
| callbacks=[ | |
| humanizeMask(self.event) | |
| ]) | |
| self.notifier = notifier | |
| @abstractmethod | |
| def event(self, fp, mask): | |
| """ | |
| @param fp L{twisted.filepath.FilePath} | |
| @param mask str L{twisted.internet.inotify.humanReadableMask} | |
| """ | |
| class MaildirMonitor(AbstractMonitor): | |
| def event(self, fp, mask): | |
| if 'modify' in mask: | |
| pass | |
| ################ | |
| class CrashTest(object): | |
| def test_does_not_crash(self): | |
| monitor = self.klass(self.path) | |
| if monitor.filepath.isdir(): | |
| target = FilePath(join(monitor.filepath.path, 'random')) | |
| elif monitor.filepath.isfile(): | |
| target = monitor.filepath | |
| else: | |
| raise ValueError() | |
| target.open("w").write("random crap") | |
| self.monitor = monitor | |
| def tearDown(self): | |
| self.monitor.notifier.loseConnection() | |
| class TestMaildirMonitor(unittest.TestCase, CrashTest): | |
| klass = MaildirMonitor | |
| path = mkdtemp() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment