diff --git a/anisearch/AniSearch.py b/anisearch/AniSearch.py index a0e4e83..2b6c223 100644 --- a/anisearch/AniSearch.py +++ b/anisearch/AniSearch.py @@ -48,11 +48,8 @@ def set_timefmt(self, timefmt: str) -> None: Args: - timefmt: Time format string """ - try: - time.strftime(timefmt, time.localtime()) - self._timefmt = timefmt - except ValueError: - raise ValueError(f"Invalid time format: {timefmt}") + time.strftime(timefmt, time.localtime()) + self._timefmt = timefmt def reset(self) -> None: """Reset the search object.""" @@ -113,11 +110,7 @@ def size_format(self, unit: str = 'MB') -> None: if not self.if_selected: raise ValueError("No item selected. Please use select() method first.") - try: - self.anime.size_format(unit) - except Exception as e: - log.error(f"Size format conversion failed: {str(e)}") - raise + self.anime.size_format(unit) def save_csv(self, filename: str) -> None: """ @@ -145,6 +138,7 @@ def save_csv(self, filename: str) -> None: log.error(f"Failed to save CSV: {str(e)}") raise + if __name__ == "__main__": import doctest diff --git a/anisearch/__init__.py b/anisearch/__init__.py index dbc2537..74c2cd1 100644 --- a/anisearch/__init__.py +++ b/anisearch/__init__.py @@ -1,3 +1,4 @@ +import importlib import logging LOG_FORMAT = "%(asctime)s %(levelname)s %(message)s" @@ -26,4 +27,4 @@ def setup_logger(name: str = "global", level: int = logging.DEBUG) -> logging.Lo log = setup_logger() -eval("from .AniSearch import AniSearch") +AniSearch = importlib.import_module('.AniSearch', package=__name__).AniSearch diff --git a/anisearch/plugins/Anime.py b/anisearch/plugins/Anime.py index 74e4b11..c87f063 100644 --- a/anisearch/plugins/Anime.py +++ b/anisearch/plugins/Anime.py @@ -20,7 +20,6 @@ 'TIB': 1099511627776 } - class Anime: def __init__(self, time: str, title: str, size: str, magnet: str): """ @@ -43,18 +42,12 @@ def size_format(self, unit: str = 'MB') -> None: Args: unit (str, optional): The target unit. Defaults to 'MB'. - - Raises: - ValueError: If the size string is invalid or an invalid storage unit is provided. """ - try: - value, pre_unit = self.extract_value_and_unit(self.size) + value, pre_unit = self.extract_value_and_unit(self.size) + if pre_unit is not None and value is not None: if pre_unit.upper() != unit.upper(): value = self.convert_byte(value, pre_unit, unit) self.size = f"{value}{unit}" - except ValueError as e: - log.critical(f"Size format error: {e}") - raise @staticmethod def convert_byte(value: float, from_unit: str, to_unit: str) -> float: @@ -68,18 +61,15 @@ def convert_byte(value: float, from_unit: str, to_unit: str) -> float: Returns: float: The converted value. - - Raises: - ValueError: If an invalid storage unit is provided. """ try: from_factor = conversion_factors[from_unit.upper()] to_factor = conversion_factors[to_unit.upper()] + return round(value * (from_factor / to_factor), 2) except KeyError as e: invalid_unit = e.args[0] if e.args else 'unknown' - raise ValueError(f"Convert: invalid storage unit '{invalid_unit}'") from e - - return round(value * (from_factor / to_factor), 2) + log.error(f"Convert: invalid storage unit '{invalid_unit}'") + return None @staticmethod def extract_value_and_unit(size: str) -> Tuple[float, str]: @@ -91,9 +81,6 @@ def extract_value_and_unit(size: str) -> Tuple[float, str]: Returns: Tuple[float, str]: The extracted value and unit. - - Raises: - ValueError: If the size string is invalid. """ match = size_pattern.match(size) @@ -102,7 +89,8 @@ def extract_value_and_unit(size: str) -> Tuple[float, str]: unit = match.group(2) return value, unit else: - raise ValueError(f"Extract: invalid size '{size}'") + log.error(f"Extract: invalid size '{size}'") + return None, None def __eq__(self, value: object) -> bool: """ @@ -118,9 +106,12 @@ def __eq__(self, value: object) -> bool: return False try: - return magnet_hash_pattern.search(self.magnet).group(1).lower() == magnet_hash_pattern.search(value.magnet).group(1).lower() + return ( + magnet_hash_pattern.search(self.magnet).group(1).lower() == + magnet_hash_pattern.search(value.magnet).group(1).lower() + ) except AttributeError: - log.critical("Magnet hash extraction failed.") + log.error("Magnet hash extraction failed.") return False def __ne__(self, other) -> bool: @@ -145,9 +136,9 @@ def __str__(self) -> str: try: hash_value = magnet_hash_pattern.search(self.magnet).group(1) except AttributeError: - log.critical("Magnet hash extraction failed.") + log.error("Magnet hash extraction failed.") hash_value = "unknown" - return f"Anime'{self.title}' with hash {hash_value}" + return f"Anime '{self.title}' with hash {hash_value}" def __repr__(self) -> str: """ diff --git a/setup.py b/setup.py index b0242d4..6080755 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ setup( name='Anisearch-lib', - version='1.3.3', + version='1.3.4', packages=find_packages(exclude=['tests*']), install_requires=requirements, entry_points={