コード例 #1
0
ファイル: file_monitor_linux.py プロジェクト: PlumpMath/pygel
 def __init__(self, reactor=None):
     self.reactor = reactor
     if reactor is None:
         self.reactor = Gel()
     self._notify = Inotify()
     self.watching = {}
     self.watching_wd = {}
     self.watching_file = {}
     monitor = IO_IN | IO_OUT
     self.reactor.register_io(self._notify, self._callback, monitor)
コード例 #2
0
ファイル: test_gel.py プロジェクト: PlumpMath/pygel
    def test_factory_use_qt5_reactor(self):
        from gel.reactor.qt5_reactor import Qt5Reactor
        from PyQt5.QtCore import QCoreApplication

        # we need an qt application in orther to use a qt reactor
        app = QCoreApplication(sys.argv)

        gel = Gel.from_reactor(event_loop='qt5')
        self.assertIsInstance(gel._reactor, Qt5Reactor)

        gel = Gel.from_reactor(event_loop='pyqt5')
        self.assertIsInstance(gel._reactor, Qt5Reactor)

        gel = Gel.from_reactor(event_loop='qt')
        self.assertIsInstance(gel._reactor, Qt5Reactor)
コード例 #3
0
ファイル: test_gel.py プロジェクト: caetanus/pygel
    def test_factory_use_qt5_reactor(self):
        from gel.reactor.qt5_reactor import Qt5Reactor
        from PyQt5.QtCore import QCoreApplication

        # we need an qt application in orther to use a qt reactor
        app = QCoreApplication(sys.argv)

        gel = Gel.from_reactor(event_loop='qt5')
        self.assertIsInstance(gel._reactor, Qt5Reactor)

        gel = Gel.from_reactor(event_loop='pyqt5')
        self.assertIsInstance(gel._reactor, Qt5Reactor)

        gel = Gel.from_reactor(event_loop='qt')
        self.assertIsInstance(gel._reactor, Qt5Reactor)
コード例 #4
0
 def setUp(self):
     try:
         self.tearDown()
     except:
         pass
     os.mkdir(directory)
     self.reactor = Gel()
     self.file_watch = file_monitor_linux.FileWatch(self.reactor)
コード例 #5
0
ファイル: file_monitor_linux.py プロジェクト: caetanus/pygel
 def __init__(self, reactor=None):
     self.reactor = reactor
     if reactor is None:
         self.reactor = Gel()
     self._notify = Inotify()
     self.watching = {}
     self.watching_wd = {}
     self.watching_file = {}
     monitor = IO_IN | IO_OUT
     self.reactor.register_io(self._notify, self._callback, monitor)
コード例 #6
0
ファイル: test_gel.py プロジェクト: PlumpMath/pygel
 def setUp(self):
     self.reactor = Gel()
