Merge pull request #10 from avatao-content/util_refactor

Util refactor
This commit is contained in:
therealkrispet 2018-02-13 18:54:08 +01:00 committed by GitHub
commit e5576775ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 57 additions and 63 deletions

View File

@ -0,0 +1 @@
from .rate_limiter import RateLimiter

View File

@ -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)

View File

@ -2,13 +2,12 @@ from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler from watchdog.events import FileSystemEventHandler
from tfw.networking.event_handlers.server_connector import ServerUplinkConnector 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 from tfw.config.logs import logging
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
class WebideReloadEventHandler(FileSystemEventHandler): class WebideReloadEventHandler(FileSystemEventHandler):
def __init__(self): def __init__(self):
super().__init__() super().__init__()

View File

@ -0,0 +1 @@
from .supervisor_mixin import SupervisorMixin

View File

@ -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()

View File

@ -1,7 +1,7 @@
from os.path import splitext, isfile, join, relpath from os.path import splitext, isfile, join, relpath
from glob import glob from glob import glob
from tfw.util import SupervisorMixin from tfw.components.mixins import SupervisorMixin
from tfw.event_handler_base import EventHandlerBase from tfw.event_handler_base import EventHandlerBase
from tfw.components.directory_monitor import DirectoryMonitor from tfw.components.directory_monitor import DirectoryMonitor

View File

@ -1,5 +1,5 @@
from tfw.event_handler_base import EventHandlerBase 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 import tfwenv
from tfw.config.logs import logging from tfw.config.logs import logging
log = logging.getLogger(__name__) log = logging.getLogger(__name__)

View File

@ -3,7 +3,7 @@ from zmq.eventloop import ioloop
from zmq.eventloop.zmqstream import ZMQStream from zmq.eventloop.zmqstream import ZMQStream
from tfw.config import tfwenv from tfw.config import tfwenv
from tfw.util import ZMQConnectorBase from tfw.networking.zmq_connector_base import ZMQConnectorBase
ioloop.install() ioloop.install()

View File

@ -4,7 +4,7 @@ from zmq.eventloop.zmqstream import ZMQStream
from tfw.networking.serialization import serialize_all from tfw.networking.serialization import serialize_all
from tfw.config import tfwenv from tfw.config import tfwenv
from tfw.util import ZMQConnectorBase from tfw.networking.zmq_connector_base import ZMQConnectorBase
class ServerDownlinkConnector(ZMQConnectorBase): class ServerDownlinkConnector(ZMQConnectorBase):

View File

@ -5,7 +5,7 @@ from tfw.networking.serialization import serialize_all
from tfw.config import tfwenv from tfw.config import tfwenv
from tfw.config.logs import logging from tfw.config.logs import logging
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
from tfw.util import ZMQConnectorBase from tfw.networking.zmq_connector_base import ZMQConnectorBase
class EventHandlerDownlinkConnector(ZMQConnectorBase): class EventHandlerDownlinkConnector(ZMQConnectorBase):

View File

@ -2,7 +2,7 @@ import zmq
from zmq.eventloop import ioloop from zmq.eventloop import ioloop
from tfw.config import tfwenv 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 from tfw.networking.serialization import serialize_all, deserialize_all
ioloop.install() ioloop.install()

View File

@ -0,0 +1,6 @@
import zmq
class ZMQConnectorBase:
def __init__(self, zmq_context=None):
self._zmq_context = zmq_context or zmq.Context.instance()

View File

@ -1,55 +0,0 @@
import xmlrpc.client, zmq
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 {
'filename': filename,
'content': content,
'language': 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 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)
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)