Skip to content

Commit

Permalink
Merge pull request #261 from powerapi-ng/refactor/fix-pylint-logging-…
Browse files Browse the repository at this point in the history
…not-lazy

refactor: Fix pylint logging-not-lazy warnings
  • Loading branch information
gfieni authored Mar 4, 2024
2 parents 816d240 + 7d27402 commit deff5c6
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 47 deletions.
3 changes: 1 addition & 2 deletions .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
32 changes: 15 additions & 17 deletions src/powerapi/actor/actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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):
"""
Expand All @@ -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):
Expand All @@ -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):
"""
Expand All @@ -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):
Expand Down
6 changes: 3 additions & 3 deletions src/powerapi/actor/socket_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
"""
Expand All @@ -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):
"""
Expand Down
8 changes: 4 additions & 4 deletions src/powerapi/cli/config_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,27 +75,27 @@ 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:
for post_processor_id in config['post-processor']:
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)
Expand Down
32 changes: 12 additions & 20 deletions src/powerapi/cli/parsing_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand All @@ -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
2 changes: 1 addition & 1 deletion tests/utils/actor/dummy_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})

0 comments on commit deff5c6

Please sign in to comment.