Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Oleksandr Kulkov <[email protected]>
  • Loading branch information
akahles and adamant-pwn authored Nov 19, 2024
1 parent 5f87604 commit c2451cb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 32 deletions.
4 changes: 2 additions & 2 deletions archiver/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.*)')
40 changes: 10 additions & 30 deletions archiver/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)]

Expand All @@ -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]

Expand Down

0 comments on commit c2451cb

Please sign in to comment.