mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-05 16:31:21 +00:00
Merge branch 'pylint'
This commit is contained in:
commit
d5f11f9a08
@ -2,7 +2,7 @@ from collections import namedtuple
|
|||||||
from os import environ
|
from os import environ
|
||||||
|
|
||||||
|
|
||||||
def generate_namedtuple_from_prefixed_envvars(prefix: str, tuple_name: str):
|
def prefixed_envvars_to_namedtuple(prefix: str, tuple_name: str):
|
||||||
envvars = {envvar.replace(prefix, '', 1): environ.get(envvar)
|
envvars = {envvar.replace(prefix, '', 1): environ.get(envvar)
|
||||||
for envvar in environ.keys()
|
for envvar in environ.keys()
|
||||||
if envvar.startswith(prefix)}
|
if envvar.startswith(prefix)}
|
||||||
|
@ -1 +1 @@
|
|||||||
from .envvars import *
|
from .envvars import * # pylint: disable=wildcard-import
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
from envvars import generate_namedtuple_from_prefixed_envvars
|
from envvars import prefixed_envvars_to_namedtuple
|
||||||
|
|
||||||
TAOENV = generate_namedtuple_from_prefixed_envvars('AVATAO_', 'taoenvtuple')
|
TAOENV = prefixed_envvars_to_namedtuple('AVATAO_', 'taoenvtuple')
|
||||||
|
@ -61,7 +61,8 @@ class WebideReloadWatchdogEventHandler(FileSystemWatchdogEventHandler):
|
|||||||
|
|
||||||
@RateLimiter(rate_per_second=2)
|
@RateLimiter(rate_per_second=2)
|
||||||
def on_modified(self, event):
|
def on_modified(self, event):
|
||||||
if self._paused: return
|
if self._paused:
|
||||||
|
return
|
||||||
if self.ignore > 0:
|
if self.ignore > 0:
|
||||||
self.ignore = self.ignore - 1
|
self.ignore = self.ignore - 1
|
||||||
return
|
return
|
||||||
|
@ -24,7 +24,8 @@ class DirectoryMonitoringEventHandler(TriggerlessEventHandler):
|
|||||||
|
|
||||||
@directory.setter
|
@directory.setter
|
||||||
def directory(self, directory):
|
def directory(self, directory):
|
||||||
if not exists(directory) or not isdir(directory): raise EnvironmentError('No such directory!')
|
if not exists(directory) or not isdir(directory):
|
||||||
|
raise EnvironmentError('No such directory!')
|
||||||
self._directory = directory
|
self._directory = directory
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -33,8 +34,10 @@ class DirectoryMonitoringEventHandler(TriggerlessEventHandler):
|
|||||||
|
|
||||||
def reload_monitor(self):
|
def reload_monitor(self):
|
||||||
if self._monitor:
|
if self._monitor:
|
||||||
try: self._monitor.stop()
|
try:
|
||||||
except KeyError: logging.debug('Working directory was removed – ignoring...')
|
self._monitor.stop()
|
||||||
|
except KeyError:
|
||||||
|
logging.debug('Working directory was removed – ignoring...')
|
||||||
self._monitor = DirectoryMonitor(self._directory)
|
self._monitor = DirectoryMonitor(self._directory)
|
||||||
self._monitor.watch() # This runs on a separate thread
|
self._monitor.watch() # This runs on a separate thread
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
from os.path import dirname
|
from os.path import dirname
|
||||||
from re import compile, findall
|
from re import findall
|
||||||
|
from re import compile as compileregex
|
||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
|
|
||||||
from watchdog.observers import Observer
|
from watchdog.observers import Observer
|
||||||
@ -39,7 +40,7 @@ class HistoryMonitor(CallbackMixin, ABC):
|
|||||||
def _fetch_history(self):
|
def _fetch_history(self):
|
||||||
self._last_length = len(self._history)
|
self._last_length = len(self._history)
|
||||||
with open(self.histfile, 'r') as ifile:
|
with open(self.histfile, 'r') as ifile:
|
||||||
pattern = compile(self.command_pattern)
|
pattern = compileregex(self.command_pattern)
|
||||||
data = ifile.read()
|
data = ifile.read()
|
||||||
self._history = [self.sanitize_command(command) for command in findall(pattern, data)]
|
self._history = [self.sanitize_command(command) for command in findall(pattern, data)]
|
||||||
|
|
||||||
@ -49,6 +50,7 @@ class HistoryMonitor(CallbackMixin, ABC):
|
|||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
def sanitize_command(self, command):
|
def sanitize_command(self, command):
|
||||||
|
# pylint: disable=no-self-use
|
||||||
return command
|
return command
|
||||||
|
|
||||||
def _invoke_callbacks(self):
|
def _invoke_callbacks(self):
|
||||||
|
@ -22,8 +22,10 @@ class FileManager:
|
|||||||
|
|
||||||
@exclude.setter
|
@exclude.setter
|
||||||
def exclude(self, exclude):
|
def exclude(self, exclude):
|
||||||
if exclude is None: return
|
if exclude is None:
|
||||||
if not isinstance(exclude, Iterable): raise TypeError('Exclude must be Iterable!')
|
return
|
||||||
|
if not isinstance(exclude, Iterable):
|
||||||
|
raise TypeError('Exclude must be Iterable!')
|
||||||
self._exclude = exclude
|
self._exclude = exclude
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -89,28 +91,38 @@ class SourceCodeEventHandler(TriggerlessEventHandler):
|
|||||||
|
|
||||||
def reload_monitor(self):
|
def reload_monitor(self):
|
||||||
if self._monitor:
|
if self._monitor:
|
||||||
try: self._monitor.stop()
|
try:
|
||||||
except KeyError: logging.debug('Working directory was removed – ignoring...')
|
self._monitor.stop()
|
||||||
|
except KeyError:
|
||||||
|
logging.debug('Working directory was removed – ignoring...')
|
||||||
self._monitor = DirectoryMonitor(self.filemanager.workdir)
|
self._monitor = DirectoryMonitor(self.filemanager.workdir)
|
||||||
self._monitor.watch() # This runs on a separate thread
|
self._monitor.watch() # This runs on a separate thread
|
||||||
|
|
||||||
def read(self, data):
|
def read(self, data):
|
||||||
try: data['content'] = self.filemanager.file_contents
|
try:
|
||||||
except PermissionError: data['content'] = 'You have no permission to open that file :('
|
data['content'] = self.filemanager.file_contents
|
||||||
except FileNotFoundError: data['content'] = 'This file was removed :('
|
except PermissionError:
|
||||||
except Exception: data['content'] = 'Failed to read file :('
|
data['content'] = 'You have no permission to open that file :('
|
||||||
|
except FileNotFoundError:
|
||||||
|
data['content'] = 'This file was removed :('
|
||||||
|
except Exception: # pylint: disable=broad-except
|
||||||
|
data['content'] = 'Failed to read file :('
|
||||||
return data
|
return data
|
||||||
|
|
||||||
def write(self, data):
|
def write(self, data):
|
||||||
self.monitor.ignore = self.monitor.ignore + 1
|
self.monitor.ignore = self.monitor.ignore + 1
|
||||||
try: self.filemanager.file_contents = data['content']
|
try:
|
||||||
except Exception: LOG.exception('Error writing file!')
|
self.filemanager.file_contents = data['content']
|
||||||
|
except Exception: # pylint: disable=broad-except
|
||||||
|
LOG.exception('Error writing file!')
|
||||||
del data['content']
|
del data['content']
|
||||||
return data
|
return data
|
||||||
|
|
||||||
def select(self, data):
|
def select(self, data):
|
||||||
try: self.filemanager.filename = data['filename']
|
try:
|
||||||
except EnvironmentError: LOG.exception('Failed to select file "%s"', data['filename'])
|
self.filemanager.filename = data['filename']
|
||||||
|
except EnvironmentError:
|
||||||
|
LOG.exception('Failed to select file "%s"', data['filename'])
|
||||||
return data
|
return data
|
||||||
|
|
||||||
def select_dir(self, data):
|
def select_dir(self, data):
|
||||||
@ -127,8 +139,10 @@ class SourceCodeEventHandler(TriggerlessEventHandler):
|
|||||||
return data
|
return data
|
||||||
|
|
||||||
def exclude(self, data):
|
def exclude(self, data):
|
||||||
try: self.filemanager.exclude = list(data['exclude'])
|
try:
|
||||||
except TypeError: LOG.error('Exclude must be Iterable!')
|
self.filemanager.exclude = list(data['exclude'])
|
||||||
|
except TypeError:
|
||||||
|
LOG.error('Exclude must be Iterable!')
|
||||||
return data
|
return data
|
||||||
|
|
||||||
def attach_fileinfo(self, data):
|
def attach_fileinfo(self, data):
|
||||||
|
@ -29,7 +29,7 @@ class TerminadoMiniServer:
|
|||||||
def pty(self):
|
def pty(self):
|
||||||
return self.term_manager.terminal.ptyproc
|
return self.term_manager.terminal.ptyproc
|
||||||
|
|
||||||
class ResetterTermSocket(TermSocket):
|
class ResetterTermSocket(TermSocket): # pylint: disable=abstract-method
|
||||||
def check_origin(self, origin):
|
def check_origin(self, origin):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
from envvars import generate_namedtuple_from_prefixed_envvars
|
from envvars import prefixed_envvars_to_namedtuple
|
||||||
|
|
||||||
TFWENV = generate_namedtuple_from_prefixed_envvars('TFW_', 'tfwenvtuple')
|
TFWENV = prefixed_envvars_to_namedtuple('TFW_', 'tfwenvtuple')
|
||||||
|
@ -16,7 +16,8 @@ class EventHandlerBase(ABC):
|
|||||||
def event_handler_callback(self, msg_parts):
|
def event_handler_callback(self, msg_parts):
|
||||||
key, message = deserialize_all(*msg_parts)
|
key, message = deserialize_all(*msg_parts)
|
||||||
response = self.dispatch_handling(key, message)
|
response = self.dispatch_handling(key, message)
|
||||||
if response is None: return
|
if response is None:
|
||||||
|
return
|
||||||
self.server_connector.send(key, response)
|
self.server_connector.send(key, response)
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
@ -24,14 +25,18 @@ class EventHandlerBase(ABC):
|
|||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
def _dispatch_handling(self, key, message):
|
def _dispatch_handling(self, key, message):
|
||||||
if key != 'reset': return self.handle_event(key, message)
|
# pylint: disable=no-else-return
|
||||||
else: return self.handle_reset(message)
|
if key != 'reset':
|
||||||
|
return self.handle_event(key, message)
|
||||||
|
else:
|
||||||
|
return self.handle_reset(message)
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def handle_event(self, key, message):
|
def handle_event(self, key, message):
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
def handle_reset(self, message):
|
def handle_reset(self, message):
|
||||||
|
# pylint: disable=unused-argument,no-self-use
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def cleanup(self):
|
def cleanup(self):
|
||||||
@ -63,11 +68,13 @@ class EventHandlerBase(ABC):
|
|||||||
|
|
||||||
|
|
||||||
class TriggerlessEventHandler(EventHandlerBase, ABC):
|
class TriggerlessEventHandler(EventHandlerBase, ABC):
|
||||||
|
# pylint: disable=abstract-method
|
||||||
def dispatch_handling(self, key, message):
|
def dispatch_handling(self, key, message):
|
||||||
return self._dispatch_handling(key, message)
|
return self._dispatch_handling(key, message)
|
||||||
|
|
||||||
|
|
||||||
class TriggeredEventHandler(EventHandlerBase, ABC):
|
class TriggeredEventHandler(EventHandlerBase, ABC):
|
||||||
|
# pylint: disable=abstract-method
|
||||||
def __init__(self, key, trigger):
|
def __init__(self, key, trigger):
|
||||||
super().__init__(key)
|
super().__init__(key)
|
||||||
self.trigger = trigger
|
self.trigger = trigger
|
||||||
@ -75,3 +82,4 @@ class TriggeredEventHandler(EventHandlerBase, ABC):
|
|||||||
def dispatch_handling(self, key, message):
|
def dispatch_handling(self, key, message):
|
||||||
if message.get('trigger') == self.trigger:
|
if message.get('trigger') == self.trigger:
|
||||||
return self._dispatch_handling(key, message)
|
return self._dispatch_handling(key, message)
|
||||||
|
return None
|
||||||
|
@ -23,4 +23,4 @@ class FSMBase(CallbackMixin):
|
|||||||
self._execute_callbacks(event_data.kwargs)
|
self._execute_callbacks(event_data.kwargs)
|
||||||
|
|
||||||
def is_solved(self):
|
def is_solved(self):
|
||||||
return self.state in self.accepted_states
|
return self.state in self.accepted_states # pylint: disable=no-member
|
||||||
|
@ -20,6 +20,7 @@ class ControllerResponder:
|
|||||||
stream.send_multipart(serialize_all(self.token, response))
|
stream.send_multipart(serialize_all(self.token, response))
|
||||||
|
|
||||||
def handle_test_request(self, data):
|
def handle_test_request(self, data):
|
||||||
|
# pylint: disable=unused-argument,no-self-use
|
||||||
return 'OK'
|
return 'OK'
|
||||||
|
|
||||||
def handle_token_request(self, data):
|
def handle_token_request(self, data):
|
||||||
@ -28,6 +29,7 @@ class ControllerResponder:
|
|||||||
return {'token': self.token}
|
return {'token': self.token}
|
||||||
|
|
||||||
def handle_solution_check_request(self, data):
|
def handle_solution_check_request(self, data):
|
||||||
|
# pylint: disable=unused-argument
|
||||||
return {
|
return {
|
||||||
'solved': self.fsm.is_solved(),
|
'solved': self.fsm.is_solved(),
|
||||||
'message': 'solved' if self.fsm.is_solved() else 'not solved'
|
'message': 'solved' if self.fsm.is_solved() else 'not solved'
|
||||||
|
@ -33,5 +33,6 @@ class EventHandlerConnector(EventHandlerDownlinkConnector, EventHandlerUplinkCon
|
|||||||
self._zmq_pull_stream.on_recv(callback)
|
self._zmq_pull_stream.on_recv(callback)
|
||||||
|
|
||||||
def send_message(self, message: dict, key: str = None):
|
def send_message(self, message: dict, key: str = None):
|
||||||
if not key: key = message.get('key', '')
|
if not key:
|
||||||
|
key = message.get('key', '')
|
||||||
self._zmq_pub_socket.send_multipart(serialize_all(key, message))
|
self._zmq_pub_socket.send_multipart(serialize_all(key, message))
|
||||||
|
@ -33,11 +33,14 @@ class TFWServer:
|
|||||||
|
|
||||||
def make_response(self, message):
|
def make_response(self, message):
|
||||||
trigger = message.get('trigger', '')
|
trigger = message.get('trigger', '')
|
||||||
try: self._fsm_manager.trigger(trigger, message)
|
try:
|
||||||
except AttributeError: LOG.debug('FSM failed to execute nonexistent trigger: "%s"', trigger)
|
self._fsm_manager.trigger(trigger, message)
|
||||||
|
except AttributeError:
|
||||||
|
LOG.debug('FSM failed to execute nonexistent trigger: "%s"', trigger)
|
||||||
return message
|
return message
|
||||||
|
|
||||||
def proxy_filter(self, message):
|
def proxy_filter(self, message):
|
||||||
|
# pylint: disable=unused-argument,no-self-use
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def listen(self, port):
|
def listen(self, port):
|
||||||
@ -62,8 +65,10 @@ class FSMManager:
|
|||||||
self.messenge_sender.send('FSM', message)
|
self.messenge_sender.send('FSM', message)
|
||||||
|
|
||||||
if all(predicate_results):
|
if all(predicate_results):
|
||||||
try: self.fsm.trigger(trigger, message=message)
|
try:
|
||||||
except AttributeError: LOG.debug('FSM failed to execute nonexistent trigger: "%s"', trigger)
|
self.fsm.trigger(trigger, message=message)
|
||||||
|
except AttributeError:
|
||||||
|
LOG.debug('FSM failed to execute nonexistent trigger: "%s"', trigger)
|
||||||
|
|
||||||
def subscribe_predicate(self, trigger, *predicates):
|
def subscribe_predicate(self, trigger, *predicates):
|
||||||
self.trigger_predicates[trigger].extend(predicates)
|
self.trigger_predicates[trigger].extend(predicates)
|
||||||
@ -79,6 +84,7 @@ class FSMUpdater:
|
|||||||
self.uplink = ServerUplinkConnector()
|
self.uplink = ServerUplinkConnector()
|
||||||
|
|
||||||
def update(self, kwargs_dict):
|
def update(self, kwargs_dict):
|
||||||
|
# pylint: disable=unused-argument
|
||||||
self.uplink.send(*self.generate_fsm_update())
|
self.uplink.send(*self.generate_fsm_update())
|
||||||
|
|
||||||
def generate_fsm_update(self):
|
def generate_fsm_update(self):
|
||||||
|
@ -34,8 +34,10 @@ class ZMQWebSocketHandler(WebSocketHandler, ABC):
|
|||||||
for instance in cls.instances:
|
for instance in cls.instances:
|
||||||
instance.write_message(data)
|
instance.write_message(data)
|
||||||
else:
|
else:
|
||||||
try: keyhandlers[key](data['data'])
|
try:
|
||||||
except KeyError: LOG.error('Invalid mirror message format! Ignoring.')
|
keyhandlers[key](data['data'])
|
||||||
|
except KeyError:
|
||||||
|
LOG.error('Invalid mirror message format! Ignoring.')
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def mirror(cls, data):
|
def mirror(cls, data):
|
||||||
@ -59,7 +61,9 @@ class ZMQWebSocketHandler(WebSocketHandler, ABC):
|
|||||||
|
|
||||||
|
|
||||||
class ZMQWebSocketProxy(ZMQWebSocketHandler):
|
class ZMQWebSocketProxy(ZMQWebSocketHandler):
|
||||||
|
# pylint: disable=abstract-method
|
||||||
def initialize(self, make_response, proxy_filter):
|
def initialize(self, make_response, proxy_filter):
|
||||||
|
# pylint: disable=arguments-differ
|
||||||
self._make_response = make_response
|
self._make_response = make_response
|
||||||
self._proxy_filter = proxy_filter
|
self._proxy_filter = proxy_filter
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user