From c2451cb0462845599bcdd2ea92ffe7b2035f49fe Mon Sep 17 00:00:00 2001 From: Andre Kahles Date: Tue, 19 Nov 2024 11:17:12 +0100 Subject: [PATCH] Apply suggestions from code review Co-authored-by: Oleksandr Kulkov --- archiver/constants.py | 4 ++-- archiver/helpers.py | 40 ++++++++++------------------------------ 2 files changed, 12 insertions(+), 32 deletions(-) diff --git a/archiver/constants.py b/archiver/constants.py index 33a459f..728c7b5 100644 --- a/archiver/constants.py +++ b/archiver/constants.py @@ -12,7 +12,7 @@ ENCRYPTION_ALGORITHM = "AES256" ENV_VAR_MAPPER_MAX_CPUS = "ARCHIVER_MAX_CPUS_ENV_VAR" DEFAULT_COMPRESSION_LEVEL = 6 -ALLOWED_SUFFIXES = ['.part[0-9]+', '.tar', '.md5', '.lz', '.gpg', '.lst', '.parts', '.txt'] -ALLOWED_SUFFIXES_REG = '(' + ')|('.join(ALLOWED_SUFFIXES) + ')' +ARCHIVE_SUFFIXES = ['\.part[0-9]+', '\.tar', '\.md5', '\.lz', '\.gpg', '\.lst', '\.parts', '\.txt'] +ARCHIVE_SUFFIXES_REG = '$|'.join(ARCHIVE_SUFFIXES) + '$' MD5_LINE_REGEX = re.compile(r'(\S+)\s+(\S.*)') diff --git a/archiver/helpers.py b/archiver/helpers.py index 3e37c2b..103d477 100644 --- a/archiver/helpers.py +++ b/archiver/helpers.py @@ -12,7 +12,7 @@ from .constants import READ_CHUNK_BYTE_SIZE, COMPRESSED_ARCHIVE_SUFFIX, \ ENCRYPTED_ARCHIVE_SUFFIX, ENV_VAR_MAPPER_MAX_CPUS, MD5_LINE_REGEX, \ - ALLOWED_SUFFIXES_REG + ARCHIVE_SUFFIXES_REG def get_files_with_type_in_directory_or_terminate(directory, file_type): @@ -339,36 +339,16 @@ def file_is_valid_archive_or_terminate(file_path): terminate_with_message(f"File {file_path.as_posix()} is not a valid archive of type {COMPRESSED_ARCHIVE_SUFFIX} or {ENCRYPTED_ARCHIVE_SUFFIX} or doesn't exist.") -def filename_without_extensions(path): - """Removes every allowed suffix, including .partX""" - suffixes = path.suffixes - if len(suffixes) > 0: - allowed_suffixes = [] - for s in suffixes[::-1]: - if re.match(ALLOWED_SUFFIXES_REG, s.lower()): - allowed_suffixes.append(s) - else: - break - suffixes = allowed_suffixes[::-1] - - suffixes_string = "".join(suffixes) - - return path.name[:-len(suffixes_string)] - +def filename_without_archive_extensions(path): + """Removes every archiving suffix""" + return filepath_without_archive_extensions(path).name -def filepath_without_extensions(path:Path) -> Path: - """Removes every suffix, including .partX""" - suffixes = path.suffixes - if len(suffixes) > 0: - allowed_suffixes = [] - for s in suffixes[::-1]: - if re.match(ALLOWED_SUFFIXES_REG, s.lower()): - allowed_suffixes.append(s) - else: - break - suffixes = allowed_suffixes[::-1] - suffixes_string = "".join(suffixes) +def filepath_without_archive_extensions(path:Path) -> Path: + """Removes every archiving suffix""" + while re.match(ARCHIVE_SUFFIXES_REG, path.suffix): + path = path.with_suffix('') + return path return path.parent / path.name[:-len(suffixes_string)] @@ -383,7 +363,7 @@ def infer_source_name(source_path: Path) -> Path: if len(unique_names) == 0: terminate_with_message('There are no archive files present') elif len(unique_names) > 1: - terminate_with_message(f'Automatic archive name detection has failed. More than one possible archive name detected: {str(unique_names)}\n optionally use --archive_name to specific archive name.') + terminate_with_message(f'Automatic archive name detection has failed. More than one possible archive name detected: {str(unique_names)}\noptionally use --archive_name to specify archive name.') return unique_names[0]