Skip to content

Commit

Permalink
refactor: Move models into package (#214)
Browse files Browse the repository at this point in the history
[no-bump]
  • Loading branch information
janw authored Dec 7, 2024
1 parent 6f8c2ab commit 1ac699d
Show file tree
Hide file tree
Showing 22 changed files with 383 additions and 365 deletions.
4 changes: 1 addition & 3 deletions podcast_archiver/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,12 +304,10 @@ def main(ctx: click.RichContext, /, **kwargs: Any) -> int:
pa.run()
except InvalidSettings as exc:
raise click.BadParameter(f"Invalid settings: {exc}") from exc
except KeyboardInterrupt as exc: # pragma: no cover
raise click.Abort("Interrupted by user") from exc
except FileNotFoundError as exc:
raise click.Abort(exc) from exc
return 0


if __name__ == "__main__":
if __name__ == "__main__": # pragma: no cover
main.main(prog_name=constants.PROG_NAME)
4 changes: 2 additions & 2 deletions podcast_archiver/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from podcast_archiver.database import BaseDatabase, Database, DummyDatabase
from podcast_archiver.exceptions import InvalidSettings
from podcast_archiver.logging import rprint
from podcast_archiver.models import ALL_FIELD_TITLES_STR
from podcast_archiver.utils import get_field_titles

if TYPE_CHECKING:
from pydantic.fields import FieldInfo
Expand Down Expand Up @@ -100,7 +100,7 @@ class Settings(BaseModel):
default=constants.DEFAULT_FILENAME_TEMPLATE,
description=(
"Template to be used when generating filenames. Available template variables are: "
f"{ALL_FIELD_TITLES_STR}, and 'ext' (the filename extension)"
f"{get_field_titles()}, and 'ext' (the filename extension)"
),
)

Expand Down
14 changes: 7 additions & 7 deletions podcast_archiver/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from podcast_archiver.logging import logger

if TYPE_CHECKING:
from podcast_archiver.models import EpisodeSkeleton
from podcast_archiver.models.episode import BaseEpisode


def adapt_datetime_iso(val: datetime) -> str:
Expand All @@ -34,19 +34,19 @@ class EpisodeInDb:

class BaseDatabase:
@abstractmethod
def add(self, episode: EpisodeSkeleton) -> None:
def add(self, episode: BaseEpisode) -> None:
pass # pragma: no cover

@abstractmethod
def exists(self, episode: EpisodeSkeleton) -> EpisodeInDb | None:
def exists(self, episode: BaseEpisode) -> EpisodeInDb | None:
pass # pragma: no cover


class DummyDatabase(BaseDatabase):
def add(self, episode: EpisodeSkeleton) -> None:
def add(self, episode: BaseEpisode) -> None:
pass

def exists(self, episode: EpisodeSkeleton) -> EpisodeInDb | None:
def exists(self, episode: BaseEpisode) -> EpisodeInDb | None:
return None


Expand Down Expand Up @@ -102,7 +102,7 @@ def _has_column(self, conn: sqlite3.Connection, name: str) -> bool:
)
return bool(result.fetchone()[0])

def add(self, episode: EpisodeSkeleton) -> None:
def add(self, episode: BaseEpisode) -> None:
with self.get_conn() as conn:
try:
conn.execute(
Expand All @@ -117,7 +117,7 @@ def add(self, episode: EpisodeSkeleton) -> None:
except sqlite3.DatabaseError as exc:
logger.debug("Error adding %s to db", episode, exc_info=exc)

def exists(self, episode: EpisodeSkeleton) -> EpisodeInDb | None:
def exists(self, episode: BaseEpisode) -> EpisodeInDb | None:
if self.ignore_existing:
return None
with self.get_conn() as conn:
Expand Down
33 changes: 9 additions & 24 deletions podcast_archiver/download.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

from contextlib import contextmanager
from dataclasses import dataclass, field
from threading import Event
from typing import IO, TYPE_CHECKING, Generator

Expand All @@ -17,32 +18,16 @@

from requests import Response

from podcast_archiver.models import EpisodeSkeleton, FeedInfo
from podcast_archiver.models.episode import BaseEpisode


@dataclass(slots=True)
class DownloadJob:
episode: EpisodeSkeleton
feed_info: FeedInfo
episode: BaseEpisode
target: Path
stop_event: Event

_max_download_bytes: int | None = None
_write_info_json: bool

def __init__(
self,
episode: EpisodeSkeleton,
*,
target: Path,
max_download_bytes: int | None = None,
write_info_json: bool = False,
stop_event: Event | None = None,
) -> None:
self.episode = episode
self.target = target
self._max_download_bytes = max_download_bytes
self._write_info_json = write_info_json
self.stop_event = stop_event or Event()
add_info_json: bool = False
stop_event: Event = field(default_factory=Event)
max_download_bytes: int | None = None

def __call__(self) -> EpisodeResult:
try:
Expand Down Expand Up @@ -71,7 +56,7 @@ def infojsonfile(self) -> Path:
def receive_data(self, fp: IO[bytes], response: Response) -> None:
total_size = int(response.headers.get("content-length", "0"))
total_written = 0
max_bytes = self._max_download_bytes
max_bytes = self.max_download_bytes
for chunk in wrapped_tqdm(
response.iter_content(chunk_size=constants.DOWNLOAD_CHUNK_SIZE),
desc=str(self.episode),
Expand All @@ -90,7 +75,7 @@ def receive_data(self, fp: IO[bytes], response: Response) -> None:

@contextmanager
def write_info_json(self) -> Generator[None, None, None]:
if not self._write_info_json:
if not self.add_info_json:
yield
return
with atomic_write(self.infojsonfile) as fp:
Expand Down
2 changes: 1 addition & 1 deletion podcast_archiver/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
if TYPE_CHECKING:
import pydantic_core

from podcast_archiver.models import FeedInfo
from podcast_archiver.models.feed import FeedInfo


class PodcastArchiverException(Exception):
Expand Down
Loading

0 comments on commit 1ac699d

Please sign in to comment.