From 86c6d1f02a56c0347ef3cc3e544fcfeb28fcb64b Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Tue, 23 Jul 2024 00:45:59 +0200 Subject: [PATCH] Ruff --- secret_loader/__init__.py | 34 +++++++++++++++++++++++++--------- test/test_init.py | 3 ++- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/secret_loader/__init__.py b/secret_loader/__init__.py index 4c6d80f..5e76300 100644 --- a/secret_loader/__init__.py +++ b/secret_loader/__init__.py @@ -6,7 +6,8 @@ Usage: ```python import secret_loader -secrets = secret_loader.SecretLoader(env_key_prefix = 'MYAPP') + +secrets = secret_loader.SecretLoader(env_key_prefix='MYAPP') db_username = secrets.load_or_fail('DATABASE_USERNAME') db_password = secrets.load_or_fail('DATABASE_PASSWORD') @@ -90,6 +91,7 @@ See more ways to supply the secret here: https://gitfub.space/Jmaa/secret_loader """ + class SecretLoader: """Main entry point for loading secrets. @@ -110,8 +112,12 @@ class SecretLoader: # Setup environment self.env_key_prefix = self._load_or_none(ENV_KEY_PREFIX) if self.env_key_prefix is not None: - assert self.env_key_prefix == self.env_key_prefix.upper(), 'Prefix must be uppercase' - assert not self.env_key_prefix.endswith('_'), 'Prefix must not end with _ (this will be added automatically)' + assert ( + self.env_key_prefix == self.env_key_prefix.upper() + ), 'Prefix must be uppercase' + assert not self.env_key_prefix.endswith( + '_', + ), 'Prefix must not end with _ (this will be added automatically)' # Setup pass self.pass_folder = self._load_or_none(ENV_KEY_PASS_FOLDER) @@ -165,7 +171,9 @@ class SecretLoader: Returns `None` if the secret is not present in either the environment or the directory. """ - filepath: Path | str | None = os.environ.get(f'{self.env_key_prefix}_{secret_name.upper()}') + filepath: Path | str | None = os.environ.get( + f'{self.env_key_prefix}_{secret_name.upper()}', + ) if filepath is None: filepath = DEFAULT_SECRETS_DIRECTORY / secret_name.lower() @@ -186,7 +194,7 @@ class SecretLoader: return None cmd = ['pass', 'show', f'{self.pass_folder}/{secret_name.lower()}'] - process = subprocess.run(cmd, capture_output = True) + process = subprocess.run(cmd, capture_output=True) if process.returncode: return None @@ -218,11 +226,19 @@ class SecretLoader: enabled. """ solutions_list = [] - solutions_list.append(f'Write secret to file: \033[1m{DEFAULT_SECRETS_DIRECTORY}/{secret_name.lower()}\033[0m') + solutions_list.append( + f'Write secret to file: \033[1m{DEFAULT_SECRETS_DIRECTORY}/{secret_name.lower()}\033[0m', + ) if self.env_key_prefix is not None: - solutions_list.append(f'Add environment variable pointing to written secret: \033[1m{self.env_key_prefix}_{secret_name.upper()}\033[0m') + solutions_list.append( + f'Add environment variable pointing to written secret: \033[1m{self.env_key_prefix}_{secret_name.upper()}\033[0m', + ) if self.pass_folder is not None: - solutions_list.append(f'Write secret to password store entry: \033[1m{self.pass_folder}/{secret_name.lower()}\033[0m') + solutions_list.append( + f'Write secret to password store entry: \033[1m{self.pass_folder}/{secret_name.lower()}\033[0m', + ) solutions_list = '\n'.join([f'* {s}' for s in solutions_list]) - return ERROR_MESSAGE_FORMAT.format(secret_name = secret_name, solutions_list=solutions_list) + return ERROR_MESSAGE_FORMAT.format( + secret_name=secret_name, solutions_list=solutions_list, + ) diff --git a/test/test_init.py b/test/test_init.py index 3687c8a..37f50b4 100644 --- a/test/test_init.py +++ b/test/test_init.py @@ -2,10 +2,11 @@ import secret_loader def test_hardcoded(): - loader = secret_loader.SecretLoader(ENV_KEY_PREFIX = 'TEST', KEY = 'VALUE') + loader = secret_loader.SecretLoader(ENV_KEY_PREFIX='TEST', KEY='VALUE') assert loader.load('ENV_KEY_PREFIX') == 'TEST' assert loader.load('KEY') == 'VALUE' + def test_lookup_unknown(): loader = secret_loader.SecretLoader() assert loader.load('UNKNOWN') is None