From f5a8230d3b8d0fd15d4b845591b982fb4ed926fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A1lint=20Bokros?= Date: Tue, 13 Feb 2018 17:44:48 +0100 Subject: [PATCH 1/4] Move ZMQConnectorBase to networking --- lib/tfw/networking/controller_connector.py | 2 +- lib/tfw/networking/event_handlers/server_connector.py | 2 +- lib/tfw/networking/server/event_handler_connector.py | 2 +- lib/tfw/networking/solvable_connector.py | 2 +- lib/tfw/networking/zmq_connector_base.py | 6 ++++++ lib/tfw/util.py | 7 +------ 6 files changed, 11 insertions(+), 10 deletions(-) create mode 100644 lib/tfw/networking/zmq_connector_base.py diff --git a/lib/tfw/networking/controller_connector.py b/lib/tfw/networking/controller_connector.py index 64f83b2..dcafa70 100644 --- a/lib/tfw/networking/controller_connector.py +++ b/lib/tfw/networking/controller_connector.py @@ -3,7 +3,7 @@ from zmq.eventloop import ioloop from zmq.eventloop.zmqstream import ZMQStream from tfw.config import tfwenv -from tfw.util import ZMQConnectorBase +from tfw.networking.zmq_connector_base import ZMQConnectorBase ioloop.install() diff --git a/lib/tfw/networking/event_handlers/server_connector.py b/lib/tfw/networking/event_handlers/server_connector.py index 700468a..d15d467 100644 --- a/lib/tfw/networking/event_handlers/server_connector.py +++ b/lib/tfw/networking/event_handlers/server_connector.py @@ -4,7 +4,7 @@ from zmq.eventloop.zmqstream import ZMQStream from tfw.networking.serialization import serialize_all from tfw.config import tfwenv -from tfw.util import ZMQConnectorBase +from tfw.networking.zmq_connector_base import ZMQConnectorBase class ServerDownlinkConnector(ZMQConnectorBase): diff --git a/lib/tfw/networking/server/event_handler_connector.py b/lib/tfw/networking/server/event_handler_connector.py index 310ad28..a5149f3 100644 --- a/lib/tfw/networking/server/event_handler_connector.py +++ b/lib/tfw/networking/server/event_handler_connector.py @@ -5,7 +5,7 @@ from tfw.networking.serialization import serialize_all from tfw.config import tfwenv from tfw.config.logs import logging log = logging.getLogger(__name__) -from tfw.util import ZMQConnectorBase +from tfw.networking.zmq_connector_base import ZMQConnectorBase class EventHandlerDownlinkConnector(ZMQConnectorBase): diff --git a/lib/tfw/networking/solvable_connector.py b/lib/tfw/networking/solvable_connector.py index 7a77a57..a207a21 100644 --- a/lib/tfw/networking/solvable_connector.py +++ b/lib/tfw/networking/solvable_connector.py @@ -2,7 +2,7 @@ import zmq from zmq.eventloop import ioloop from tfw.config import tfwenv -from tfw.util import ZMQConnectorBase +from tfw.networking.zmq_connector_base import ZMQConnectorBase from tfw.networking.serialization import serialize_all, deserialize_all ioloop.install() diff --git a/lib/tfw/networking/zmq_connector_base.py b/lib/tfw/networking/zmq_connector_base.py new file mode 100644 index 0000000..c4c838a --- /dev/null +++ b/lib/tfw/networking/zmq_connector_base.py @@ -0,0 +1,6 @@ +import zmq + + +class ZMQConnectorBase: + def __init__(self, zmq_context=None): + self._zmq_context = zmq_context or zmq.Context.instance() diff --git a/lib/tfw/util.py b/lib/tfw/util.py index 7ee99cc..4c53d4e 100644 --- a/lib/tfw/util.py +++ b/lib/tfw/util.py @@ -1,4 +1,4 @@ -import xmlrpc.client, zmq +import xmlrpc.client from contextlib import suppress from xmlrpc.client import Fault as SupervisorFault from time import time, sleep @@ -30,11 +30,6 @@ class SupervisorMixin: self.start_process() -class ZMQConnectorBase: - def __init__(self, zmq_context=None): - self._zmq_context = zmq_context or zmq.Context.instance() - - class RateLimiter: def __init__(self, rate_per_second): self.min_interval = 1 / float(rate_per_second) From 14f3a4a153186c96a2dc1d2cf1cd969910162b26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A1lint=20Bokros?= Date: Tue, 13 Feb 2018 17:51:19 +0100 Subject: [PATCH 2/4] Move SupervisorMixin to mixins --- lib/tfw/components/mixins/__init__.py | 1 + lib/tfw/components/mixins/supervisor_mixin.py | 20 +++++++++++++++++++ .../components/source_code_event_handler.py | 2 +- lib/tfw/components/terminado_event_handler.py | 2 +- lib/tfw/util.py | 20 ------------------- 5 files changed, 23 insertions(+), 22 deletions(-) create mode 100644 lib/tfw/components/mixins/__init__.py create mode 100644 lib/tfw/components/mixins/supervisor_mixin.py diff --git a/lib/tfw/components/mixins/__init__.py b/lib/tfw/components/mixins/__init__.py new file mode 100644 index 0000000..fd2a569 --- /dev/null +++ b/lib/tfw/components/mixins/__init__.py @@ -0,0 +1 @@ +from .supervisor_mixin import SupervisorMixin diff --git a/lib/tfw/components/mixins/supervisor_mixin.py b/lib/tfw/components/mixins/supervisor_mixin.py new file mode 100644 index 0000000..968063e --- /dev/null +++ b/lib/tfw/components/mixins/supervisor_mixin.py @@ -0,0 +1,20 @@ +import xmlrpc.client +from contextlib import suppress +from xmlrpc.client import Fault as SupervisorFault + +from tfw.config import tfwenv + + +class SupervisorMixin: + supervisor = xmlrpc.client.ServerProxy(tfwenv.SUPERVISOR_HTTP_URI).supervisor + + def stop_process(self): + with suppress(SupervisorFault): + self.supervisor.stopProcess(self.process_name) + + def start_process(self): + self.supervisor.startProcess(self.process_name) + + def restart_process(self): + self.stop_process() + self.start_process() diff --git a/lib/tfw/components/source_code_event_handler.py b/lib/tfw/components/source_code_event_handler.py index 5b5de40..a0964a8 100644 --- a/lib/tfw/components/source_code_event_handler.py +++ b/lib/tfw/components/source_code_event_handler.py @@ -1,7 +1,7 @@ from os.path import splitext, isfile, join, relpath from glob import glob -from tfw.util import SupervisorMixin +from tfw.components.mixins import SupervisorMixin from tfw.event_handler_base import EventHandlerBase from tfw.components.directory_monitor import DirectoryMonitor diff --git a/lib/tfw/components/terminado_event_handler.py b/lib/tfw/components/terminado_event_handler.py index 4222e05..bdfebb2 100644 --- a/lib/tfw/components/terminado_event_handler.py +++ b/lib/tfw/components/terminado_event_handler.py @@ -1,5 +1,5 @@ from tfw.event_handler_base import EventHandlerBase -from tfw.util import SupervisorMixin +from tfw.components.mixins import SupervisorMixin from tfw.config import tfwenv from tfw.config.logs import logging log = logging.getLogger(__name__) diff --git a/lib/tfw/util.py b/lib/tfw/util.py index 4c53d4e..bede6e8 100644 --- a/lib/tfw/util.py +++ b/lib/tfw/util.py @@ -1,11 +1,6 @@ -import xmlrpc.client -from contextlib import suppress -from xmlrpc.client import Fault as SupervisorFault from time import time, sleep from functools import wraps -from tfw.config import tfwenv - def create_source_code_response_data(filename, content, language): return { @@ -15,21 +10,6 @@ def create_source_code_response_data(filename, content, language): } -class SupervisorMixin: - supervisor = xmlrpc.client.ServerProxy(tfwenv.SUPERVISOR_HTTP_URI).supervisor - - def stop_process(self): - with suppress(SupervisorFault): - self.supervisor.stopProcess(self.process_name) - - def start_process(self): - self.supervisor.startProcess(self.process_name) - - def restart_process(self): - self.stop_process() - self.start_process() - - class RateLimiter: def __init__(self, rate_per_second): self.min_interval = 1 / float(rate_per_second) From b9e41c01cf8be9bccaf08ac9cac3da0d41c5c322 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A1lint=20Bokros?= Date: Tue, 13 Feb 2018 17:56:01 +0100 Subject: [PATCH 3/4] Move RateLimiter to decorators --- lib/tfw/components/decorators/__init__.py | 1 + lib/tfw/components/decorators/rate_limiter.py | 22 +++++++++++++++++++ lib/tfw/components/directory_monitor.py | 3 +-- lib/tfw/util.py | 22 ------------------- 4 files changed, 24 insertions(+), 24 deletions(-) create mode 100644 lib/tfw/components/decorators/__init__.py create mode 100644 lib/tfw/components/decorators/rate_limiter.py diff --git a/lib/tfw/components/decorators/__init__.py b/lib/tfw/components/decorators/__init__.py new file mode 100644 index 0000000..c3d1ffd --- /dev/null +++ b/lib/tfw/components/decorators/__init__.py @@ -0,0 +1 @@ +from .rate_limiter import RateLimiter diff --git a/lib/tfw/components/decorators/rate_limiter.py b/lib/tfw/components/decorators/rate_limiter.py new file mode 100644 index 0000000..c71c86c --- /dev/null +++ b/lib/tfw/components/decorators/rate_limiter.py @@ -0,0 +1,22 @@ +from functools import wraps +from time import time, sleep + + +class RateLimiter: + def __init__(self, rate_per_second): + self.min_interval = 1 / float(rate_per_second) + self.last_call = time() + + def __call__(self, fun): + @wraps(fun) + def wrapper(*args, **kwargs): + self._limit_rate() + fun(*args, **kwargs) + return wrapper + + def _limit_rate(self): + since_last_call = time() - self.last_call + to_next_call = self.min_interval - since_last_call + self.last_call = time() + if to_next_call > 0: + sleep(to_next_call) diff --git a/lib/tfw/components/directory_monitor.py b/lib/tfw/components/directory_monitor.py index 5dae10e..6dbf78f 100644 --- a/lib/tfw/components/directory_monitor.py +++ b/lib/tfw/components/directory_monitor.py @@ -2,13 +2,12 @@ from watchdog.observers import Observer from watchdog.events import FileSystemEventHandler from tfw.networking.event_handlers.server_connector import ServerUplinkConnector -from tfw.util import RateLimiter +from tfw.components.decorators import RateLimiter from tfw.config.logs import logging log = logging.getLogger(__name__) - class WebideReloadEventHandler(FileSystemEventHandler): def __init__(self): super().__init__() diff --git a/lib/tfw/util.py b/lib/tfw/util.py index bede6e8..388ed56 100644 --- a/lib/tfw/util.py +++ b/lib/tfw/util.py @@ -1,7 +1,3 @@ -from time import time, sleep -from functools import wraps - - def create_source_code_response_data(filename, content, language): return { 'filename': filename, @@ -10,21 +6,3 @@ def create_source_code_response_data(filename, content, language): } -class RateLimiter: - def __init__(self, rate_per_second): - self.min_interval = 1 / float(rate_per_second) - self.last_call = time() - - def __call__(self, fun): - @wraps(fun) - def wrapper(*args, **kwargs): - self._limit_rate() - fun(*args, **kwargs) - return wrapper - - def _limit_rate(self): - since_last_call = time() - self.last_call - to_next_call = self.min_interval - since_last_call - self.last_call = time() - if to_next_call > 0: - sleep(to_next_call) \ No newline at end of file From 2ebc34fa05ead31c75ef8fa95e5799016a513fa8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A1lint=20Bokros?= Date: Tue, 13 Feb 2018 17:57:06 +0100 Subject: [PATCH 4/4] Remove unused create_source_code_response_data method --- lib/tfw/util.py | 8 -------- 1 file changed, 8 deletions(-) delete mode 100644 lib/tfw/util.py diff --git a/lib/tfw/util.py b/lib/tfw/util.py deleted file mode 100644 index 388ed56..0000000 --- a/lib/tfw/util.py +++ /dev/null @@ -1,8 +0,0 @@ -def create_source_code_response_data(filename, content, language): - return { - 'filename': filename, - 'content': content, - 'language': language - } - -