forked from Arelle/Arelle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateMessagesCatalog.py
153 lines (140 loc) · 7.66 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
'''
Created on Aug 26, 2012
@author: Mark V Systems Limited
(c) Copyright 2012 Mark V Systems Limited, All rights reserved.
'''
import os, time, io, ast
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 + "examples" + os.sep + "plugin"):
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) 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
errCodeArg = item.args[0 + iArgOffset] # str or tuple
if isinstance(errCodeArg,ast.Str):
errCodes = (errCodeArg.s,)
else:
if any(isinstance(elt, (ast.Call, ast.Name))
for elt in ast.walk(errCodeArg)):
errCodes = ("(dynamic)",)
else:
errCodes = [elt.s
for elt in ast.walk(errCodeArg)
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 = [keyword.arg
for keyword in item.keywords
if keyword.arg != 'modelObject']
for errCode in errCodes:
idMsg.append((errCode, msg, level, keywords, refFilename, item.lineno))
except (AttributeError, IndexError):
pass
lines = []
for id,msg,level,args,module,line in idMsg:
try:
lines.append("<message code=\"{0}\"\n level=\"{3}\"\n module=\"{4}\" line=\"{5}\"\n args=\"{2}\">\n{1}\n</message>"
.format(id,
entityEncode(msg),
entityEncode(" ".join(args)),
level,
module,
line))
except Exception as ex:
print(ex)
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) ))