Skip to content

Commit c80af50

Browse files
Use enums with a default (unknown or other) instead of ValueError
Use Self
1 parent 55be55c commit c80af50

4 files changed

Lines changed: 43 additions & 17 deletions

File tree

perdoo/metadata/comic_info.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
__all__ = ["AgeRating", "ComicInfo", "Manga", "Page", "PageType", "YesNo"]
22

3+
import logging
34
from collections.abc import Callable
45
from datetime import date
56
from enum import Enum
67
from pathlib import Path
7-
from typing import Optional
88

99
from natsort import humansorted, ns
1010
from PIL import Image
@@ -14,6 +14,14 @@
1414
from perdoo.metadata._base import PascalModel
1515
from perdoo.settings import Naming
1616

17+
try:
18+
from typing import Self # Python >= 3.11
19+
except ImportError:
20+
from typing_extensions import Self # Python < 3.11
21+
22+
23+
LOGGER = logging.getLogger(__name__)
24+
1725

1826
def str_to_list(value: str | None) -> list[str]:
1927
if not value:
@@ -33,11 +41,12 @@ class YesNo(Enum):
3341
YES = "Yes"
3442

3543
@staticmethod
36-
def load(value: str) -> "YesNo":
44+
def load(value: str) -> Self:
3745
for entry in YesNo:
3846
if entry.value.replace(" ", "").casefold() == value.replace(" ", "").casefold():
3947
return entry
40-
raise ValueError(f"'{value}' isn't a valid YesNo")
48+
LOGGER.warning("'%s' isn't a valid YesNo", value)
49+
return YesNo.UNKNOWN
4150

4251
def __str__(self) -> str:
4352
return self.value
@@ -50,11 +59,12 @@ class Manga(Enum):
5059
YES_AND_RIGHT_TO_LEFT = "YesAndRightToLeft"
5160

5261
@staticmethod
53-
def load(value: str) -> "Manga":
62+
def load(value: str) -> Self:
5463
for entry in Manga:
5564
if entry.value.replace(" ", "").casefold() == value.replace(" ", "").casefold():
5665
return entry
57-
raise ValueError(f"'{value}' isn't a valid Manga")
66+
LOGGER.warning("'%s' isn't a valid Manga", value)
67+
return Manga.UNKNOWN
5868

5969
def __str__(self) -> str:
6070
return self.value
@@ -78,11 +88,12 @@ class AgeRating(Enum):
7888
X18 = "X18+"
7989

8090
@staticmethod
81-
def load(value: str) -> "AgeRating":
91+
def load(value: str) -> Self:
8292
for entry in AgeRating:
8393
if entry.value.replace(" ", "").casefold() == value.replace(" ", "").casefold():
8494
return entry
85-
raise ValueError(f"'{value}' isn't a valid AgeRating")
95+
LOGGER.warning("'%s' isn't a valid AgeRating", value)
96+
return AgeRating.UNKNOWN
8697

8798
def __str__(self) -> str:
8899
return self.value
@@ -102,11 +113,12 @@ class PageType(Enum):
102113
DELETED = "Deleted"
103114

104115
@staticmethod
105-
def load(value: str) -> "PageType":
116+
def load(value: str) -> Self:
106117
for entry in PageType:
107118
if entry.value.replace(" ", "").casefold() == value.replace(" ", "").casefold():
108119
return entry
109-
raise ValueError(f"'{value}' isn't a valid PageType")
120+
LOGGER.warning("'%s' isn't a valid PageType", value)
121+
return PageType.OTHER
110122

111123
def __str__(self) -> str:
112124
return self.value
@@ -136,7 +148,7 @@ def __hash__(self) -> int:
136148
return hash((type(self), self.image))
137149

138150
@staticmethod
139-
def from_path(file: Path, index: int, is_final_page: bool, page: Optional["Page"]) -> "Page":
151+
def from_path(file: Path, index: int, is_final_page: bool, page: Self | None) -> Self:
140152
if page:
141153
page_type = page.type
142154
elif index == 0:

perdoo/metadata/metron_info.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"Url",
1818
]
1919

20+
import logging
2021
from collections.abc import Callable
2122
from datetime import date, datetime
2223
from decimal import Decimal
@@ -29,6 +30,12 @@
2930
from perdoo.metadata._base import PascalModel
3031
from perdoo.settings import Naming
3132

33+
try:
34+
from typing import Self # Python >= 3.11
35+
except ImportError:
36+
from typing_extensions import Self # Python < 3.11
37+
38+
LOGGER = logging.getLogger(__name__)
3239
T = TypeVar("T")
3340

3441

@@ -42,11 +49,12 @@ class AgeRating(Enum):
4249
ADULT = "Adult"
4350

4451
@staticmethod
45-
def load(value: str) -> "AgeRating":
52+
def load(value: str) -> Self:
4653
for entry in AgeRating:
4754
if entry.value.replace(" ", "").casefold() == value.replace(" ", "").casefold():
4855
return entry
49-
raise ValueError(f"'{value}' isn't a valid AgeRating")
56+
LOGGER.warning("'%s' isn't a valid AgeRating", value)
57+
return AgeRating.UNKNOWN
5058

5159
def __str__(self) -> str:
5260
return self.value
@@ -134,11 +142,12 @@ class Role(Enum):
134142
OTHER = "Other"
135143

136144
@staticmethod
137-
def load(value: str) -> "Role":
145+
def load(value: str) -> Self:
138146
for entry in Role:
139147
if entry.value.replace(" ", "").casefold() == value.replace(" ", "").casefold():
140148
return entry
141-
raise ValueError(f"'{value}' isn't a valid Role")
149+
LOGGER.warning("'%s' isn't a valid Role", value)
150+
return Role.OTHER
142151

143152
def __str__(self) -> str:
144153
return self.value
@@ -182,7 +191,7 @@ class InformationSource(Enum):
182191
LEAGUE_OF_COMIC_GEEKS = "League of Comic Geeks"
183192

184193
@staticmethod
185-
def load(value: str) -> "InformationSource":
194+
def load(value: str) -> Self:
186195
for entry in InformationSource:
187196
if entry.value.replace(" ", "").casefold() == value.replace(" ", "").casefold():
188197
return entry

perdoo/settings.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
from perdoo.console import CONSOLE
2525
from perdoo.utils import blank_is_none, flatten_dict
2626

27+
try:
28+
from typing import Self # Python >= 3.11
29+
except ImportError:
30+
from typing_extensions import Self # Python < 3.11
31+
2732
try:
2833
import tomllib as tomlreader # Python >= 3.11
2934
except ModuleNotFoundError:
@@ -140,7 +145,7 @@ class Settings(SettingsModel):
140145
services: Services = Services()
141146

142147
@classmethod
143-
def load(cls) -> "Settings":
148+
def load(cls) -> Self:
144149
if not cls._file.exists():
145150
cls().save()
146151
with cls._file.open("rb") as stream:

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)