diff --git a/lib/envvars/__init__.py b/lib/envvars/__init__.py index b5d9503..d1ed0d4 100644 --- a/lib/envvars/__init__.py +++ b/lib/envvars/__init__.py @@ -17,7 +17,9 @@ class LazyEnvironment: return self.prefixed_envvars_to_namedtuple() def prefixed_envvars_to_namedtuple(self): - envvars = {envvar.replace(self._prefix, '', 1): environ.get(envvar) - for envvar in environ.keys() - if envvar.startswith(self._prefix)} + envvars = { + envvar.replace(self._prefix, '', 1): environ.get(envvar) + for envvar in environ.keys() + if envvar.startswith(self._prefix) + } return namedtuple(self._tuple_name, envvars)(**envvars) diff --git a/lib/tfw/components/directory_monitor.py b/lib/tfw/components/directory_monitor.py index 3ad6732..dc72e9e 100644 --- a/lib/tfw/components/directory_monitor.py +++ b/lib/tfw/components/directory_monitor.py @@ -64,8 +64,10 @@ class IdeReloadWatchdogEventHandler(FileSystemWatchdogEventHandler): self.ignore = self.ignore - 1 return LOG.debug(event) - self.uplink.send({'key': 'ide', - 'data': {'command': 'reload'}}) + self.uplink.send({ + 'key': 'ide', + 'data': {'command': 'reload'} + }) def with_monitor_paused(fun): diff --git a/lib/tfw/components/directory_monitoring_event_handler.py b/lib/tfw/components/directory_monitoring_event_handler.py index 03f58e1..b3beeb2 100644 --- a/lib/tfw/components/directory_monitoring_event_handler.py +++ b/lib/tfw/components/directory_monitoring_event_handler.py @@ -17,10 +17,12 @@ class DirectoryMonitoringEventHandler(EventHandlerBase, MonitorManagerMixin): self._directory = directory MonitorManagerMixin.__init__(self, DirectoryMonitor, self._directory) - self.commands = {'pause': self.pause, - 'resume': self.resume, - 'ignore': self.ignore, - 'selectdir': self.selectdir} + self.commands = { + 'pause': self.pause, + 'resume': self.resume, + 'ignore': self.ignore, + 'selectdir': self.selectdir + } @property def directory(self): diff --git a/lib/tfw/components/history_monitor.py b/lib/tfw/components/history_monitor.py index e775c08..9d259ae 100644 --- a/lib/tfw/components/history_monitor.py +++ b/lib/tfw/components/history_monitor.py @@ -39,10 +39,14 @@ class HistoryMonitor(CallbackMixin, ObserverMixin, ABC): self.histfile = histfile self._history = [] self._last_length = len(self._history) - self.observer.schedule(CallbackEventHandler([self.histfile], - self._fetch_history, - self._invoke_callbacks), - dirname(self.histfile)) + self.observer.schedule( + CallbackEventHandler( + [self.histfile], + self._fetch_history, + self._invoke_callbacks + ), + dirname(self.histfile) + ) @property def history(self): @@ -53,7 +57,10 @@ class HistoryMonitor(CallbackMixin, ObserverMixin, ABC): with open(self.histfile, 'r') as ifile: pattern = compileregex(self.command_pattern) 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) + ] @property @abstractmethod diff --git a/lib/tfw/components/ide_event_handler.py b/lib/tfw/components/ide_event_handler.py index 327f6ef..659ade0 100644 --- a/lib/tfw/components/ide_event_handler.py +++ b/lib/tfw/components/ide_event_handler.py @@ -65,11 +65,13 @@ class FileManager: # pylint: disable=too-many-instance-attributes @property def files(self): - return [self._relpath(file) - for file in glob(join(self._workdir, '**/*'), recursive=True) - if isfile(file) - and self._is_in_allowed_dir(file) - and not self._is_blacklisted(file)] + return [ + self._relpath(file) + for file in glob(join(self._workdir, '**/*'), recursive=True) + if isfile(file) + and self._is_in_allowed_dir(file) + and not self._is_blacklisted(file) + ] @property def file_contents(self): @@ -82,11 +84,16 @@ class FileManager: # pylint: disable=too-many-instance-attributes ofile.write(value) def _is_in_allowed_dir(self, path): - return any(realpath(path).startswith(allowed_dir) - for allowed_dir in self.allowed_directories) + return any( + realpath(path).startswith(allowed_dir) + for allowed_dir in self.allowed_directories + ) def _is_blacklisted(self, file): - return any(fnmatchcase(file, blacklisted) for blacklisted in self.exclude) + return any( + fnmatchcase(file, blacklisted) + for blacklisted in self.exclude + ) def _filepath(self, filename): return join(self._workdir, filename) diff --git a/lib/tfw/components/log_monitor.py b/lib/tfw/components/log_monitor.py index d8d1825..2c6ade8 100644 --- a/lib/tfw/components/log_monitor.py +++ b/lib/tfw/components/log_monitor.py @@ -14,7 +14,10 @@ from tfw.mixins import ObserverMixin, SupervisorLogMixin class LogMonitor(ObserverMixin): def __init__(self, process_name, log_tail=0): self.prevent_log_recursion() - event_handler = SendLogWatchdogEventHandler(process_name, log_tail=log_tail) + event_handler = SendLogWatchdogEventHandler( + process_name, + log_tail=log_tail + ) self.observer.schedule( event_handler, event_handler.path @@ -30,7 +33,10 @@ class SendLogWatchdogEventHandler(PatternMatchingWatchdogEventHandler, Superviso def __init__(self, process_name, log_tail=0): self.process_name = process_name self.procinfo = self.supervisor.getProcessInfo(self.process_name) - super().__init__([self.procinfo['stdout_logfile'], self.procinfo['stderr_logfile']]) + super().__init__([ + self.procinfo['stdout_logfile'], + self.procinfo['stderr_logfile'] + ]) self.uplink = ServerUplinkConnector() self.log_tail = log_tail diff --git a/lib/tfw/components/log_monitoring_event_handler.py b/lib/tfw/components/log_monitoring_event_handler.py index eb9782f..c5dd84c 100644 --- a/lib/tfw/components/log_monitoring_event_handler.py +++ b/lib/tfw/components/log_monitoring_event_handler.py @@ -23,7 +23,12 @@ class LogMonitoringEventHandler(EventHandlerBase, MonitorManagerMixin): super().__init__(key) self.process_name = process_name self.log_tail = log_tail - MonitorManagerMixin.__init__(self, LogMonitor, self.process_name, self.log_tail) + MonitorManagerMixin.__init__( + self, + LogMonitor, + self.process_name, + self.log_tail + ) self.command_handlers = { 'process_name': self.handle_process_name, diff --git a/lib/tfw/components/process_managing_event_handler.py b/lib/tfw/components/process_managing_event_handler.py index 0a3d052..3ccf6b1 100644 --- a/lib/tfw/components/process_managing_event_handler.py +++ b/lib/tfw/components/process_managing_event_handler.py @@ -13,9 +13,11 @@ LOG = logging.getLogger(__name__) class ProcessManager(SupervisorMixin, SupervisorLogMixin): def __init__(self): - self.commands = {'start': self.start_process, - 'stop': self.stop_process, - 'restart': self.restart_process} + self.commands = { + 'start': self.start_process, + 'stop': self.stop_process, + 'restart': self.restart_process + } def __call__(self, command, process_name): return self.commands[command](process_name) @@ -50,8 +52,14 @@ class ProcessManagingEventHandler(EventHandlerBase): except SupervisorFault as fault: message['data']['error'] = fault.faultString finally: - message['data']['stdout'] = self.processmanager.read_stdout(data['process_name'], self.log_tail) - message['data']['stderr'] = self.processmanager.read_stderr(data['process_name'], self.log_tail) + message['data']['stdout'] = self.processmanager.read_stdout( + data['process_name'], + self.log_tail + ) + message['data']['stderr'] = self.processmanager.read_stderr( + data['process_name'], + self.log_tail + ) return message except KeyError: LOG.error('IGNORING MESSAGE: Invalid message received: %s', message) diff --git a/lib/tfw/components/terminado_mini_server.py b/lib/tfw/components/terminado_mini_server.py index 9c79b83..3726097 100644 --- a/lib/tfw/components/terminado_mini_server.py +++ b/lib/tfw/components/terminado_mini_server.py @@ -14,15 +14,15 @@ LOG = logging.getLogger(__name__) class TerminadoMiniServer: def __init__(self, url, port, workdir, shellcmd): self.port = port - self._term_manager = SingleTermManager(shell_command=shellcmd, - term_settings={'cwd': workdir}) - self.application = Application( - [( - url, - TerminadoMiniServer.ResetterTermSocket, - {'term_manager': self._term_manager} - )] + self._term_manager = SingleTermManager( + shell_command=shellcmd, + term_settings={'cwd': workdir} ) + self.application = Application([( + url, + TerminadoMiniServer.ResetterTermSocket, + {'term_manager': self._term_manager} + )]) @property def term_manager(self): @@ -46,5 +46,10 @@ class TerminadoMiniServer: if __name__ == '__main__': LOG.info('Terminado Mini Server listening on %s', TFWENV.TERMINADO_PORT) - TerminadoMiniServer('/terminal', TFWENV.TERMINADO_PORT, TFWENV.TERMINADO_WD, ['bash']).listen() + TerminadoMiniServer( + '/terminal', + TFWENV.TERMINADO_PORT, + TFWENV.TERMINADO_WD, + ['bash'] + ).listen() IOLoop.instance().start() diff --git a/lib/tfw/components/terminal_commands.py b/lib/tfw/components/terminal_commands.py index ae22975..a47852d 100644 --- a/lib/tfw/components/terminal_commands.py +++ b/lib/tfw/components/terminal_commands.py @@ -36,8 +36,12 @@ class TerminalCommands(ABC): self._setup_bashrc_aliases(bashrc) def _build_command_to_implementation_dict(self): - return {self._parse_command_name(fun): getattr(self, fun) for fun in dir(self) - if callable(getattr(self, fun)) and self._is_command_implementation(fun)} + return { + self._parse_command_name(fun): getattr(self, fun) + for fun in dir(self) + if callable(getattr(self, fun)) + and self._is_command_implementation(fun) + } def _setup_bashrc_aliases(self, bashrc): with open(bashrc, 'a') as ofile: diff --git a/lib/tfw/components/terminal_event_handler.py b/lib/tfw/components/terminal_event_handler.py index 5d1956e..ea135b6 100644 --- a/lib/tfw/components/terminal_event_handler.py +++ b/lib/tfw/components/terminal_event_handler.py @@ -31,7 +31,10 @@ class TerminalEventHandler(EventHandlerBase): bash_as_user_cmd = ['sudo', '-u', TAOENV.USER, 'bash'] self.terminado_server = TerminadoMiniServer( - '/terminal', TFWENV.TERMINADO_PORT, TFWENV.TERMINADO_WD, bash_as_user_cmd + '/terminal', + TFWENV.TERMINADO_PORT, + TFWENV.TERMINADO_WD, + bash_as_user_cmd ) self.commands = { diff --git a/lib/tfw/fsm_base.py b/lib/tfw/fsm_base.py index 85ac12f..7296627 100644 --- a/lib/tfw/fsm_base.py +++ b/lib/tfw/fsm_base.py @@ -20,13 +20,15 @@ class FSMBase(CallbackMixin): def __init__(self, initial: str = None, accepted_states: List[str] = None): self.accepted_states = accepted_states or [self.states[-1]] - self.machine = Machine(model=self, - states=self.states, - transitions=self.transitions, - initial=initial or self.states[0], - send_event=True, - ignore_invalid_triggers=True, - after_state_change='execute_callbacks') + self.machine = Machine( + model=self, + states=self.states, + transitions=self.transitions, + initial=initial or self.states[0], + send_event=True, + ignore_invalid_triggers=True, + after_state_change='execute_callbacks' + ) def execute_callbacks(self, event_data): self._execute_callbacks(event_data.kwargs) diff --git a/lib/tfw/linear_fsm.py b/lib/tfw/linear_fsm.py index 61e4c43..1f0e3af 100644 --- a/lib/tfw/linear_fsm.py +++ b/lib/tfw/linear_fsm.py @@ -16,6 +16,14 @@ class LinearFSM(FSMBase): self.states = list(map(str, range(number_of_steps))) self.transitions = [] for index in self.states[:-1]: - self.transitions.append({'trigger': f'step_{int(index)+1}', 'source': index, 'dest': str(int(index)+1)}) - self.transitions.append({'trigger': 'step_next', 'source': index, 'dest': str(int(index)+1)}) + self.transitions.append({ + 'trigger': f'step_{int(index)+1}', + 'source': index, + 'dest': str(int(index)+1) + }) + self.transitions.append({ + 'trigger': 'step_next', + 'source': index, + 'dest': str(int(index)+1) + }) super(LinearFSM, self).__init__() diff --git a/lib/tfw/mixins/callback_mixin.py b/lib/tfw/mixins/callback_mixin.py index de0de6c..3c94e71 100644 --- a/lib/tfw/mixins/callback_mixin.py +++ b/lib/tfw/mixins/callback_mixin.py @@ -15,8 +15,8 @@ class CallbackMixin: """ Subscribe a callable to invoke once an event is triggered. :param callback: callable to be executed on events - :param *args: arguments passed to callable - :param **kwargs: kwargs passed to callable + :param args: arguments passed to callable + :param kwargs: kwargs passed to callable """ fun = partial(callback, *args, **kwargs) self._callbacks.append(fun) diff --git a/lib/tfw/networking/message_sender.py b/lib/tfw/networking/message_sender.py index d1bf0d5..2210d0a 100644 --- a/lib/tfw/networking/message_sender.py +++ b/lib/tfw/networking/message_sender.py @@ -26,5 +26,7 @@ class MessageSender: 'timestamp': datetime.now().isoformat(), 'message': message } - self.server_connector.send({'key': self.key, - 'data': data}) + self.server_connector.send({ + 'key': self.key, + 'data': data + }) diff --git a/lib/tfw/networking/serialization.py b/lib/tfw/networking/serialization.py index 1eaf933..6a6a3e7 100644 --- a/lib/tfw/networking/serialization.py +++ b/lib/tfw/networking/serialization.py @@ -43,11 +43,17 @@ def deserialize_tfw_msg(*args): def _serialize_all(*args): - return tuple(_serialize_single(arg) for arg in args) + return tuple( + _serialize_single(arg) + for arg in args + ) def _deserialize_all(*args): - return tuple(_deserialize_single(arg) for arg in args) + return tuple( + _deserialize_single(arg) + for arg in args + ) def _serialize_single(data): diff --git a/lib/tfw/networking/server/tfw_server.py b/lib/tfw/networking/server/tfw_server.py index 89ad3d4..78cc8ee 100644 --- a/lib/tfw/networking/server/tfw_server.py +++ b/lib/tfw/networking/server/tfw_server.py @@ -30,13 +30,16 @@ class TFWServer: self._fsm.subscribe_callback(self._fsm_updater.update) self._event_handler_connector = EventHandlerConnector() - self.application = Application( - [(r'/ws', ZMQWebSocketProxy, {'make_eventhandler_message': self.make_eventhandler_message, - 'proxy_filter': self.proxy_filter, - 'handle_trigger': self.handle_trigger, - 'event_handler_connector': self._event_handler_connector})] + self.application = Application([( + r'/ws', ZMQWebSocketProxy,{ + 'make_eventhandler_message': self.make_eventhandler_message, + 'proxy_filter': self.proxy_filter, + 'handle_trigger': self.handle_trigger, + 'event_handler_connector': self._event_handler_connector + })] ) - #self.controller_responder = ControllerResponder(self.fsm) TODO: add this once controller stuff is resolved + # self.controller_responder = ControllerResponder(self.fsm) + # TODO: add this once controller stuff is resolved @property def fsm(self): @@ -97,8 +100,11 @@ class FSMManager: self.trigger_predicates[trigger].extend(predicates) def unsubscribe_predicate(self, trigger, *predicates): - self.trigger_predicates[trigger] = [predicate for predicate in self.trigger_predicates[trigger] - not in predicates] + self.trigger_predicates[trigger] = [ + predicate + for predicate in self.trigger_predicates[trigger] + not in predicates + ] class FSMUpdater: @@ -111,10 +117,18 @@ class FSMUpdater: self.uplink.send(self.generate_fsm_update()) def generate_fsm_update(self): - return {'key': 'FSMUpdate', - 'data': self.get_fsm_state_and_transitions()} + return { + 'key': 'FSMUpdate', + 'data': self.get_fsm_state_and_transitions() + } def get_fsm_state_and_transitions(self): state = self.fsm.state - valid_transitions = [{'trigger': trigger} for trigger in self.fsm.machine.get_triggers(self.fsm.state)] - return {'current_state': state, 'valid_transitions': valid_transitions} + valid_transitions = [ + {'trigger': trigger} + for trigger in self.fsm.machine.get_triggers(self.fsm.state) + ] + return { + 'current_state': state, + 'valid_transitions': valid_transitions + }