Skip to content

comicbox.readinglists.cli.online ¶

The online actions: search Metron or the catalog, and fetch a list.

FetchSpec dataclass ¶

What fetch was asked for: a source kind and its id, path or URL.

Source code in comicbox/readinglists/cli/online.py
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
@dataclass(frozen=True, slots=True)
class FetchSpec:
    """What ``fetch`` was asked for: a source kind and its id, path or URL."""

    kind: str
    target: str

    @override
    def __str__(self) -> str:
        """Spell it as it was given, but a URL's user name and password."""
        if self.kind == _URL_KIND:
            from comicbox.readinglists.sources.url import without_userinfo

            return without_userinfo(self.target)
        return f"{self.kind}:{self.target}"

__str__() ¶

Spell it as it was given, but a URL’s user name and password.

Source code in comicbox/readinglists/cli/online.py
73
74
75
76
77
78
79
80
@override
def __str__(self) -> str:
    """Spell it as it was given, but a URL's user name and password."""
    if self.kind == _URL_KIND:
        from comicbox.readinglists.sources.url import without_userinfo

        return without_userinfo(self.target)
    return f"{self.kind}:{self.target}"

add_online_actions(actions) ¶

Add the online actions to the parser’s subcommands.

Source code in comicbox/readinglists/cli/online.py
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
def add_online_actions(actions: Any) -> None:
    """Add the online actions to the parser's subcommands."""
    sub = actions.add_parser(
        "search",
        help="Search Metron's reading lists or the DieselTech catalog.",
        description=(
            "Search Metron's reading lists by name (needs a Metron API token),"
            " or the paths of the DieselTech catalog on GitHub."
        ),
    )
    sub.add_argument(
        "where", choices=_SEARCH_WHERE, metavar="metron|catalog", help="Where to look."
    )
    sub.add_argument(
        "query", nargs="+", metavar="QUERY", help="Words the name or path holds."
    )
    _add_online_options(sub)
    sub = actions.add_parser(
        "fetch",
        help="Fetch a list from Metron, Comic Vine, the catalog or a URL.",
        description=(
            "Fetch metron:ID (a Metron reading list), metron-arc:ID (a Metron"
            " story arc), comicvine-arc:ID (a Comic Vine story arc; 4045-ID,"
            " as its URLs spell it, works too),"
            " catalog:PATH (a DieselTech list) or an http(s) URL, and write"
            " it, to stdout without -o. Metron needs an API token and Comic"
            " Vine an API key."
        ),
    )
    add_write_options(sub, "FILE", "Write here, not to stdout.")
    sub.add_argument(
        "spec",
        type=fetch_spec,
        metavar="WHAT",
        help=f"{_WHAT}.",
    )
    _add_online_options(sub)

fetch_spec(text) ¶

Parse a database’s KIND:ID, catalog:PATH or a URL.

Source code in comicbox/readinglists/cli/online.py
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
def fetch_spec(text: str) -> FetchSpec:
    """Parse a database's ``KIND:ID``, ``catalog:PATH`` or a URL."""
    if urlsplit(text).scheme.lower() in _URL_SCHEMES:
        return FetchSpec(_URL_KIND, text)
    kind, colon, target = text.partition(":")
    kind = kind.lower()
    if kind == _COMICVINE_ARC_KIND:
        target = _comicvine_arc_id(target)
    # ASCII digits only: "²" is a digit to isdigit and not to int.
    if colon and kind in _ID_KINDS and target.isascii() and target.isdigit():
        return FetchSpec(kind, target)
    if colon and kind == _CATALOG_KIND and target.strip():
        return FetchSpec(kind, target)
    reason = f"{text!r}: give {_WHAT}"
    raise ArgumentTypeError(reason)

open_session(run) ¶

Open a session on the command’s settings, reporting progress.

Source code in comicbox/readinglists/cli/online.py
149
150
151
152
153
154
155
def open_session(run: Run) -> ReadingListSession:
    """Open a session on the command's settings, reporting progress."""
    from comicbox.version import set_user_agent_context

    set_user_agent_context("cli", client="readinglist")
    progress = _Progress(run.err, quiet=run.args.quiet > 0)
    return ReadingListSession(config=_settings(run.args), on_event=progress)