-
-
Notifications
You must be signed in to change notification settings - Fork 453
/
Copy pathgen_error_wikitable.py
97 lines (82 loc) · 4.26 KB
/
gen_error_wikitable.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
##############################################################################
#
# PROJECT: Multi Theft Auto v1.0
# LICENSE: See LICENSE in the top level directory
# FILE: utils/gen_error_wikitable.py
# PURPOSE: Generates a MediaWiki table for error codes from source code
# DEVELOPERS: Dan Chowdhury <>
#
# Multi Theft Auto is available from http://www.multitheftauto.com/
#
##############################################################################
import os
import re
import sys
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-d", "--dir", dest="dir",
help="MTA Source code location", default="../" )
parser.add_option("-o", "--output", dest="output",
help="Output .txt file", default="errors_wikicode.txt" )
(options, args) = parser.parse_args()
ErrorCodes = {}
# Scan for .cpp and .h files
for root,dirs,files in os.walk(options.dir):
for file in files:
filename,ext = os.path.splitext(file)
if ext == ".c" or ext == ".cpp" or ext == ".h" or ext == ".hpp":
filePath = os.path.abspath(os.path.join(root,file))
#Parse the file looking for error codes
openedFile = open(filePath,"r")
lineNo = 0
for line in openedFile:
lineNo += 1
startScan = line.find('_E("')
if startScan == -1:
continue
# Check previous character is not alphanumerical
if (startScan > 0 and re.match('^[\w-]+$',line[startScan-1:startScan]) != None ):
continue
endScan = line.find('"',startScan+4)
errorCode = line[startScan+4:endScan]
# Find any other strings and compile for information
newStartScan = line.find('"',0)
finalIndex = 0
information = []
while newStartScan != -1:
newEndScan = line.find('"',newStartScan+1)
foundString = line[newStartScan+1:newEndScan]
if foundString != errorCode:
information.append(foundString)
newStartScan = line.find('"',newEndScan+1)
finalIndex = max([ finalIndex, newEndScan+1 ])
# Find any line comments and append
newStartScan = line.rfind("//",finalIndex)
if newStartScan != -1:
information.append(line[newStartScan+2:-1].lstrip())
if errorCode in ErrorCodes:
print("WARNING: Error Code conflict: " + errorCode )
ErrorCodes[errorCode] = { "information" : information, "file" : os.path.relpath(filePath,options.dir), "line" : lineNo }
openedFile.close()
#### Put into media wiki format
wikicode = "<!-- Automatically generated with %s -->"%(os.path.relpath(sys.argv[0],options.dir)) + """
{| class="wikitable" style="width: auto; text-align: center; table-layout: fixed;"
|-
!Error Code
!Associated strings
!File:LineNumber
"""
for code,data in sorted(ErrorCodes.items()):
wikicode += "|-\n"
wikicode += "|'''%s'''\n"%(code)
wikicode += "|"
if len(data["information"]) > 0:
wikicode += ("\n\n").join(data["information"])
wikicode += "\n"
wikicode += "|%s:%s\n"%(data["file"],data["line"])
#print code, data["information"], "%s:%s"%(data["file"],data["line"])
wikicode += "|}"
outputFile = open(options.output,"w")
outputFile.write(wikicode)
outputFile.close()
print( "Operation complete, %i error codes found. File written to '%s'."%(len(ErrorCodes),os.path.abspath(options.output)) )