]> git.scottworley.com Git - paperdoorknob/blame - args.py
Strip all  
[paperdoorknob] / args.py
CommitLineData
23f31879
SW
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
929db576 17from htmlfilter import ApplyHTMLFilters, HTMLFilters
23f31879
SW
18from spec import Spec
19from texify import PandocTexifier
20
21
22def _command_line_parser() -> ArgumentParser:
23 parser = ArgumentParser(prog='paperdoorknob', description='Print glowfic')
24 parser.add_argument(
25 '--cache_path',
26 metavar='PATH',
27 help='Where to keep the http cache (instead of %(default)s)',
28 default=os.path.join(xdg_cache_home(), "paperdoorknob"))
929db576
SW
29 parser.add_argument(
30 '--htmlfilters',
31 help='Which HTML filters to use (default: %(default)s)',
32 default=','.join(f[0] for f in HTMLFilters))
23f31879
SW
33 parser.add_argument(
34 '--out',
35 help='The filename stem at which to write output ' +
36 '(eg: "%(default)s" produces %(default)s.tex, %(default)s.pdf, etc.)',
37 default='book')
38 parser.add_argument('--pandoc', help='Location of the pandoc executable')
39 parser.add_argument(
40 '--timeout',
41 help='How long to wait for HTTP requests, in seconds',
42 default=30)
43 parser.add_argument('url', help='URL to retrieve')
44 return parser
45
46
47@contextmanager
48def spec_from_commandline_args() -> Iterator[Spec]:
49 args = _command_line_parser().parse_args()
50 with CachingFetcher(args.cache_path, args.timeout) as fetcher:
51 with open(args.out + '.tex', 'wb') as texout:
929db576
SW
52 yield Spec(
53 args.url,
54 fetcher,
55 lambda x: ApplyHTMLFilters(args.htmlfilters, x),
56 PandocTexifier(args.pandoc or 'pandoc'),
57 texout)