Skip to content

Commit

Permalink
Black formatting applied
Browse files Browse the repository at this point in the history
  • Loading branch information
Veinar committed Dec 9, 2024
1 parent 662e656 commit 5358e6a
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 15 deletions.
12 changes: 9 additions & 3 deletions envcloak/commands/decrypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
debug_log,
calculate_required_space,
list_files_to_encrypt,
read_key_file, conditional_echo, conditional_secho
read_key_file,
conditional_echo,
conditional_secho,
)
from envcloak.handlers import (
handle_directory_preview,
Expand Down Expand Up @@ -132,7 +134,9 @@ def decrypt(
debug,
)
decrypt_file(input, output, key, validate_integrity=not skip_sha_validation)
conditional_echo(f"File {input} decrypted -> {output} using key {key_file}", quiet)
conditional_echo(
f"File {input} decrypted -> {output} using key {key_file}", quiet
)
elif directory:
debug_log(f"Debug: Decrypting files in directory {directory}.", debug)
traverse_and_process_files(
Expand All @@ -149,7 +153,9 @@ def decrypt(
),
recursion=recursion,
)
conditional_echo(f"All files in directory {directory} decrypted -> {output}", quiet)
conditional_echo(
f"All files in directory {directory} decrypted -> {output}", quiet
)
except FileDecryptionException as e:
click.echo(
f"Error during decryption: Error: Failed to decrypt the file.\nDetails: {e.details}",
Expand Down
12 changes: 9 additions & 3 deletions envcloak/commands/encrypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
calculate_required_space,
list_files_to_encrypt,
validate_paths,
read_key_file, conditional_echo, conditional_secho
read_key_file,
conditional_echo,
conditional_secho,
)
from envcloak.handlers import (
handle_directory_preview,
Expand Down Expand Up @@ -108,7 +110,9 @@ def encrypt(
debug,
)
encrypt_file(input, output, key)
conditional_echo(f"File {input} encrypted -> {output} using key {key_file}", quiet)
conditional_echo(
f"File {input} encrypted -> {output} using key {key_file}", quiet
)
elif directory:
debug_log(f"Debug: Encrypting files in directory {directory}.", debug)
traverse_and_process_files(
Expand All @@ -122,7 +126,9 @@ def encrypt(
),
recursion=recursion,
)
conditional_echo(f"All files in directory {directory} encrypted -> {output}", quiet)
conditional_echo(
f"All files in directory {directory} encrypted -> {output}", quiet
)
except FileEncryptionException as e:
click.echo(
f"Error: An error occurred during file encryption.\nDetails: {e}",
Expand Down
7 changes: 6 additions & 1 deletion envcloak/commands/generate_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
import click
from envcloak.validation import check_output_not_exists, check_disk_space
from envcloak.generator import generate_key_file
from envcloak.utils import add_to_gitignore, debug_log, conditional_echo, conditional_secho
from envcloak.utils import (
add_to_gitignore,
debug_log,
conditional_echo,
conditional_secho,
)
from envcloak.decorators.common_decorators import (
debug_option,
dry_run_option,
Expand Down
7 changes: 6 additions & 1 deletion envcloak/commands/generate_key_from_password.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
import click
from envcloak.validation import check_output_not_exists, check_disk_space, validate_salt
from envcloak.generator import generate_key_from_password_file
from envcloak.utils import debug_log, add_to_gitignore, conditional_echo, conditional_secho
from envcloak.utils import (
debug_log,
add_to_gitignore,
conditional_echo,
conditional_secho,
)
from envcloak.decorators.common_decorators import (
debug_option,
dry_run_option,
Expand Down
3 changes: 2 additions & 1 deletion envcloak/commands/rotate_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ def rotate_keys(
- New key: {new_key_file} will be used to decrypt the encrypted file.
- Encrypted file: {input} will be re-encrypted to {output}.
""",
fg="cyan", quiet=quiet
fg="cyan",
quiet=quiet,
)
return
if dry_run:
Expand Down
10 changes: 7 additions & 3 deletions envcloak/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ def generate_key_file(output_path: Path, quiet: bool):
output_path.parent.mkdir(parents=True, exist_ok=True) # Ensure directory exists
with open(output_path, "wb") as key_file:
key_file.write(key)
if not quiet: print(f"Encryption key generated and saved to {output_path}")
if not quiet:
print(f"Encryption key generated and saved to {output_path}")


def generate_key_from_password_file(password: str, output_path: Path, quiet: bool, salt: str = None):
def generate_key_from_password_file(
password: str, output_path: Path, quiet: bool, salt: str = None
):
"""
Derive an encryption key from a password and save it to a file.
If no salt is provided, a random one is generated.
Expand All @@ -49,4 +52,5 @@ def generate_key_from_password_file(password: str, output_path: Path, quiet: boo
with open(output_path, "wb") as key_file:
key_file.write(key)

if not quiet: print(f"Derived encryption key saved to {output_path}")
if not quiet:
print(f"Derived encryption key saved to {output_path}")
10 changes: 7 additions & 3 deletions envcloak/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ def add_to_gitignore(directory: str, filename: str, quiet: bool):
content = gitignore_file.read()
if filename not in content:
gitignore_file.write(f"\n{filename}")
if not quiet: print(f"Added '{filename}' to {gitignore_path}")
if not quiet:
print(f"Added '{filename}' to {gitignore_path}")
else:
# Create a new .gitignore file and add the filename
with open(gitignore_path, "w", encoding="utf-8") as gitignore_file:
gitignore_file.write(f"{filename}\n")
if not quiet: print(f"Created {gitignore_path} and added '{filename}'")
if not quiet:
print(f"Created {gitignore_path} and added '{filename}'")


def calculate_required_space(input=None, directory=None):
Expand Down Expand Up @@ -144,16 +146,18 @@ def read_key_file(key_file, debug):
debug_log(f"Debug: Key file {key_file} read successfully.", debug)
return key


def conditional_echo(message, quiet, **kwargs):
"""
Echo a message only if `quiet` is False.
"""
if not quiet:
click.echo(message, **kwargs)


def conditional_secho(message, quiet, **kwargs):
"""
Styled echo a message only if `quiet` is False.
"""
if not quiet:
click.secho(message, **kwargs)
click.secho(message, **kwargs)

0 comments on commit 5358e6a

Please sign in to comment.