mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2025-06-28 18:05:12 +00:00
Move mixins and decorators to tfw module from tfw.components
This commit is contained in:
6
lib/tfw/mixins/__init__.py
Normal file
6
lib/tfw/mixins/__init__.py
Normal 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
|
20
lib/tfw/mixins/callback_mixin.py
Normal file
20
lib/tfw/mixins/callback_mixin.py
Normal 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)
|
16
lib/tfw/mixins/observer_mixin.py
Normal file
16
lib/tfw/mixins/observer_mixin.py
Normal 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()
|
30
lib/tfw/mixins/supervisor_mixin.py
Normal file
30
lib/tfw/mixins/supervisor_mixin.py
Normal 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)
|
Reference in New Issue
Block a user