Make project comply new coding standards and pylint

This commit is contained in:
Kristóf Tóth 2018-03-30 17:50:20 +02:00
parent c2375e9206
commit 3ae0995b95
11 changed files with 64 additions and 35 deletions

View File

@ -2,7 +2,7 @@ from collections import namedtuple
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)
for envvar in environ.keys()
if envvar.startswith(prefix)}

View File

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

View File

@ -61,7 +61,8 @@ class WebideReloadWatchdogEventHandler(FileSystemWatchdogEventHandler):
@RateLimiter(rate_per_second=2)
def on_modified(self, event):
if self._paused: return
if self._paused:
return
if self.ignore > 0:
self.ignore = self.ignore - 1
return

View File

@ -24,7 +24,8 @@ class DirectoryMonitoringEventHandler(TriggerlessEventHandler):
@directory.setter
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
@property
@ -33,8 +34,10 @@ class DirectoryMonitoringEventHandler(TriggerlessEventHandler):
def reload_monitor(self):
if self._monitor:
try: self._monitor.stop()
except KeyError: logging.debug('Working directory was removed ignoring...')
try:
self._monitor.stop()
except KeyError:
logging.debug('Working directory was removed ignoring...')
self._monitor = DirectoryMonitor(self._directory)
self._monitor.watch() # This runs on a separate thread

View File

@ -1,5 +1,6 @@
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 watchdog.observers import Observer
@ -39,7 +40,7 @@ class HistoryMonitor(CallbackMixin, ABC):
def _fetch_history(self):
self._last_length = len(self._history)
with open(self.histfile, 'r') as ifile:
pattern = compile(self.command_pattern)
pattern = compileregex(self.command_pattern)
data = ifile.read()
self._history = [self.sanitize_command(command) for command in findall(pattern, data)]

View File

@ -22,8 +22,10 @@ class FileManager:
@exclude.setter
def exclude(self, exclude):
if exclude is None: return
if not isinstance(exclude, Iterable): raise TypeError('Exclude must be Iterable!')
if exclude is None:
return
if not isinstance(exclude, Iterable):
raise TypeError('Exclude must be Iterable!')
self._exclude = exclude
@property
@ -89,28 +91,38 @@ class SourceCodeEventHandler(TriggerlessEventHandler):
def reload_monitor(self):
if self._monitor:
try: self._monitor.stop()
except KeyError: logging.debug('Working directory was removed ignoring...')
try:
self._monitor.stop()
except KeyError:
logging.debug('Working directory was removed ignoring...')
self._monitor = DirectoryMonitor(self.filemanager.workdir)
self._monitor.watch() # This runs on a separate thread
def read(self, data):
try: data['content'] = self.filemanager.file_contents
except PermissionError: data['content'] = 'You have no permission to open that file :('
except FileNotFoundError: data['content'] = 'This file was removed :('
except Exception: data['content'] = 'Failed to read file :('
try:
data['content'] = self.filemanager.file_contents
except PermissionError:
data['content'] = 'You have no permission to open that file :('
except FileNotFoundError:
data['content'] = 'This file was removed :('
except Exception:
data['content'] = 'Failed to read file :('
return data
def write(self, data):
self.monitor.ignore = self.monitor.ignore + 1
try: self.filemanager.file_contents = data['content']
except Exception: LOG.exception('Error writing file!')
try:
self.filemanager.file_contents = data['content']
except Exception:
LOG.exception('Error writing file!')
del data['content']
return data
def select(self, data):
try: self.filemanager.filename = data['filename']
except EnvironmentError: LOG.exception('Failed to select file "%s"', data['filename'])
try:
self.filemanager.filename = data['filename']
except EnvironmentError:
LOG.exception('Failed to select file "%s"', data['filename'])
return data
def select_dir(self, data):
@ -127,8 +139,10 @@ class SourceCodeEventHandler(TriggerlessEventHandler):
return data
def exclude(self, data):
try: self.filemanager.exclude = list(data['exclude'])
except TypeError: LOG.error('Exclude must be Iterable!')
try:
self.filemanager.exclude = list(data['exclude'])
except TypeError:
LOG.error('Exclude must be Iterable!')
return data
def attach_fileinfo(self, data):

View File

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

View File

@ -16,7 +16,8 @@ class EventHandlerBase(ABC):
def event_handler_callback(self, msg_parts):
key, message = deserialize_all(*msg_parts)
response = self.dispatch_handling(key, message)
if response is None: return
if response is None:
return
self.server_connector.send(key, response)
@abstractmethod
@ -24,8 +25,10 @@ class EventHandlerBase(ABC):
raise NotImplementedError
def _dispatch_handling(self, key, message):
if key != 'reset': return self.handle_event(key, message)
else: return self.handle_reset(message)
if key != 'reset':
return self.handle_event(key, message)
else:
return self.handle_reset(message)
@abstractmethod
def handle_event(self, key, message):

View File

@ -33,5 +33,6 @@ class EventHandlerConnector(EventHandlerDownlinkConnector, EventHandlerUplinkCon
self._zmq_pull_stream.on_recv(callback)
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))

View File

@ -33,8 +33,10 @@ class TFWServer:
def make_response(self, message):
trigger = message.get('trigger', '')
try: self._fsm_manager.trigger(trigger, message)
except AttributeError: LOG.debug('FSM failed to execute nonexistent trigger: "%s"', trigger)
try:
self._fsm_manager.trigger(trigger, message)
except AttributeError:
LOG.debug('FSM failed to execute nonexistent trigger: "%s"', trigger)
return message
def proxy_filter(self, message):
@ -62,8 +64,10 @@ class FSMManager:
self.messenge_sender.send('FSM', message)
if all(predicate_results):
try: self.fsm.trigger(trigger, message=message)
except AttributeError: LOG.debug('FSM failed to execute nonexistent trigger: "%s"', trigger)
try:
self.fsm.trigger(trigger, message=message)
except AttributeError:
LOG.debug('FSM failed to execute nonexistent trigger: "%s"', trigger)
def subscribe_predicate(self, trigger, *predicates):
self.trigger_predicates[trigger].extend(predicates)

View File

@ -34,8 +34,10 @@ class ZMQWebSocketHandler(WebSocketHandler, ABC):
for instance in cls.instances:
instance.write_message(data)
else:
try: keyhandlers[key](data['data'])
except KeyError: LOG.error('Invalid mirror message format! Ignoring.')
try:
keyhandlers[key](data['data'])
except KeyError:
LOG.error('Invalid mirror message format! Ignoring.')
@classmethod
def mirror(cls, data):