Skip to content
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 45 additions & 31 deletions pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion peat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,9 @@ def emit(self, record: logging.LogRecord) -> None:
from .api.push_api import push
from .api.heat_api import heat_main
from .api.config_builder_api import generate_simple_config, generate_full_config
from .api.crypto_api import encrypt, decrypt
from .api.crypto_api import (
encrypt_config_api,
decrypt_config_api,
encrypt_results_api,
decrypt_results_api,
)
68 changes: 65 additions & 3 deletions peat/api/crypto_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import yaml

from peat import config_crypto, log
from peat import config_crypto, log, results_crypto


def encrypt(config_path: str, user_password: str | None = None) -> bool:
def encrypt_config_api(config_path: str, user_password: str | None = None) -> bool:
"""
PEAT CLI functionality to encrypt a file

Expand All @@ -14,6 +14,10 @@ def encrypt(config_path: str, user_password: str | None = None) -> bool:
user_password: (Optional) password for encryption specified by CLI, defaults to None.
If none is given by CLI command, user will be asked to input one
"""
if config_path is None:
log.error("No config specified")
return False

fp = Path(config_path)
result = config_crypto.encrypt_config(fp, user_password)
if result:
Expand All @@ -23,7 +27,7 @@ def encrypt(config_path: str, user_password: str | None = None) -> bool:
return False


def decrypt(
def decrypt_config_api(
config_path: str,
output_path: str | None = None,
new_filename: str | None = "decrypted_config.yaml",
Expand All @@ -41,6 +45,10 @@ def decrypt(
user_password: (Optional) password for encryption specified by CLI, defaults to None.
If none is given by CLI command, user will be asked to input one
"""
if config_path is None:
log.error("No config specified")
return False

fp = Path(config_path)
decrypted_str = config_crypto.decrypt_config(fp, user_password=user_password)
if not decrypted_str:
Expand All @@ -63,3 +71,57 @@ def decrypt(
yaml.dump(yaml_data, file, default_flow_style=False, sort_keys=False)
log.info(f"Encrypted config saved to current directory as {new_filename}")
return True


def encrypt_results_api(
results_dir_path: str, write_path: str | None = None, user_password: str | None = None
) -> bool:
"""
API for CLI -> Encrypt results function

Args:
results_dir_path: PEAT results directory
write_path: Path to write encrypted archive
user_password: password to encrypt archive with
Returns:
bool
"""
if results_dir_path is None:
log.error("No directory specified")
return False

results_dir_path = Path(results_dir_path)

if write_path is None:
write_path = Path("./")
else:
write_path = Path(write_path)

return results_crypto.zip_encrypt_results(results_dir_path, write_path, user_password)


def decrypt_results_api(
encrypted_dir_path: str, write_path: str | None = None, user_password: str | None = None
) -> bool:
"""
API for CLI -> Decrypt archive function

Args:
encrypted_dir_path: Encrypted zip archive path
write_path: Path to write extracted PEAT results
user_password: password to decrypt archive with
Returns:
bool
"""
if encrypted_dir_path is None:
log.error("No archive specified")
return False

encrypted_dir_path = Path(encrypted_dir_path)

if write_path is None:
write_path = Path("./")
else:
write_path = Path(write_path)

return results_crypto.unzip_decrypt_results(encrypted_dir_path, write_path, user_password)
Loading
Loading