diff --git a/src/hypermodern_python/__init__.py b/src/hypermodern_python/__init__.py index 21ed31b..c1948db 100644 --- a/src/hypermodern_python/__init__.py +++ b/src/hypermodern_python/__init__.py @@ -1,13 +1,13 @@ """The hypermodern Python project.""" -import argparse +# import argparse __version__ = "0.2.0" -def main(): - parser = argparse.ArgumentParser(prog="random-wikipedia-article") - parser.add_argument( - "--version", action="version", version=f"%(prog)s {__version__}" - ) - parser.parse_args() +# def main(): +# parser = argparse.ArgumentParser(prog="random-wikipedia-article") +# parser.add_argument( +# "--version", action="version", version=f"%(prog)s {__version__}" +# ) +# parser.parse_args() diff --git a/src/hypermodern_python/__pycache__/__init__.cpython-312.pyc b/src/hypermodern_python/__pycache__/__init__.cpython-312.pyc index 1a05a91..be3e445 100644 Binary files a/src/hypermodern_python/__pycache__/__init__.cpython-312.pyc and b/src/hypermodern_python/__pycache__/__init__.cpython-312.pyc differ diff --git a/src/hypermodern_python/__pycache__/console.cpython-312.pyc b/src/hypermodern_python/__pycache__/console.cpython-312.pyc new file mode 100644 index 0000000..6d478fb Binary files /dev/null and b/src/hypermodern_python/__pycache__/console.cpython-312.pyc differ diff --git a/src/hypermodern_python/__pycache__/prettier.cpython-312.pyc b/src/hypermodern_python/__pycache__/prettier.cpython-312.pyc index b9e7e3d..b35267c 100644 Binary files a/src/hypermodern_python/__pycache__/prettier.cpython-312.pyc and b/src/hypermodern_python/__pycache__/prettier.cpython-312.pyc differ diff --git a/src/hypermodern_python/__pycache__/wikipedia.cpython-312.pyc b/src/hypermodern_python/__pycache__/wikipedia.cpython-312.pyc index 94d822a..291e0fe 100644 Binary files a/src/hypermodern_python/__pycache__/wikipedia.cpython-312.pyc and b/src/hypermodern_python/__pycache__/wikipedia.cpython-312.pyc differ diff --git a/src/hypermodern_python/console.py b/src/hypermodern_python/console.py index dfacdbf..081cb74 100644 --- a/src/hypermodern_python/console.py +++ b/src/hypermodern_python/console.py @@ -4,8 +4,11 @@ from jsonargparse import CLI -from . import wikipedia +from .wikipedia import Fetcher, cli, random_page def cmd() -> None: - CLI(wikipedia.Fetcher) + try: + CLI(cli(Fetcher, random_page)) + except ValueError: + pass diff --git a/src/hypermodern_python/wikipedia.py b/src/hypermodern_python/wikipedia.py index 2f648a6..f49fd7c 100644 --- a/src/hypermodern_python/wikipedia.py +++ b/src/hypermodern_python/wikipedia.py @@ -7,7 +7,7 @@ import textwrap import time from dataclasses import dataclass, field -from typing import TYPE_CHECKING, TypeAlias +from typing import TYPE_CHECKING, Any, TypeAlias if TYPE_CHECKING: from collections.abc import Awaitable @@ -15,7 +15,8 @@ import httpx from cattrs.preconf.orjson import make_converter from loguru import logger -from prettier import cprint + +from .prettier import cprint if sys.version_info >= (3, 8): from importlib.metadata import metadata @@ -27,6 +28,7 @@ else: from typing import Iterable + API_URL: str = ( "https://{language}.wikipedia.org/api/rest_v1/page/random/summary" ) @@ -58,7 +60,7 @@ class Page: class Fetcher: language: str = "en" url: str | None = None - timeout: float | None = None + timeout: float | None = 100 headers: dict | None = None # These two are marked 'init=False' so they do not show up in the constructor # noqa: E501 # logic because the user doesn't need the ability to initialize these values since # noqa: E501 @@ -80,9 +82,7 @@ def __post_init__(self) -> None: self.client.headers = self.headers self.client.http2 = True - async def fetch( - self, func: Awaitable[[httpx.AsyncClient, str], Page] - ) -> None: + async def fetch(self, func: Awaitable[[Any, Any], Page]) -> None: async with self.client as client: tasks = [ asyncio.ensure_future(func(client, self.url)) @@ -135,7 +135,11 @@ async def random_page(client: httpx.AsyncClient, url: str) -> Page: raise error -headers = {"User-Agent": build_user_agent()} - -fetch_wikipedia = Fetcher(timeout=100, headers=headers) -asyncio.run(fetch_wikipedia.fetch(random_page)) +def cli( + fetch_service: Fetcher, + func: Awaitable[[httpx.AsyncClient, str], Page], + timeout: int = 100, + headers: dict = {"User-Agent": build_user_agent()}, +) -> None: + fetch_wikipedia = fetch_service(timeout=timeout, headers=headers) + asyncio.run(fetch_wikipedia.fetch(func))