forked from Arelle/Arelle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generateMessagesCatalog.py
177 lines (166 loc) · 9.42 KB
/
generateMessagesCatalog.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
'''
See COPYRIGHT.md for copyright information.
'''
import os, time, io, ast, sys, traceback
def entityEncode(arg): # be sure it's a string, vs int, etc, and encode &, <, ".
return str(arg).replace("&","&").replace("<","<").replace('"','"')
if __name__ == "__main__":
startedAt = time.time()
idMsg = []
numArelleSrcFiles = 0
arelleSrcPath = (os.path.dirname(__file__) or os.curdir) + os.sep + "arelle"
for arelleSrcDir in (arelleSrcPath,
arelleSrcPath + os.sep + "plugin",
arelleSrcPath + os.sep + "plugin" + os.sep + "validate" + os.sep + "EFM",
# arelleSrcPath + os.sep + "plugin" + os.sep + "validate" + os.sep + "ESEF",
arelleSrcPath + os.sep + "plugin" + os.sep + "EdgarRenderer"
# arelleSrcPath + os.sep + "plugin" + os.sep + "validate" + os.sep + "GL"
):
if not os.path.exists(arelleSrcDir):
continue
for moduleFilename in os.listdir(arelleSrcDir):
if moduleFilename.endswith(".py"):
numArelleSrcFiles += 1
fullFilenamePath = arelleSrcDir + os.sep + moduleFilename
refFilename = fullFilenamePath[len(arelleSrcPath)+1:].replace("\\","/")
with open(fullFilenamePath, encoding="utf-8") as f:
tree = ast.parse(f.read(), filename=moduleFilename)
for item in ast.walk(tree):
try:
if (isinstance(item, ast.Call) and
(getattr(item.func, "attr", '') or getattr(item.func, "id", '')) # imported function could be by id instead of attr
in ("info","warning","log","error","exception")):
funcName = item.func.attr
iArgOffset = 0
if funcName == "info":
level = "info"
elif funcName == "warning":
level = "warning"
elif funcName == "error":
level = "error"
elif funcName == "exception":
level = "exception"
elif funcName == "log":
levelArg = item.args[0]
if isinstance(levelArg,ast.Str):
level = levelArg.s.lower()
else:
if any(isinstance(elt, (ast.Call, ast.Name))
for elt in ast.walk(levelArg)):
level = "(dynamic)"
else:
level = ', '.join(elt.s.lower()
for elt in ast.walk(levelArg)
if isinstance(elt, ast.Str))
iArgOffset = 1
msgCodeArg = item.args[0 + iArgOffset] # str or tuple
if isinstance(msgCodeArg,ast.Str):
msgCodes = (msgCodeArg.s,)
elif isinstance(msgCodeArg, ast.Call) and getattr(msgCodeArg.func, "id", '') == 'ixMsgCode':
msgCodes = ("ix{{ver.sect}}:{}".format(msgCodeArg.args[0].s),)
else:
if any(isinstance(elt, (ast.Call, ast.Name))
for elt in ast.walk(msgCodeArg)):
msgCodes = ("(dynamic)",)
else:
msgCodes = [elt.s
for elt in ast.walk(msgCodeArg)
if isinstance(elt, ast.Str)]
msgArg = item.args[1 + iArgOffset]
if isinstance(msgArg, ast.Str):
msg = msgArg.s
elif isinstance(msgArg, ast.Call) and getattr(msgArg.func, "id", '') == '_':
msg = msgArg.args[0].s
elif any(isinstance(elt, (ast.Call,ast.Name))
for elt in ast.walk(msgArg)):
msg = "(dynamic)"
else:
continue # not sure what to report
keywords = []
for keyword in item.keywords:
if keyword.arg == 'modelObject':
pass
elif keyword.arg == 'messageCodes':
msgCodeArg = keyword.value
if any(isinstance(elt, (ast.Call, ast.Name))
for elt in ast.walk(msgCodeArg)):
pass # dynamic
else:
msgCodes = [elt.s
for elt in ast.walk(msgCodeArg)
if isinstance(elt, ast.Str)]
else:
keywords.append(keyword.arg)
for msgCode in msgCodes:
idMsg.append((msgCode, msg, level, keywords, refFilename, item.lineno))
except (AttributeError, IndexError):
pass
lines = []
for id,msg,level,args,module,line in idMsg:
try:
if args and any(isinstance(arg,str) for arg in args):
argAttr = "\n args=\"{0}\"".format(
entityEncode(" ".join(arg for arg in args if arg is not None)))
else:
argAttr = ""
lines.append("<message code=\"{0}\"\n level=\"{3}\"\n module=\"{4}\" line=\"{5}\"{2}>\n{1}\n</message>"
.format(id,
entityEncode(msg),
argAttr,
level,
module,
line))
except Exception as ex:
print(ex)
print("traceback {}".format(traceback.format_tb(sys.exc_info()[2])))
os.makedirs(arelleSrcPath + os.sep + "doc", exist_ok=True)
with io.open(arelleSrcPath + os.sep + "doc" + os.sep + "messagesCatalog.xml", 'wt', encoding='utf-8') as f:
f.write(
'''<?xml version="1.0" encoding="utf-8"?>
<messages
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="messagesCatalog.xsd"
variablePrefix="%("
variableSuffix=")s"
variablePrefixEscape="" >
<!--
This file contains Arelle messages text. Each message has a code
that corresponds to the message code in the log file, level (severity),
args (available through log file), and message replacement text.
(Messages with dynamically composed error codes or text content
(such as ValidateXbrlDTS.py line 158 or lxml parser messages)
are reported as "(dynamic)".)
-->
''')
f.write("\n\n".join(sorted(lines)))
f.write("\n\n</messages>")
with io.open(arelleSrcPath + os.sep + "doc" + os.sep + "messagesCatalog.xsd", 'wt', encoding='utf-8') as f:
f.write(
'''<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xs:element name="messages">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="message">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="code" use="required" type="xs:normalizedString"/>
<xs:attribute name="level" use="required" type="xs:token"/>
<xs:attribute name="module" type="xs:normalizedString"/>
<xs:attribute name="line" type="xs:integer"/>
<xs:attribute name="args" type="xs:NMTOKENS"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="variablePrefix" type="xs:string"/>
<xs:attribute name="variableSuffix" type="xs:string"/>
<xs:attribute name="variablePrefixEscape" type="xs:string"/>
</xs:complexType>
</xs:element>
</xs:schema>
''')
print("Arelle messages catalog {0:.2f} secs, {1} formula files, {2} messages".format( time.time() - startedAt, numArelleSrcFiles, len(idMsg) ))