From db8f617aa78698760e5aaa198445d349755366a1 Mon Sep 17 00:00:00 2001 From: Isaac Riley Date: Tue, 26 Jul 2022 14:56:21 +0200 Subject: [PATCH] clean up config hygiene; align queue manager and storage signature --- pyinfra/config.py | 5 +++-- pyinfra/queue/queue_manager.py | 11 +++++------ pyinfra/storage/adapters/azure.py | 3 +-- pyinfra/storage/adapters/s3.py | 29 +++++++++++++++++------------ pyinfra/storage/storage.py | 4 ---- 5 files changed, 26 insertions(+), 26 deletions(-) diff --git a/pyinfra/config.py b/pyinfra/config.py index 8efc335..ee1efa7 100644 --- a/pyinfra/config.py +++ b/pyinfra/config.py @@ -50,8 +50,9 @@ class Config(object): self.storage_secret = read_from_environment("STORAGE_SECRET", "password") # Connection string for Azure storage - self.storage_azureconnectionstring = read_from_environment("STORAGE_AZURECONNECTIONSTRING", - "DefaultEndpointsProtocol=...") + self.storage_azureconnectionstring = read_from_environment( + "STORAGE_AZURECONNECTIONSTRING", "DefaultEndpointsProtocol=..." + ) def get_config() -> Config: diff --git a/pyinfra/queue/queue_manager.py b/pyinfra/queue/queue_manager.py index 17a7ada..e1d4543 100644 --- a/pyinfra/queue/queue_manager.py +++ b/pyinfra/queue/queue_manager.py @@ -9,8 +9,6 @@ import pika.exceptions from pyinfra.config import get_config, Config -CONFIG = get_config() - pika_logger = logging.getLogger("pika") pika_logger.setLevel(logging.WARNING) @@ -32,7 +30,7 @@ def _get_n_previous_attempts(props): class QueueManager(object): - def __init__(self, config: Config = CONFIG): + def __init__(self, config: Config): connection_params = get_connection_params(config) atexit.register(self.stop_consuming) @@ -43,7 +41,7 @@ class QueueManager(object): self._channel = self._connection.channel() self._channel.basic_qos(prefetch_count=1) - args = {"x-dead-letter-exchange": "", "x-dead-letter-routing-key": CONFIG.dead_letter_queue} + args = {"x-dead-letter-exchange": "", "x-dead-letter-routing-key": config.dead_letter_queue} self._input_queue = config.request_queue self._output_queue = config.response_queue @@ -54,7 +52,7 @@ class QueueManager(object): self._consumer_token = None self.logger = logging.getLogger("queue_manager") - self.logger.setLevel(CONFIG.logging_level_root) + self.logger.setLevel(config.logging_level_root) def start_consuming(self, process_message_callback: Callable): callback = self._create_queue_callback(process_message_callback) @@ -91,7 +89,8 @@ class QueueManager(object): self._channel.basic_publish("", self._output_queue, json.dumps(callback_result).encode()) self.logger.info( - f"Result published, acknowledging incoming message with delivery_tag {frame.delivery_tag}") + f"Result published, acknowledging incoming message with delivery_tag {frame.delivery_tag}" + ) self._channel.basic_ack(frame.delivery_tag) self.logger.info(f"Message with delivery_tag {frame.delivery_tag} processed") diff --git a/pyinfra/storage/adapters/azure.py b/pyinfra/storage/adapters/azure.py index 8f59ea7..9f451ec 100644 --- a/pyinfra/storage/adapters/azure.py +++ b/pyinfra/storage/adapters/azure.py @@ -72,5 +72,4 @@ class AzureStorageAdapter(object): def get_azure_storage(config: Config): - return AzureStorageAdapter( - BlobServiceClient.from_connection_string(conn_str=config.storage_azureconnectionstring)) + return AzureStorageAdapter(BlobServiceClient.from_connection_string(conn_str=config.storage_azureconnectionstring)) diff --git a/pyinfra/storage/adapters/s3.py b/pyinfra/storage/adapters/s3.py index ef2b1d1..0dc430f 100644 --- a/pyinfra/storage/adapters/s3.py +++ b/pyinfra/storage/adapters/s3.py @@ -16,11 +16,14 @@ logger = logging.getLogger(CONFIG.logging_level_root) ALLOWED_CONNECTION_SCHEMES = {"http", "https"} URL_VALIDATOR = re.compile( - r"^((" + - r"([A-Za-z]{3,9}:(?:\/\/)?)" + - r"(?:[\-;:&=\+\$,\w]+@)?" + r"[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)" + - r"[A-Za-z0-9\.\-]+)" + r"((?:\/[\+~%\/\.\w\-_]*)?" + - r"\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)") + r"^((" + + r"([A-Za-z]{3,9}:(?:\/\/)?)" + + r"(?:[\-;:&=\+\$,\w]+@)?" + + r"[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)" + + r"[A-Za-z0-9\.\-]+)" + + r"((?:\/[\+~%\/\.\w\-_]*)?" + + r"\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)" +) class S3StorageAdapter(object): @@ -79,10 +82,12 @@ def _parse_endpoint(endpoint): def get_s3_storage(config: Config): - return S3StorageAdapter(Minio( - **_parse_endpoint(config.storage_endpoint), - access_key=config.storage_key, - secret_key=config.storage_secret, - # FIXME Is this still needed? Check and if yes, add it to config - # region=config.region, - )) + return S3StorageAdapter( + Minio( + **_parse_endpoint(config.storage_endpoint), + access_key=config.storage_key, + secret_key=config.storage_secret, + # FIXME Is this still needed? Check and if yes, add it to config + # region=config.region, + ) + ) diff --git a/pyinfra/storage/storage.py b/pyinfra/storage/storage.py index dcd777a..15313f3 100644 --- a/pyinfra/storage/storage.py +++ b/pyinfra/storage/storage.py @@ -4,10 +4,6 @@ from pyinfra.config import get_config, Config from pyinfra.storage.adapters.azure import get_azure_storage from pyinfra.storage.adapters.s3 import get_s3_storage -CONFIG = get_config() -logger = logging.getLogger(__name__) -logger.setLevel(CONFIG.logging_level_root) - def get_storage(config: Config):