Add Transforms and Transformables

This commit is contained in:
Joscha 2020-04-22 18:25:09 +00:00
parent a051e3bcca
commit 3c808879c9
3 changed files with 46 additions and 4 deletions

View File

@ -11,16 +11,16 @@ import requests.auth
from .organizer import Organizer from .organizer import Organizer
from .tmp_dir import TmpDir from .tmp_dir import TmpDir
from .transform import Transformable
from .utils import stream_to_path from .utils import stream_to_path
@dataclass @dataclass
class HttpDownloadInfo: class HttpDownloadInfo(Transformable):
""" """
This class describes a single file to be downloaded. This class describes a single file to be downloaded.
""" """
path: Path
url: str url: str
parameters: Dict[str, Any] = field(default_factory=dict) parameters: Dict[str, Any] = field(default_factory=dict)

View File

@ -10,6 +10,7 @@ import requests
from ..organizer import Organizer from ..organizer import Organizer
from ..tmp_dir import TmpDir from ..tmp_dir import TmpDir
from ..transform import Transformable
from ..utils import soupify, stream_to_path from ..utils import soupify, stream_to_path
from .authenticators import IliasAuthenticator from .authenticators import IliasAuthenticator
@ -19,12 +20,11 @@ class ContentTypeException(Exception):
@dataclass @dataclass
class IliasDownloadInfo: class IliasDownloadInfo(Transformable):
""" """
This class describes a single file to be downloaded. This class describes a single file to be downloaded.
""" """
path: Path
url: str url: str
modification_date: datetime.datetime modification_date: datetime.datetime
# parameters: Dict[str, Any] = field(default_factory=dict) # parameters: Dict[str, Any] = field(default_factory=dict)

42
PFERD/transform.py Normal file
View File

@ -0,0 +1,42 @@
"""
Transforms let the user define functions to decide where the downloaded files
should be placed locally. They let the user do more advanced things like moving
only files whose names match a regex, or renaming files from one numbering
scheme to another.
"""
from dataclasses import dataclass
from pathlib import Path
from typing import Callable, List, Optional, TypeVar
Transform = Callable[[Path], Optional[Path]]
@dataclass
class Transformable:
"""
An object that can be transformed by a Transform.
"""
path: Path
TF = TypeVar("TF", bound=Transformable)
def apply_transform(
transform: Callable[[Path], Optional[Path]],
transformables: List[TF],
) -> List[TF]:
"""
Apply a Transform to multiple Transformables, discarding those that were
not transformed by the Transform.
"""
result: List[TF] = []
for transformable in transformables:
if new_path := transform(transformable.path):
transformable.path = new_path
result.append(transformable)
return result