]> git.scottworley.com Git - paperdoorknob/blame_incremental - args.py
FakeFetcher for faster tests
[paperdoorknob] / args.py
... / ...
CommitLineData
1# paperdoorknob: Print glowfic
2#
3# This program is free software: you can redistribute it and/or modify it
4# under the terms of the GNU General Public License as published by the
5# Free Software Foundation, version 3.
6
7
8from argparse import ArgumentParser
9from contextlib import contextmanager
10import os.path
11
12from typing import Iterator
13
14from xdg_base_dirs import xdg_cache_home
15
16from fetch import CachingFetcher
17from spec import Spec
18from texify import PandocTexifier
19
20
21def _command_line_parser() -> ArgumentParser:
22 parser = ArgumentParser(prog='paperdoorknob', description='Print glowfic')
23 parser.add_argument(
24 '--cache_path',
25 metavar='PATH',
26 help='Where to keep the http cache (instead of %(default)s)',
27 default=os.path.join(xdg_cache_home(), "paperdoorknob"))
28 parser.add_argument(
29 '--out',
30 help='The filename stem at which to write output ' +
31 '(eg: "%(default)s" produces %(default)s.tex, %(default)s.pdf, etc.)',
32 default='book')
33 parser.add_argument('--pandoc', help='Location of the pandoc executable')
34 parser.add_argument(
35 '--timeout',
36 help='How long to wait for HTTP requests, in seconds',
37 default=30)
38 parser.add_argument('url', help='URL to retrieve')
39 return parser
40
41
42@contextmanager
43def spec_from_commandline_args() -> Iterator[Spec]:
44 args = _command_line_parser().parse_args()
45 with CachingFetcher(args.cache_path, args.timeout) as fetcher:
46 with open(args.out + '.tex', 'wb') as texout:
47 yield Spec(args.url, fetcher, PandocTexifier(args.pandoc or 'pandoc'), texout)