コード例 #7
0
ファイル: test_gel.py プロジェクト: PlumpMath/pygel
class GelTestCase(unittest.TestCase):
    def setUp(self):
        self.reactor = Gel()

    def test_timeout_call(self):
        A = 1
        B = 2
        C = 3

        @gel_main(self.reactor)
        def timer():
            run_time = time.time()

            @gel_quit(self.reactor)
            def callback(a, b, c):
                cb_time = time.time()
                self.assertEqual(A, a)
                self.assertEqual(B, b)
                self.assertEqual(C, c)
                # check if the timer was dispared between 1 and 2 seconds
                # as we can't really tell if the time will be triggered exactly on time
                self.assertGreaterEqual(cb_time + .1, run_time)
                self.assertLessEqual(run_time, cb_time + 2)

            self.reactor.timeout_seconds_call(.1, callback, A, B, c=C)

        timer()

    def test_idle_call(self):

        A = 1
        B = 2
        C = 3

        @gel_main(self.reactor)
        def call_later():
            @gel_quit(self.reactor)
            def callback(a, b, c):
                self.assertEqual(A, a)
                self.assertEqual(B, b)
                self.assertEqual(C, c)

            self.reactor.idle_call(callback, A, B, c=C)

        call_later()

    def test_socket_accept(self):
        @gel_main(self.reactor)
        def actual_test():
            import socket
            port_generator = port_generator_helper()
            while True:
                s = socket.socket()
                try:
                    s.bind(("127.0.0.1", six.next(port_generator)))
                except socket.error:
                    continue
                s.listen(1)
                break

            @gel_quit(self.reactor)
            def callback(event):
                socket, addr = event.accept()
                self.assertIs(event, s)

            self.reactor.register_io(s, callback)
            client = socket.socket()
            client.connect(s.getsockname())

        actual_test()

    def test_main_iteration_with_block_false_should_return_True_with_no_events(
            self):
        self.assertTrue(self.reactor.main_iteration(block=False))

    def test_sleep(self):
        start = time.time()
        self.reactor.sleep(200)
        current = time.time()
        # assure that the sleep time is almost the sleep we asked for
        wait_time = current - start
        self.assertLessEqual(wait_time, .220)
        self.assertGreaterEqual(wait_time, .190)

    def test_wait_task(self):
        def threaded_task(a, b):
            return a * b

        ret = self.reactor.wait_task(threaded_task, 3, 4)
        self.assertEqual(ret, 3 * 4)

    def test_wait_task_throwns_an_exception_inside_thread_should_raises_that_exception_in_the_current_execution_flow(
            self):
        def threaded_task():
            return 0 / 0

        self.assertRaises(ZeroDivisionError, self.reactor.wait_task,
                          threaded_task)

    def test_threaded_wrapper(self):
        @self.reactor.threaded_wrapper
        def threaded_task():
            time.sleep(0.1)
            return 10

        self.assertEqual(10, threaded_task())

    def test_selector(self):
        @gel_main(self.reactor)
        def actual_test():

            s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

            s.sendto(b"", ("", 1))
            _, port = s.getsockname()

            def timeout():
                s.sendto(b"\x00", s.getsockname())

            self.reactor.timeout_seconds_call(0.1, timeout)
            response = self.reactor.selector([s])

            self.assertEqual(s, response)
            self.assertEqual(s.recvfrom(1),
                             (b'\x00', ('127.0.0.1', s.getsockname()[1])))
            self.reactor.main_quit()

        actual_test()

    def test_selector_with_timeout_timed_out(self):
        @gel_main(self.reactor)
        def actual_test():

            s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            s.sendto(b"", ("", 1))
            _, port = s.getsockname()

            def timeout():
                s.sendto(b"\x00", s.getsockname())

            self.reactor.timeout_seconds_call(0.3, timeout)
            response = self.reactor.selector([s], 0.1)

            self.assertIsNone(response)
            self.reactor.main_quit()

        actual_test()

    def test_selector_with_timeout(self):
        @gel_main(self.reactor)
        def actual_test():

            s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

            s.sendto(b"", ("", 1))
            _, port = s.getsockname()

            def timeout():
                s.sendto(b"\x00", s.getsockname())

            self.reactor.timeout_seconds_call(0.01, timeout)
            response = self.reactor.selector([s], 0.2)

            self.assertEqual(s, response)
            self.assertEqual(s.recvfrom(1),
                             (b'\x00', ('127.0.0.1', s.getsockname()[1])))
            self.reactor.main_quit()

        actual_test()
コード例 #8
0
ファイル: test_gel.py プロジェクト: PlumpMath/pygel
 def test_factory_with_no_arguments_should_use_gel_reactor(self):
     from gel.reactor.gel_reactor import GelReactor
     gel = Gel.from_reactor()
     self.assertIsInstance(gel._reactor, GelReactor)
