-
-
Notifications
You must be signed in to change notification settings - Fork 988
/
scoutDefault.py
executable file
·278 lines (245 loc) · 10.4 KB
/
scoutDefault.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/env python2
#
# Automagically set the village_per_scout parameters in MP scenarios.
import sys
import os
import getopt
import re
overwrite = False
defaultValue = 4
suffix = ''
RE_SCENARIO = '.*?\[multiplayer\].*?\[\/multiplayer\]'
scenario_block = re.compile(RE_SCENARIO, re.DOTALL)
side_block = re.compile('( |\t)*\[side\].*?\[\/side\]( |\t)*\n', re.DOTALL)
ai_block = re.compile('( |\t)*\[ai\].*?\[\/ai\]( |\t)*\n', re.DOTALL)
AI_SIDE = re.compile('(?P<text>( |\t)*side=(\w| |\-|\,|\t)*)\n', re.DOTALL)
AI_TIME = re.compile('(?P<text>( |\t)*time_of_day=(\w| |\-|\,|\t)*)', re.DOTALL)
AI_TURNS = re.compile('(?P<text>( |\t)*turns=(\w| |\-|\,|\t)*)', re.DOTALL)
AI_SCOUTS = re.compile('(?P<text>( |\t)*villages_per_scout=(\w| |\-|\,|\t)*)', re.DOTALL)
AI_SCOUTS_VALUE = re.compile('(?P<text>(?<=villages_per_scout=)(\d)*)', re.DOTALL)
AI_CAN_RECRUIT = re.compile('(?P<text>(?<=canrecruit=)(\d)*)', re.DOTALL)
AI_START = re.compile('(?P<text>( |\t)*\[ai\](\w| |\t)*\n)', re.DOTALL)
IF_TEXT = "[if]\n"
ENDIF_TEXT = "[/if]\n"
ELSE_TEXT = "[else]\n"
ENDELSE_TEXT = "[/else]"
SCOUTS_TEXT = "\n\tvillages_per_scout=0"
AI_SCOUTS_TEXT = "\n\t[ai]%s\n\t[/ai]" % SCOUTS_TEXT.replace('\n','\n\t')
def applySearch(text, RE, groupId):
data = RE.search(text, 0)
if data != None:
return data.group(groupId)
else:
return ""
def updateDescription(ai, sides):
if ai.value != "":
new = defaultValue * sides
ai.updated_description = ai.updated_description.replace(ai.value, str(new))
def getIndent(itemText, subitemText):
item = re.compile('^( |\t)*').search(itemText).group()
subitem = re.compile('^( |\t)*').search(subitemText).group()
if item == '' or subitem == '':
return subitem
if item[0] == '\t' and subitem[0] == '\t':
return (len(subitem) - len(item)) * '\t'
if item[0] == ' ' and subitem[0] == ' ':
return (len(subitem) - len(item)) * ' '
return '\t'
class wikiAi:
def __init__(self):
self.start = ""
self.scouts = ""
self.full_description = ""
self.updated_description = ""
def addAiData(self, aiContent):
if aiContent != None:
self.start = applySearch(aiContent, AI_START, 'text')
self.scouts = applySearch(aiContent, AI_SCOUTS, 'text')
self.full_description = aiContent
self.updated_description = aiContent
self.value = applySearch(aiContent, AI_SCOUTS_VALUE, 'text')
class wikiAiList(list):
def __str__(self):
output = ""
for item in self:
output = output + item.full_description + " ; "
return output
class wikiSide:
def __init__(self):
self.full_description = ''
self.updated_description = ''
self.side = ''
# Will only contain one element
self.ai = wikiAiList()
self.scouts_setting = False
def addAiData(self, sideContent):
if sideContent != None:
aiDetail = ai_block.search(sideContent, 0)
while aiDetail != None:
if applySearch(aiDetail.group(), AI_TIME, 'text') == "" and applySearch(aiDetail.group(), AI_TURNS, 'text') == "":
self.ai.append(wikiAi())
self.ai[self.getCurrentAiNumber()].addAiData(aiDetail.group())
if self.ai[self.getCurrentAiNumber()].scouts != "":
self.scouts_setting = True
break
searchStart = aiDetail.end()
aiDetail = ai_block.search(sideContent, searchStart)
def updateAi(self, sides):
if not len(self.ai):
self.ai.append(wikiAi())
space = re.compile('^( |\t)*').search(self.full_description).group()
indent = getIndent(self.full_description, self.side)
side_scout_text = AI_SCOUTS_TEXT.replace('\t', indent)
side_scout_text = side_scout_text.replace('\n', '\n' + space)
self.ai[self.getCurrentAiNumber()].addAiData(side_scout_text)
self.updated_description = self.updated_description.replace('\n', self.ai[self.getCurrentAiNumber()].full_description + '\n', 1)
updateDescription(self.ai[0], sides)
else:
if not self.scouts_setting:
space = re.compile('^( |\t)*').search(self.full_description).group()
indent = getIndent(self.full_description, self.side)
side_scout_text = AI_SCOUTS_TEXT.replace('\t', indent)
side_scout_text = side_scout_text.replace('\n', '\n' + space)
self.ai[0].updated_description = self.ai[0].updated_description.replace(self.ai[0].start, self.ai[0].start.replace('\n', side_scout_text + '\n'))
updateDescription(self.ai[0], sides)
else:
if overwrite:
updateDescription(self.ai[0], sides)
if self.ai[0].full_description != self.ai[0].updated_description:
self.updated_description = self.updated_description.replace(self.ai[0].full_description, self.ai[0].updated_description, 1)
def getCurrentAiNumber(self):
return len(self.ai) - 1
class wikiSideList(list):
def __str__(self):
output = ""
for item in self:
output = output + item.full_description + " ; "
return output
class wikiScenario:
def __init__(self):
self.side = wikiSideList()
self.full_description = ''
self.updated_description = ''
def parseScenario (self, scenarioContent):
self.addScenarioData(scenarioContent)
sideDetail = side_block.search(scenarioContent, 0)
while sideDetail != None:
self.addSideData(sideDetail.group())
self.addAiData(sideDetail.group())
searchStart = sideDetail.end()
sideDetail = side_block.search(scenarioContent, searchStart)
self.updateAi()
def addScenarioData(self, scenarioContent):
self.full_description = scenarioContent
self.updated_description = scenarioContent
def addSideData(self, sideContent):
canrecruit = applySearch(sideContent, AI_CAN_RECRUIT, 'text')
if canrecruit == "0":
return
self.side.append(wikiSide())
self.side[self.getCurrentSideNumber()].full_description = sideContent
self.side[self.getCurrentSideNumber()].updated_description = sideContent
self.side[self.getCurrentSideNumber()].side = applySearch(sideContent, AI_SIDE, 'text')
def addAiData(self, aiContent):
self.side[self.getCurrentSideNumber()].addAiData(aiContent)
def updateAi(self):
for side in self.side:
side.updateAi(len(self.side))
for side in self.side:
if side.full_description != side.updated_description:
self.updated_description = self.updated_description.replace(side.full_description, side.updated_description, 1)
def getCurrentSideNumber(self):
return len(self.side) - 1
class wikiScenarioList(list):
def __str__(self):
output = ""
for scenario in self:
output = output + scenario.full_description + " ; "
return output
def addScenario(self, scenario):
self.append(scenario)
def parseAll(dirName, fileList):
scenarioListIndex = 0
scenarioList = wikiScenarioList()
for fileName in fileList:
if os.path.splitext(fileName)[1] != '.cfg':
continue
if os.path.isdir(os.path.join(dirName, fileName)):
continue
f = file(os.path.join(dirName, fileName))
fileContent = f.read()
f.close()
searchStart = 0
scenario = scenario_block.match(fileContent, searchStart)
while scenario != None:
scenarioList.addScenario(wikiScenario())
scenarioList[scenarioListIndex].parseScenario(scenario.group(0))
searchStart = scenario.end()
scenario = scenario_block.search(fileContent, searchStart)
scenarioListIndex += 1
updated_file = fileContent
for scenarioItem in scenarioList:
if scenarioItem.full_description != scenarioItem.updated_description:
updated_file = updated_file.replace(scenarioItem.full_description, scenarioItem.updated_description)
if updated_file != fileContent:
(basename_out, ext_out) = os.path.splitext(fileName)
basename_out = basename_out + suffix + ext_out
f = file(basename_out,'w')
f.write(updated_file)
f.close()
def printUsage():
print """scoutDefault.py [-hRO] [-d directory] [-f file] [-x extension]
-h : print this message
-R : recursively parse directories
-O : overwrite village_per_scout value in scenario
-d : directory to look for file to parse
-f : name of the file to parse
-x : suffix to append to filename
Example of use:
./scoutDefault.py -h
Get help
./scoutDefault.py -f 2p_Blitz.cfg -x _new
Run the script and write output on 2p_Blitz_new.cfg
./scoutDefault.py -d /usr/local/share/wesnoth/data/scenarios
Run the script on all file under that directory
./scoutDefault.py -R -d /usr/local/share/wesnoth
Run the script on all directories under that directory
./scoutDefault.py -f 2p_Blitz.cfg -O
Run the script on 2p_Blitz.cfg and delete previous value"""
recursive = False
entryPoint = os.getcwd()
entryFile = os.listdir(os.getcwd())
resourcesFile = {}
try:
(opts, argsProper) = getopt.getopt(sys.argv[1:], 'ROhf:d:x:v:"')
except getopt.GetoptError, e:
print 'Error parsing command-line arguments: %s' % e
printUsage()
sys.exit(1)
for (option, parameter) in opts:
if option == '-h': # Print the commandline help and exit.
printUsage()
sys.exit(0)
elif option == '-R':
recursive = True
elif option == '-O':
overwrite = False
elif option == '-d':
if not os.path.exists(parameter):
print 'Error: %s directory does not exist' % parameter
sys.exit(1)
elif not os.path.isdir(parameter):
print 'Error: %s is not a directory' % parameter
sys.exit(1)
entryPoint = parameter
entryFile = os.listdir(entryPoint)
elif option == '-f':
entryFile = []
entryFile.append(os.path.basename(parameter))
entryPoint = os.path.dirname(parameter)
elif option == '-x':
suffix = parameter
if recursive == True:
os.path.walk(entryPoint, parseAll, resourcesFile)
else:
parseAll(entryPoint, entryFile)