-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdinbrief.py
136 lines (106 loc) · 3.52 KB
/
dinbrief.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import logging
import os
import pathlib
from collections import ChainMap
import click
import click_log
import crayons
import delegator
import yaml
from click_default_group import DefaultGroup
from halo import Halo
THIS_DIR = pathlib.Path(__file__).parent
CONFIG_DIR = pathlib.Path.home() / ".config/dinbrief"
DEFAULTS = {
"compiler": "pandoc",
"flags": "--pdf-engine=xelatex",
"template": THIS_DIR / "template.tex",
"letter": THIS_DIR / "letter.md",
"defaults": "",
}
logger = logging.getLogger("dinbrief")
click_log.basic_config(logger)
LOG_LEVELS = {
"fatal": logging.CRITICAL,
"error": logging.ERROR,
"warn": logging.WARNING,
"info": logging.INFO,
"debug": logging.DEBUG,
}
def log(msg, level="info"):
colors = {"debug": crayons.yellow, "error": crayons.red}
fmter = colors.get(level, crayons.black)
logger.log(LOG_LEVELS.get(level, 0), fmter(msg))
def get_config():
def find_local_config() -> pathlib.Path:
# include the cwd in the parents iterator
for dir in (pathlib.Path.cwd() / "fake-file").parents:
if (dir / ".dinbrief").is_dir():
return dir / ".dinbrief"
return pathlib.Path("/")
def _dir_config(dir):
config = {}
user_template = dir / "template.tex"
user_defaults = dir / "defaults.yml"
letter = dir / "letter.md"
letterhead = dir / "letterhead.tex"
if user_template.exists():
config["template"] = user_template
if user_defaults.exists():
config["defaults"] = user_defaults
if letterhead.exists():
config["letterhead"] = str(letterhead.resolve())
if letter.exists():
config["letter"] = letter
return config
local_config = _dir_config(find_local_config())
user_config = _dir_config(pathlib.Path.home() / ".config/dinbrief")
config = ChainMap(local_config, user_config, DEFAULTS)
log("Using config:", "debug")
for k, v in sorted(config.items()):
log(f"\t{k}:\t{v}", "debug")
return config
@click.group(cls=DefaultGroup, default="compile", default_if_no_args=True)
@click_log.simple_verbosity_option(logger)
def cli():
pass
def run(cmd, text=""):
text = text or f"Running {cmd.split(' ')[0]}"
with Halo(text=text, spinner="dots") as spinner:
log("Running:", "debug")
log(f"\t$ {cmd}", "debug")
c = delegator.run(cmd)
if c.out:
log(c.out)
if c.err:
log(c.err, "error")
spinner.fail()
else:
spinner.succeed()
@cli.command()
@click.option("--md", default="letter.md")
@click.option("--pdf", default=None)
@click.pass_context
def compile(ctx, md, pdf):
if pdf is None:
pdf = pathlib.Path(md).with_suffix(".pdf")
config = get_config()
letterhead = config.pop("letterhead", "")
letterhead_opt = f"-M letterhead={letterhead}" if letterhead else ""
cmd = f"{config['compiler']} {md} --metadata-file={config['defaults']} -o {pdf} --template={config['template']} {config['flags']} {letterhead_opt}"
run(cmd, text=f"Compiling {md}")
@cli.command()
def create_defaults():
pathlib.Path(CONFIG_DIR).mkdir(exist_ok=True, parents=True)
tpath = CONFIG_DIR / "template.tex"
if not tpath.exists():
cmd = f"cp {DEFAULTS['template']} {tpath}"
run(cmd)
@cli.command()
@click.option("--dir", default=os.path.curdir)
def copy(dir):
letter = get_config()["letter"]
cmd = f"cp {letter} {dir}"
run(cmd)
if __name__ == "__main__":
cli()