コード例 #9
0
ファイル: file_monitor_linux.py プロジェクト: caetanus/pygel
class FileWatch(object):

    def __init__(self, reactor=None):
        self.reactor = reactor
        if reactor is None:
            self.reactor = Gel()
        self._notify = Inotify()
        self.watching = {}
        self.watching_wd = {}
        self.watching_file = {}
        monitor = IO_IN | IO_OUT
        self.reactor.register_io(self._notify, self._callback, monitor)

    def _callback(self, *args):
        for i in self._notify.read():
            wd, _, _, _ = i
            try:
                #TODO ADD a way to return the file modified, created or etc if the observing is a directory
                path, mask = self.watching_file[wd]
                cb = self.watching_wd[wd]
                cb(path)
                return True

            except Exception as e:
                #raise
                pass
            try:
                self.watching_wd[wd](wd)
            except:
                pass
        return True

    def watch_file(self, path, callback, mask=IN_MODIFY | IN_DELETE_SELF |
                                    IN_MOVE_SELF | IN_MASK_ADD | IN_ATTRIB):
        if not callable(callback):
            raise TypeError("callback must be a function.")

        if not os.path.exists(path):
            raise FileError("the path '%s' doesn't exists." % path)

        if os.path.isdir(path):
            raise FileError("the path '%s' is a directory." % path)
        if path in self.watching:
            self.remove_watch(path)
        wd = self._notify.add_watch(path, mask)
        self.watching[path] = wd
        self.watching_wd[wd] = callback
        self.watching_file[wd] = (path, mask)

    def watch_directory(self, path, callback, mask=IN_CREATE | IN_DELETE |
                                    IN_WRITE |IN_MOVED_FROM |
                                    IN_MOVED_TO | IN_MOVE_SELF):
        if not os.path.isdir(path):
            raise FileError(("the path '%s' doesn't exists"
                             " or is not a directory") % path)
        if not callable(callback):
            raise TypeError("callback must be a function.")

        if path in self.watching:
            self.remove_watch(path)

        wd = self._notify.add_watch(path, mask)
        self.watching[path] = wd
        self.watching_wd[wd] = callback
        self.watching_file[wd] = (path, mask)

    def remove_watch(self, path):
        try:
            wd = self.watching[path]
            self._notify.rm_watch(wd)
            del self.watching[path]
            del self.watching_wd[wd]
        except:
            raise FileWatchError("not watching path %s" % path)
コード例 #10
0
ファイル: file_monitor_linux.py プロジェクト: PlumpMath/pygel
class FileWatch(object):
    def __init__(self, reactor=None):
        self.reactor = reactor
        if reactor is None:
            self.reactor = Gel()
        self._notify = Inotify()
        self.watching = {}
        self.watching_wd = {}
        self.watching_file = {}
        monitor = IO_IN | IO_OUT
        self.reactor.register_io(self._notify, self._callback, monitor)

    def _callback(self, *args):
        for i in self._notify.read():
            wd, _, _, _ = i
            try:
                #TODO ADD a way to return the file modified, created or etc if the observing is a directory
                path, mask = self.watching_file[wd]
                cb = self.watching_wd[wd]
                cb(path)
                return True

            except Exception as e:
                #raise
                pass
            try:
                self.watching_wd[wd](wd)
            except:
                pass
        return True

    def watch_file(self,
                   path,
                   callback,
                   mask=IN_MODIFY | IN_DELETE_SELF | IN_MOVE_SELF | IN_MASK_ADD
                   | IN_ATTRIB):
        if not callable(callback):
            raise TypeError("callback must be a function.")

        if not os.path.exists(path):
            raise FileError("the path '%s' doesn't exists." % path)

        if os.path.isdir(path):
            raise FileError("the path '%s' is a directory." % path)
        if path in self.watching:
            self.remove_watch(path)
        wd = self._notify.add_watch(path, mask)
        self.watching[path] = wd
        self.watching_wd[wd] = callback
        self.watching_file[wd] = (path, mask)

    def watch_directory(self,
                        path,
                        callback,
                        mask=IN_CREATE | IN_DELETE | IN_WRITE | IN_MOVED_FROM
                        | IN_MOVED_TO | IN_MOVE_SELF):
        if not os.path.isdir(path):
            raise FileError(("the path '%s' doesn't exists"
                             " or is not a directory") % path)
        if not callable(callback):
            raise TypeError("callback must be a function.")

        if path in self.watching:
            self.remove_watch(path)

        wd = self._notify.add_watch(path, mask)
        self.watching[path] = wd
        self.watching_wd[wd] = callback
        self.watching_file[wd] = (path, mask)

    def remove_watch(self, path):
        try:
            wd = self.watching[path]
            self._notify.rm_watch(wd)
            del self.watching[path]
            del self.watching_wd[wd]
        except:
            raise FileWatchError("not watching path %s" % path)
コード例 #11
0
ファイル: test_gel.py プロジェクト: caetanus/pygel
 def setUp(self):
     self.reactor = Gel()
