Skip to content

Commit

Permalink
create cli
Browse files Browse the repository at this point in the history
  • Loading branch information
duksosleepy committed Jul 31, 2024
1 parent 938be7c commit b892a61
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 19 deletions.
14 changes: 7 additions & 7 deletions src/hypermodern_python/__init__.py
Original file line number Diff line number Diff line change
@@ -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()
Binary file modified src/hypermodern_python/__pycache__/__init__.cpython-312.pyc
Binary file not shown.
Binary file not shown.
Binary file modified src/hypermodern_python/__pycache__/prettier.cpython-312.pyc
Binary file not shown.
Binary file modified src/hypermodern_python/__pycache__/wikipedia.cpython-312.pyc
Binary file not shown.
7 changes: 5 additions & 2 deletions src/hypermodern_python/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 14 additions & 10 deletions src/hypermodern_python/wikipedia.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@
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

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
Expand All @@ -27,6 +28,7 @@
else:
from typing import Iterable


API_URL: str = (
"https://{language}.wikipedia.org/api/rest_v1/page/random/summary"
)
Expand Down Expand Up @@ -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
Expand All @@ -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))
Expand Down Expand Up @@ -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))

0 comments on commit b892a61

Please sign in to comment.