-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
70 lines (55 loc) · 1.9 KB
/
main.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
# python code for mkdocs macros plugin
from __future__ import annotations
import os
from jinja2_shell_extension import shell # type: ignore
from stlog.formatter import (
DEFAULT_STLOG_HUMAN_FORMAT,
DEFAULT_STLOG_JSON_FORMAT,
DEFAULT_STLOG_LOGFMT_FORMAT,
DEFAULT_STLOG_RICH_HUMAN_FORMAT,
)
os.environ["STLOG_UNIT_TESTS_MODE"] = "1"
def define_env(env):
env.filters["shell"] = shell
@env.macro
def apilink(obj: str = "", title: str = ""):
if not obj:
url = env.variables["_apilink"]
else:
tmp = obj.split(".")
if len(tmp) == 2:
url = env.variables["_apilink"] + f"{tmp[0]}.html#stlog.{obj}"
else:
url = env.variables["_apilink"] + "#stlog." + obj
if not title:
if obj:
title = f"`{obj}`"
else:
title = "API reference"
return f"[{title}]({url})"
@env.macro
def code_example(filename: str):
with open(f"./docs/python/{filename}") as f:
return f.read()
@env.macro
def code_example_to_svg(
filename: str, interpreter: str = "python", lines: int = 10
):
cmd = f"python ./docs/python/termtosvg.py --interpreter={interpreter} --lines={lines} {filename}"
return shell(None, cmd, die_on_error=True)
@env.macro
def code_example_to_output(filename: str, interpreter: str = "python"):
cmd = f"STLOG_USE_RICH=0 ; {interpreter} docs/python/{filename}"
return shell(None, cmd, die_on_error=True)
@env.macro
def default_human_format():
return DEFAULT_STLOG_HUMAN_FORMAT
@env.macro
def default_rich_human_format():
return DEFAULT_STLOG_RICH_HUMAN_FORMAT
@env.macro
def default_logfmt_format():
return DEFAULT_STLOG_LOGFMT_FORMAT
@env.macro
def default_json_format():
return DEFAULT_STLOG_JSON_FORMAT