forked from intel/device-modeling-language
-
Notifications
You must be signed in to change notification settings - Fork 0
/
messages_to_md.py
98 lines (74 loc) · 2.54 KB
/
messages_to_md.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
# © 2021 Intel Corporation
# SPDX-License-Identifier: MPL-2.0
import sys
import argparse
def fmt_message(err):
msg = err.fmt
# Keep only the first line
msg = msg.split('\n')[0]
# Escape markup
msg = msg.replace('&', '&').replace('<', '<').replace('>', '>')
# Replace placeholders
msg = msg.replace('%s', '...')
msg = msg.replace('%d', '<i>N</i>')
return msg
def extract_messages(sys_path):
sys.path.append(sys_path)
from dml import messages
from dml.messages import DMLError, DMLWarning
errors = []
warnings = []
for n in dir(messages):
o = getattr(messages, n)
#sys.stderr.write("%s: %r\n" % (n, o))
if isinstance(o, type):
if issubclass(o, DMLError) and o is not DMLError:
errors.append(o)
elif issubclass(o, DMLWarning) and o is not DMLWarning:
warnings.append(o)
errors.sort(key=lambda x: x.fmt)
warnings.sort(key=lambda x: x.fmt)
return (warnings, errors)
def print_message_table(f, messages):
f.write("<dl>\n")
for m in messages:
assert m.__doc__
f.write(f" <dt><b>\n\n{fmt_message(m)} [{m.__name__}]</b></dt>\n")
doc = '\n'.join(line[4:] if line.startswith(' ') else line
for line in m.__doc__.strip().splitlines())
f.write(f" <dd>\n\n{doc}\n</dd>\n")
f.write("</dl>\n")
def print_messages(f, warnings, errors):
f.write("""
# Messages
The following sections list the warnings and error messages from
`dmlc`, with some clarifications.
## Warning Messages
The messages are listed in alphabetical order; the corresponding tags
are shown within brackets, e.g., `[WNDOC]`.
""")
print_message_table(f, warnings)
f.write("""
## Error Messages
The messages are listed in alphabetical order; the corresponding tags
are shown within brackets, e.g., `[ENBOOL]`.
""")
print_message_table(f, errors)
def matches_version(message, target):
ver = message.version
if not ver:
return True
if isinstance(ver, str):
ver = (ver,)
return target in ver
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('path')
parser.add_argument('version')
parser.add_argument('outfile')
args = parser.parse_args()
(warnings, errors) = extract_messages(args.path)
with open(args.outfile, 'w') as f:
print_messages(
f, [w for w in warnings if matches_version(w, args.version)],
[e for e in errors if matches_version(e, args.version)])