コード例 #12
0
ファイル: test_gel.py プロジェクト: caetanus/pygel
class GelTestCase(unittest.TestCase):

    def setUp(self):
        self.reactor = Gel()

    def test_timeout_call(self):
        A = 1
        B = 2
        C = 3
        @gel_main(self.reactor)
        def timer():
            run_time = time.time()

            @gel_quit(self.reactor)
            def callback(a, b, c):
                cb_time = time.time()
                self.assertEqual(A, a)
                self.assertEqual(B, b)
                self.assertEqual(C, c)
                # check if the timer was dispared between 1 and 2 seconds
                # as we can't really tell if the time will be triggered exactly on time
                self.assertGreaterEqual(cb_time + .1, run_time)
                self.assertLessEqual(run_time, cb_time + 2)
            self.reactor.timeout_seconds_call(.1, callback, A, B, c=C)

        timer()

    def test_idle_call(self):

        A = 1
        B = 2
        C = 3
        @gel_main(self.reactor)
        def call_later():

            @gel_quit(self.reactor)
            def callback(a, b, c):
                self.assertEqual(A, a)
                self.assertEqual(B, b)
                self.assertEqual(C, c)
            self.reactor.idle_call(callback, A, B, c=C)

        call_later()

    def test_socket_accept(self):

        @gel_main(self.reactor)
        def actual_test():
            import socket
            port_generator = port_generator_helper()
            while True:
                s = socket.socket()
                try:
                    s.bind(("127.0.0.1", six.next(port_generator)))
                except socket.error:
                    continue
                s.listen(1)
                break

            @gel_quit(self.reactor)
            def callback(event):
                socket, addr = event.accept()
                self.assertIs(event, s)

            self.reactor.register_io(s, callback)
            client = socket.socket()
            client.connect(s.getsockname())

        actual_test()

    def test_main_iteration_with_block_false_should_return_True_with_no_events(self):
        self.assertTrue(self.reactor.main_iteration(block=False))

    def test_sleep(self):
        start = time.time()
        self.reactor.sleep(200)
        current = time.time()
        # assure that the sleep time is almost the sleep we asked for
        wait_time = current - start
        self.assertLessEqual(wait_time, .220)
        self.assertGreaterEqual(wait_time, .190)

    def test_wait_task(self):

        def threaded_task(a, b):
            return a * b

        ret = self.reactor.wait_task(threaded_task, 3, 4)
        self.assertEqual(ret, 3 * 4)

    def test_wait_task_throwns_an_exception_inside_thread_should_raises_that_exception_in_the_current_execution_flow(self):
        def threaded_task():
            return 0/0
        self.assertRaises(ZeroDivisionError, self.reactor.wait_task, threaded_task)

    def test_threaded_wrapper(self):

        @self.reactor.threaded_wrapper
        def threaded_task():
            time.sleep(0.1)
            return 10

        self.assertEqual(10, threaded_task())

    def test_selector(self):

        @gel_main(self.reactor)
        def actual_test():

            s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

            s.sendto(b"", ("", 1))
            _, port = s.getsockname()

            def timeout():
                s.sendto(b"\x00", s.getsockname())

            self.reactor.timeout_seconds_call(0.1, timeout)
            response = self.reactor.selector([s])

            self.assertEqual(s, response)
            self.assertEqual(s.recvfrom(1), (b'\x00', ('127.0.0.1', s.getsockname()[1])))
            self.reactor.main_quit()

        actual_test()

    def test_selector_with_timeout_timed_out(self):
        @gel_main(self.reactor)
        def actual_test():

            s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            s.sendto(b"", ("", 1))
            _, port = s.getsockname()

            def timeout():
                s.sendto(b"\x00", s.getsockname())

            self.reactor.timeout_seconds_call(0.3, timeout)
            response = self.reactor.selector([s], 0.1)

            self.assertIsNone(response)
            self.reactor.main_quit()

        actual_test()

    def test_selector_with_timeout(self):
        @gel_main(self.reactor)
        def actual_test():

            s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

            s.sendto(b"", ("", 1))
            _, port = s.getsockname()

            def timeout():
                s.sendto(b"\x00", s.getsockname())

            self.reactor.timeout_seconds_call(0.01, timeout)
            response = self.reactor.selector([s], 0.2)

            self.assertEqual(s, response)
            self.assertEqual(s.recvfrom(1), (b'\x00', ('127.0.0.1', s.getsockname()[1])))
            self.reactor.main_quit()

        actual_test()
コード例 #13
0
ファイル: test_gel.py プロジェクト: caetanus/pygel
 def test_factory_with_no_arguments_should_use_gel_reactor(self):
     from gel.reactor.gel_reactor import GelReactor
     gel = Gel.from_reactor()
     self.assertIsInstance(gel._reactor, GelReactor)