Skip to content

Commit

Permalink
chore: misc fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ravencentric committed Oct 11, 2024
1 parent 907a876 commit 878100e
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 15 deletions.
19 changes: 10 additions & 9 deletions src/nzb/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from nzb._exceptions import InvalidNZBError
from nzb._models import NZB
from nzb._parser import parse_doctype, parse_files, parse_metadata
from nzb._utils import meta_constructor
from nzb._utils import meta_constructor, realpath

if TYPE_CHECKING:
from collections.abc import Iterable
Expand Down Expand Up @@ -77,7 +77,7 @@ def from_file(cls, nzb: StrPath, *, encoding: str | None = "utf-8") -> Self:
NZBParser
An NZBParser instance initialized with the content of the specified NZB file.
"""
nzb = Path(nzb).expanduser().resolve().read_text(encoding=encoding)
nzb = realpath(nzb).read_text(encoding=encoding)
return cls(nzb, encoding=encoding)


Expand Down Expand Up @@ -280,18 +280,18 @@ def save(self, filename: StrPath | None = None, *, overwrite: bool = False) -> P
If the file exists and overwrite is `False`.
"""

filename_from_self: Path | None = getattr(self, "__nzb_file", None)
self_filename: Path | None = getattr(self, "__nzb_file", None)

if filename is None:
if filename_from_self is None:
if self_filename is None:
raise FileNotFoundError("No filename specified!")
else:
if overwrite:
outfile = filename_from_self
outfile = self_filename
else:
raise FileExistsError(filename_from_self)
raise FileExistsError(self_filename)
else:
outfile = Path(filename).resolve()
outfile = realpath(filename)

outfile.parent.mkdir(parents=True, exist_ok=True)
unparsed = xmltodict_unparse(self.__nzbdict, encoding=self.__encoding, pretty=True, indent=" ")
Expand Down Expand Up @@ -324,7 +324,8 @@ def from_file(cls, nzb: StrPath, encoding: str = "utf-8") -> Self:
Self
Returns itself.
"""
data = Path(nzb).expanduser().resolve().read_text(encoding=encoding)
__nzb_file = realpath(nzb)
data = __nzb_file.read_text(encoding=encoding)
instance = cls(data, encoding=encoding)
setattr(instance, "__nzb_file", nzb)
setattr(instance, "__nzb_file", __nzb_file)
return instance
2 changes: 1 addition & 1 deletion src/nzb/_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def __init__(self, message: str) -> None:
super().__init__(message)

def __str__(self) -> str:
"""Equivalent to accessing the .member attribute"""
"""Equivalent to accessing the .message attribute"""
return self.message

def __repr__(self) -> str:
Expand Down
8 changes: 4 additions & 4 deletions src/nzb/_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

def parse_metadata(nzb: dict[str, Any]) -> Meta:
"""
Parses the <meta>...</meta> field present in an NZB.
Parses the `<meta>...</meta>` field present in an NZB.
```xml
<?xml version="1.0" encoding="iso-8859-1" ?>
Expand Down Expand Up @@ -90,7 +90,7 @@ def parse_metadata(nzb: dict[str, Any]) -> Meta:

def parse_segments(segmentdict: dict[str, list[dict[str, str]] | dict[str, str] | None] | None) -> tuple[Segment, ...]:
"""
Parses the <segments>...</segments> field present in an NZB.
Parses the `<segments>...</segments>` field present in an NZB.
There's 3 possible things that we can get here:
- A list of dictionaries if there's more than 1 segment field present
Expand Down Expand Up @@ -140,7 +140,7 @@ def parse_segments(segmentdict: dict[str, list[dict[str, str]] | dict[str, str]

def parse_files(nzb: dict[str, Any]) -> tuple[File, ...]:
"""
Parses the <file>...</file> field present in an NZB.
Parses the `<file>...</file>` field present in an NZB.
```xml
<?xml version="1.0" encoding="iso-8859-1" ?>
Expand All @@ -158,7 +158,7 @@ def parse_files(nzb: dict[str, Any]) -> tuple[File, ...]:
# There's 3 possible things that we can get from the above here:
# - A list of dictionaries if there's more than 1 file field present, i.e, list[dict[str, str]]
# - A dictionary if there's only one file field present, i.e, dict[str, str]
# - None if there's no meta field
# - None if there's no file field

# Here's the type representation of the three possible cases that we need to handle
FileFieldType: TypeAlias = Union[list[dict[str, str]], dict[str, str], None]
Expand Down
10 changes: 10 additions & 0 deletions src/nzb/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,25 @@

if TYPE_CHECKING:
from collections.abc import Iterable
from pathlib import Path
from typing import Callable, ParamSpec, TypeVar

from nzb._types import StrPath

T = TypeVar("T")
P = ParamSpec("P")

def cache(user_function: Callable[P, T], /) -> Callable[P, T]: # type: ignore
return user_function


def realpath(path: StrPath, /) -> Path:
"""
Canonicalize a given path.
"""
return Path(path).expanduser().resolve()


def meta_constructor(
title: str | None = None,
passwords: Iterable[str] | str | None = None,
Expand Down
2 changes: 1 addition & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 878100e

Please sign in to comment.