diff --git a/.pylintrc b/.pylintrc index f3ba45c3..6aeab09c 100644 --- a/.pylintrc +++ b/.pylintrc @@ -61,7 +61,7 @@ confidence= # no Warning level messages displayed, use "--disable=all --enable=classes # --disable=W". disable=raw-checker-failed, - too-many-locals, + too-many-locals, too-many-branches, too-many-nested-blocks, too-many-instance-attributes, @@ -83,7 +83,6 @@ disable=raw-checker-failed, arguments-renamed, invalid-overridden-method, abstract-method, - logging-not-lazy, protected-access, broad-exception-caught, unspecified-encoding diff --git a/src/powerapi/actor/actor.py b/src/powerapi/actor/actor.py index ae0d43eb..21db95c8 100644 --- a/src/powerapi/actor/actor.py +++ b/src/powerapi/actor/actor.py @@ -175,7 +175,7 @@ def _setup(self): self.socket_interface.setup() - self.logger.debug(self.name + ' process created.') + self.logger.debug('Actor "%s" process created', self.name) self._signal_handler_setup() @@ -217,25 +217,23 @@ def _initial_behaviour(self): """ msg = self.receive() - # Timeout if msg is None: - return - # Message - else: - try: - handler = self.state.get_corresponding_handler(msg) - handler.handle_message(msg) - except UnknownMessageTypeException: - self.logger.warning("UnknownMessageTypeException: " + str(msg)) - except HandlerException: - self.logger.warning("HandlerException") + return # Timeout + + try: + handler = self.state.get_corresponding_handler(msg) + handler.handle_message(msg) + except UnknownMessageTypeException: + self.logger.warning("Unknown message type: %s", msg) + except HandlerException: + self.logger.warning("Failed to handle message: %s", msg) def _kill_process(self): """ Kill the actor (close sockets) """ self.socket_interface.close() - self.logger.debug(self.name + ' teardown') + self.logger.debug('Actor "%s" teardown', self.name) def connect_data(self): """ @@ -259,14 +257,14 @@ def send_control(self, msg): :param Object msg: the message to send to this actor """ self.socket_interface.send_control(msg) - self.logger.debug('send control [' + str(msg) + '] to Actor ' + self.name) + self.logger.debug('Send control to actor "%s" : %s', self.name, msg) def receive_control(self, timeout=None): """ Receive a message from this actor on the control canal """ msg = self.socket_interface.receive_control(timeout) - self.logger.debug('Actor ' + self.name + ' receive control : [' + str(msg) + ']') + self.logger.debug('Actor "%s" received control : %s', self.name, msg) return msg def send_data(self, msg): @@ -276,7 +274,7 @@ def send_data(self, msg): :param Object msg: the message to send to this actor """ self.socket_interface.send_data(msg) - self.logger.debug('Actor ' + self.name + ' send data [' + str(msg) + '] to ' + self.name) + self.logger.debug('Send data to actor "%s" : %s ', self.name, msg) def receive(self): """ @@ -287,7 +285,7 @@ def receive(self): :rtype: a list of Object """ msg = self.socket_interface.receive() - self.logger.debug('Actor ' + self.name + ' receive data : [' + str(msg) + ']') + self.logger.debug('Actor "%s" received data : %s', self.name, msg) return msg def soft_kill(self): diff --git a/src/powerapi/actor/socket_interface.py b/src/powerapi/actor/socket_interface.py index 9a3e2b9d..f8321ff3 100644 --- a/src/powerapi/actor/socket_interface.py +++ b/src/powerapi/actor/socket_interface.py @@ -145,7 +145,7 @@ def _create_socket(self, socket_type, linger_value): socket.set_hwm(0) port_number = socket.bind_to_random_port(LOCAL_ADDR) self.poller.register(socket, zmq.POLLIN) - self.logger.debug("bind to " + LOCAL_ADDR + ':' + str(port_number)) + self.logger.debug('Bind socket to %s:%d', LOCAL_ADDR, port_number) return (socket, port_number) def receive(self): @@ -239,7 +239,7 @@ def connect_data(self): self.push_socket.setsockopt(zmq.LINGER, -1) self.push_socket.set_hwm(0) self.push_socket.connect(self.pull_socket_address) - self.logger.debug("connected data to %s" % (self.pull_socket_address)) + self.logger.debug('Connected data socket to %s', self.pull_socket_address) def connect_control(self): """ @@ -258,7 +258,7 @@ def connect_control(self): self.control_socket.setsockopt(zmq.LINGER, 0) self.control_socket.set_hwm(0) self.control_socket.connect(self.control_socket_address) - self.logger.debug("connected control to %s" % (self.control_socket_address)) + self.logger.debug('Connected control socket to %s', self.control_socket_address) def send_control(self, msg): """ diff --git a/src/powerapi/cli/config_validator.py b/src/powerapi/cli/config_validator.py index 674a1cb2..6282aab9 100644 --- a/src/powerapi/cli/config_validator.py +++ b/src/powerapi/cli/config_validator.py @@ -75,13 +75,13 @@ def validate(config: Dict): pre_processor_config = config['pre-processor'][pre_processor_id] if 'puller' not in pre_processor_config: - logging.error("no puller name found for pre-processor " + pre_processor_id) + logging.error("No puller name found for pre-processor: %s ", pre_processor_id) raise MissingArgumentException(argument_name='puller') puller_id = pre_processor_config['puller'] if puller_id not in config['input']: - logging.error("puller actor " + puller_id + " does not exist") + logging.error("Puller actor '%s' does not exist", puller_id) raise UnexistingActorException(actor=puller_id) elif 'post-processor' in config: @@ -89,13 +89,13 @@ def validate(config: Dict): post_processor_config = config['post-processor'][post_processor_id] if 'pusher' not in post_processor_config: - logging.error("no pusher name found for post-processor " + post_processor_id) + logging.error("No pusher name found for post-processor: %s", post_processor_id) raise MissingArgumentException(argument_name='pusher') pusher_id = post_processor_config['pusher'] if pusher_id not in config['output']: - logging.error("pusher actor " + pusher_id + " does not exist") + logging.error("Pusher actor '%s' does not exist", pusher_id) raise UnexistingActorException(actor=pusher_id) ConfigValidator._validate_input(config) diff --git a/src/powerapi/cli/parsing_manager.py b/src/powerapi/cli/parsing_manager.py index c0a99b59..2ece6a41 100644 --- a/src/powerapi/cli/parsing_manager.py +++ b/src/powerapi/cli/parsing_manager.py @@ -26,6 +26,7 @@ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + import json import logging import sys @@ -121,8 +122,7 @@ def add_subgroup(self, name: str, help_text: str = '', prefix: str = ''): try: self.cli_parser.add_subgroup(subgroup_type=name, help_text=help_text, prefix=prefix) except AlreadyAddedSubgroupException as exn: - msg = "Configuration error: " + exn.msg - logging.error(msg) + logging.error("Configuration error: %s", exn.msg) sys.exit(-1) def add_subgroup_parser(self, subgroup_name: str, subgroup_parser: SubgroupConfigParsingManager): @@ -220,8 +220,9 @@ def parse(self, args: list = None) -> dict: for current_arg in args: if current_arg == '--config-file': if current_position + 1 == len(args): - logging.error("CLI Error: config file path needed with argument --config-file") + logging.error("CLI Error: Config filepath needed with argument --config-file") sys.exit(-1) + filename = args[current_position + 1] args.pop(current_position + 1) args.pop(current_position) @@ -242,44 +243,35 @@ def parse(self, args: list = None) -> dict: conf = self.validate(conf) except MissingValueException as exn: - msg = 'CLI error: argument ' + exn.argument_name + ': expect a value' - logging.error(msg) + logging.error('CLI error: Argument "%s" expect a value', exn.argument_name) sys.exit(-1) except BadTypeException as exn: - msg = "Configuration error: " + exn.msg - logging.error(msg) + logging.error('Configuration error: %s', exn.msg) sys.exit(-1) except UnknownArgException as exn: - msg = 'Configuration error: unknown argument ' + exn.argument_name - logging.error(msg) + logging.error('Configuration error: Argument "%s" is unknown', exn.argument_name) sys.exit(-1) except BadContextException as exn: - msg = 'CLI error: argument ' + exn.argument_name - msg += ' not used in the correct context\nUse it with the following arguments:' - for main_arg_name, context_name in exn.context_list: - msg += '\n --' + main_arg_name + ' ' + context_name - logging.error(msg) + logging.error('CLI error: %s', exn.msg) sys.exit(-1) except FileNotFoundError: - logging.error("Configuration Error: configuration file not found") + logging.error("Configuration Error: Configuration file not found") sys.exit(-1) except json.JSONDecodeError as exn: - logging.error( - 'Configuration Error: JSON Error: ' + exn.msg + ' at line' + str(exn.lineno) + ' colon ' + - str(exn.colno)) + logging.error('JSON error: "%s" at line %d column %d', exn.msg, exn.lineno, exn.colno) sys.exit(-1) except MissingArgumentException as exn: - logging.error("Configuration Error: " + exn.msg) + logging.error("Configuration Error: %s", exn.msg) sys.exit(-1) except RepeatedArgumentException as exn: - logging.error("Configuration Error: " + exn.msg) + logging.error("Configuration Error: %s", exn.msg) sys.exit(-1) return conf diff --git a/tests/utils/actor/dummy_handlers.py b/tests/utils/actor/dummy_handlers.py index de9b773c..89ed9e7a 100644 --- a/tests/utils/actor/dummy_handlers.py +++ b/tests/utils/actor/dummy_handlers.py @@ -48,4 +48,4 @@ def handle(self, msg: Message): :param Object msg: the message received by the actor """ self.state.pipe.send((self.state.actor.name, msg)) - logging.debug('received : ' + str(msg), extra={'actor_name': self.state.actor.name}) + logging.debug('Received message: %s', msg, extra={'actor_name': self.state.actor.name})