Skip to content

Commit

Permalink
Refactored conversion.py & converters.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Casvt committed Dec 10, 2024
1 parent e38d2df commit 43a24ec
Show file tree
Hide file tree
Showing 8 changed files with 329 additions and 369 deletions.
24 changes: 24 additions & 0 deletions backend/base/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ class Constants:
LOGGER_FILENAME = "Kapowarr.log"

ARCHIVE_EXTRACT_FOLDER = '.archive_extract'
ZIP_MIN_MOD_TIME = 315619200
RAR_EXECUTABLES = {
'linux': 'rar_linux_64',
'darwin': 'rar_bsd_64',
'win32': 'rar_windows_64.exe'
}

DEFAULT_USERAGENT = "Kapowarr"
TOTAL_RETRIES = 5
Expand Down Expand Up @@ -486,3 +492,21 @@ class DBMigrator(ABC):
@abstractmethod
def run(self) -> None:
...


class FileConverter(ABC):
source_format: str
target_format: str

@staticmethod
@abstractmethod
def convert(file: str) -> List[str]:
"""Convert a file from source_format to target_format.
Args:
file (str): Filepath to the source file, should be in source_format.
Returns:
List[str]: The resulting files or directories, in target_format.
"""
...
31 changes: 29 additions & 2 deletions backend/base/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
from collections import deque
from os import listdir, makedirs, remove, scandir
from os.path import (abspath, basename, commonpath, dirname, isdir,
isfile, join, relpath, samefile, splitext)
isfile, join, relpath, samefile, sep, splitext)
from re import compile
from shutil import copytree, move, rmtree
from typing import Deque, Dict, Iterable, List, Sequence, Set

from backend.base.definitions import CharConstants
from backend.base.definitions import CharConstants, Constants
from backend.base.helpers import check_filter, force_suffix
from backend.base.logging import LOGGER

Expand Down Expand Up @@ -196,6 +196,33 @@ def propose_basefolder_change(
return file_changes


def generate_archive_folder(
volume_folder: str,
archive_file: str
) -> str:
"""Generate a folder in which the given archive file can be extracted.
Args:
volume_folder (str): The volume folder that the archive file is in.
archive_file (str): The filepath of the archive file itself.
Returns:
str: The folder in which the archive file can be extracted.
"""
return join(
volume_folder,
Constants.ARCHIVE_EXTRACT_FOLDER,
splitext(
'_'.join(
relpath(
archive_file,
volume_folder
).split(sep)
)
)[0]
)


# region Creation
def create_folder(folder: str) -> None:
"""Create a folder
Expand Down
24 changes: 12 additions & 12 deletions backend/features/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,15 @@ def __init__(
self,
volume_id: int,
issue_id: int,
filepath_filter: Union[List[str], None] = []
filepath_filter: List[str] = []
) -> None:
"""Create the task
Args:
volume_id (int): The ID of the volume for which to perform the task.
issue_id (int): The ID of the issue for which to perform the task.
filepath_filter (Union[List[str], None], optional): Only rename
files in this list.
filepath_filter (List[str], optional): Only rename files in this
list.
Defaults to [].
"""
self._volume_id = volume_id
Expand Down Expand Up @@ -184,15 +184,15 @@ def __init__(
self,
volume_id: int,
issue_id: int,
filepath_filter: Union[List[str], None] = []
filepath_filter: List[str] = []
) -> None:
"""Create the task
Args:
volume_id (int): The ID of the volume for which to perform the task.
issue_id (int): The ID of the issue for which to perform the task.
filepath_filter (Union[List[str], None], optional): Only rename
files in this list.
filepath_filter (List[str], optional): Only rename files in this
list.
Defaults to [].
"""
self._volume_id = volume_id
Expand Down Expand Up @@ -320,14 +320,14 @@ def issue_id(self) -> None:
def __init__(
self,
volume_id: int,
filepath_filter: Union[List[str], None] = []
filepath_filter: List[str] = []
) -> None:
"""Create the task
Args:
volume_id (int): The ID of the volume for which to perform the task.
filepath_filter (Union[List[str], None], optional): Only rename
files in this list.
filepath_filter (List[str], optional): Only rename files in this
list.
Defaults to [].
"""
self._volume_id = volume_id
Expand Down Expand Up @@ -368,14 +368,14 @@ def issue_id(self) -> None:
def __init__(
self,
volume_id: int,
filepath_filter: Union[List[str], None] = []
filepath_filter: List[str] = []
) -> None:
"""Create the task
Args:
volume_id (int): The ID of the volume for which to perform the task.
filepath_filter (Union[List[str], None], optional): Only convert
files in this list.
filepath_filter (List[str], optional): Only convert files in this
list.
Defaults to [].
"""
self._volume_id = volume_id
Expand Down
Loading

0 comments on commit 43a24ec

Please sign in to comment.