From 7bd369f20c9d0074b47334c68f57b055ea5acdf0 Mon Sep 17 00:00:00 2001 From: jo Date: Mon, 25 Jul 2022 01:35:14 +0200 Subject: [PATCH] refactor(analyzer): fix linting --- analyzer/libretime_analyzer/main.py | 2 +- .../libretime_analyzer/message_listener.py | 28 +++++++++--------- .../libretime_analyzer/pipeline/pipeline.py | 29 +++++++------------ 3 files changed, 25 insertions(+), 34 deletions(-) diff --git a/analyzer/libretime_analyzer/main.py b/analyzer/libretime_analyzer/main.py index 4bb4e170e..e33de9f0c 100644 --- a/analyzer/libretime_analyzer/main.py +++ b/analyzer/libretime_analyzer/main.py @@ -41,6 +41,6 @@ def cli( # Start listening for RabbitMQ messages telling us about newly # uploaded files. This blocks until we receive a shutdown signal. - _msg_listener = MessageListener(config.rabbitmq) + MessageListener(config.rabbitmq) StatusReporter.stop_thread() diff --git a/analyzer/libretime_analyzer/message_listener.py b/analyzer/libretime_analyzer/message_listener.py index cef9fed2e..421c64eab 100644 --- a/analyzer/libretime_analyzer/message_listener.py +++ b/analyzer/libretime_analyzer/message_listener.py @@ -1,7 +1,7 @@ import json -import queue import signal import time +from queue import Queue import pika from libretime_shared.config import RabbitMQConfig @@ -39,11 +39,11 @@ class MessageListener: break # Break out of the while loop and exit the application except OSError: pass - except pika.exceptions.AMQPError as e: + except pika.exceptions.AMQPError as exception: if self._shutdown: break logger.error("Connection to message queue failed. ") - logger.error(e) + logger.error(exception) logger.info("Retrying in 5 seconds...") time.sleep(5) @@ -134,7 +134,7 @@ class MessageListener: callback_url, api_key, audio_metadata ) - except KeyError as e: + except KeyError: logger.exception("A mandatory field was missing from the message.") channel.basic_nack( delivery_tag=method_frame.delivery_tag, @@ -142,8 +142,8 @@ class MessageListener: requeue=False, ) - except Exception as e: - logger.exception(e) + except Exception as exception: + logger.exception(exception) channel.basic_nack( delivery_tag=method_frame.delivery_tag, multiple=False, @@ -171,23 +171,23 @@ class MessageListener: ): metadata = {} - q = queue.Queue() + queue = Queue() try: Pipeline.run_analysis( - q, + queue, audio_file_path, import_directory, original_filename, storage_backend, file_prefix, ) - metadata = q.get() - except Exception as e: - logger.error("Analyzer pipeline exception: %s" % str(e)) + metadata = queue.get() + except Exception as exception: + logger.error("Analyzer pipeline exception: %s" % str(exception)) metadata["import_status"] = PipelineStatus.failed - # Ensure our queue doesn't fill up and block due to unexpected behaviour. Defensive code. - while not q.empty(): - q.get() + # Ensure our queue doesn't fill up and block due to unexpected behavior. Defensive code. + while not queue.empty(): + queue.get() return metadata diff --git a/analyzer/libretime_analyzer/pipeline/pipeline.py b/analyzer/libretime_analyzer/pipeline/pipeline.py index f8c6a1e5d..438ab99ff 100644 --- a/analyzer/libretime_analyzer/pipeline/pipeline.py +++ b/analyzer/libretime_analyzer/pipeline/pipeline.py @@ -1,5 +1,3 @@ -""" Analyzes and imports an audio file into the Airtime library. -""" from enum import Enum from queue import Queue from typing import Any, Dict @@ -31,9 +29,7 @@ class Pipeline: This currently performs metadata extraction (eg. gets the ID3 tags from an MP3), then moves the file to the Airtime music library (stor/imported), and returns - the results back to the parent process. This class is used in an isolated process - so that if it crashes, it does not kill the entire airtime_analyzer daemon and - the failure to import can be reported back to the web application. + the results back to the parent process. """ @staticmethod @@ -90,7 +86,7 @@ class Pipeline: # Analyze the audio file we were told to analyze: # First, we extract the ID3 tags and other metadata: - metadata = dict() + metadata = {} metadata["file_prefix"] = file_prefix metadata = analyze_metadata(audio_file_path, metadata) @@ -102,20 +98,15 @@ class Pipeline: audio_file_path, import_directory, original_filename, metadata ) - metadata["import_status"] = 0 # Successfully imported + metadata["import_status"] = PipelineStatus.succeed - # Note that the queue we're putting the results into is our interprocess communication - # back to the main process. - - # Pass all the file metadata back to the main analyzer process, which then passes - # it back to the Airtime web application. + # Pass all the file metadata back to the main analyzer process queue.put(metadata) - except UnplayableFileError as e: - logger.exception(e) + except UnplayableFileError as exception: + logger.exception(exception) metadata["import_status"] = PipelineStatus.failed metadata["reason"] = "The file could not be played." - raise e - except Exception as e: - # Ensures the traceback for this child process gets written to our log files: - logger.exception(e) - raise e + raise exception + except Exception as exception: + logger.exception(exception) + raise exception