Move mixins and decorators to tfw module from tfw.components

This commit is contained in:
Kristóf Tóth
2018-04-14 21:15:30 +02:00
parent e2bb126e6f
commit 1c29b700c2
10 changed files with 6 additions and 6 deletions

View File

@ -0,0 +1,6 @@
# Copyright (C) 2018 Avatao.com Innovative Learning Kft.
# All Rights Reserved. See LICENSE file for details.
from .supervisor_mixin import SupervisorMixin
from .callback_mixin import CallbackMixin
from .observer_mixin import ObserverMixin

View File

@ -0,0 +1,20 @@
# Copyright (C) 2018 Avatao.com Innovative Learning Kft.
# All Rights Reserved. See LICENSE file for details.
from functools import partial
class CallbackMixin:
def __init__(self):
self._callbacks = []
def subscribe_callback(self, callback, *args, **kwargs):
fun = partial(callback, *args, **kwargs)
self._callbacks.append(fun)
def unsubscribe_callback(self, callback):
self._callbacks.remove(callback)
def _execute_callbacks(self, *args, **kwargs):
for callback in self._callbacks:
callback(*args, **kwargs)

View File

@ -0,0 +1,16 @@
# Copyright (C) 2018 Avatao.com Innovative Learning Kft.
# All Rights Reserved. See LICENSE file for details.
from watchdog.observers import Observer
class ObserverMixin:
def __init__(self):
self.observer = Observer()
def watch(self):
self.observer.start()
def stop(self):
self.observer.stop()
self.observer.join()

View File

@ -0,0 +1,30 @@
# Copyright (C) 2018 Avatao.com Innovative Learning Kft.
# All Rights Reserved. See LICENSE file for details.
import xmlrpc.client
from xmlrpc.client import Fault as SupervisorFault
from contextlib import suppress
from os import remove
from tfw.config import TFWENV
class SupervisorMixin:
supervisor = xmlrpc.client.ServerProxy(TFWENV.SUPERVISOR_HTTP_URI).supervisor
def stop_process(self, process_name):
with suppress(SupervisorFault):
self.supervisor.stopProcess(process_name)
def start_process(self, process_name):
self.supervisor.startProcess(process_name)
def read_log(self, process_name):
logs = self.supervisor.readProcessStderrLog(process_name, 0, 0)
remove(self.supervisor.getProcessInfo(process_name)['stderr_logfile'])
self.supervisor.clearProcessLogs(process_name)
return logs
def restart_process(self, process_name):
self.stop_process(process_name)
self.start_process(process_name)