home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 July / maximum-cd-2010-07.iso / DiscContents / wesnoth-1.8-win32.exe / data / tools / scoutDefault.py < prev    next >
Encoding:
Python Source  |  2009-03-01  |  11.0 KB  |  282 lines

  1. #!/usr/bin/env python
  2. #
  3. # Automagically set the village_per_scout parameters in MP scenarios.
  4.  
  5. import sys
  6. import os
  7. import getopt
  8. import re
  9.  
  10. overwrite = 0
  11. defaultValue = 4
  12. suffix = ''
  13.  
  14. RE_SCENARIO = '.*?\[multiplayer\].*?\[\/multiplayer\]'
  15. scenario_block = re.compile(RE_SCENARIO, re.DOTALL)
  16.  
  17. RE_SIDE = re.compile('( |\t)*\[side\].*?\[\/side\]( |\t)*\n', re.DOTALL)
  18. side_block = re.compile(RE_SIDE, re.DOTALL)
  19.  
  20. RE_AI = re.compile('( |\t)*\[ai\].*?\[\/ai\]( |\t)*\n', re.DOTALL)
  21. ai_block = re.compile(RE_AI, re.DOTALL)
  22.  
  23. AI_SIDE = re.compile('(?P<text>( |\t)*side=(\w| |\-|\,|\t)*)\n', re.DOTALL)
  24. AI_TIME = re.compile('(?P<text>( |\t)*time_of_day=(\w| |\-|\,|\t)*)', re.DOTALL)
  25. AI_TURNS = re.compile('(?P<text>( |\t)*turns=(\w| |\-|\,|\t)*)', re.DOTALL)
  26. AI_SCOUTS = re.compile('(?P<text>( |\t)*villages_per_scout=(\w| |\-|\,|\t)*)', re.DOTALL)
  27. AI_SCOUTS_VALUE = re.compile('(?P<text>(?<=villages_per_scout=)(\d)*)', re.DOTALL)
  28. AI_CAN_RECRUIT = re.compile('(?P<text>(?<=canrecruit=)(\d)*)', re.DOTALL)
  29. AI_START = re.compile('(?P<text>( |\t)*\[ai\](\w| |\t)*\n)', re.DOTALL)
  30.  
  31. IF_TEXT = "[if]\n"
  32. ENDIF_TEXT = "[/if]\n"
  33. ELSE_TEXT = "[else]\n"
  34. ENDELSE_TEXT = "[/else]"
  35.  
  36. SCOUTS_TEXT = "\n\tvillages_per_scout=0"
  37. AI_SCOUTS_TEXT = "\n\t[ai]" + SCOUTS_TEXT.replace('\n','\n\t') + "\n\t[/ai]"
  38.  
  39. def applySearch(text, RE, groupId):
  40.     data = RE.search(text, 0)
  41.     if data <> None:
  42.         return data.group(groupId)
  43.     else:
  44.         return ""
  45.  
  46. def updateDescription(ai,sides):
  47.     if ai.value <> "":
  48.         new = defaultValue*sides
  49.         ai.updated_description = ai.updated_description.replace(ai.value,"%s"%(new))
  50.  
  51. def getIndent(itemText,subitemText):
  52.     item = re.compile('^( |\t)*').search(itemText).group()
  53.     subitem = re.compile('^( |\t)*').search(subitemText).group()
  54.     if item == '' or subitem == '':
  55.         return subitem
  56.     if item[0] == '\t' and subitem[0] == '\t':
  57.         return (len(subitem)-len(item)) * '\t'
  58.     if item[0] == ' ' and subitem[0] == ' ':
  59.         return (len(subitem)-len(item)) * ' '
  60.     return '\t'
  61.     
  62. class wikiAi:
  63.     def __init__(self):
  64.         self.start = ""
  65.         self.scouts = ""
  66.         self.full_description = ""
  67.         self.updated_description = ""
  68.  
  69.     def addAiData(self,aiContent):
  70.         if aiContent <> None:
  71.             self.start = applySearch(aiContent, AI_START, 'text')
  72.             self.scouts = applySearch(aiContent, AI_SCOUTS, 'text')
  73.             self.full_description = aiContent
  74.             self.updated_description = aiContent
  75.             self.value = applySearch(aiContent, AI_SCOUTS_VALUE, 'text')
  76.  
  77. class wikiAiList(list):
  78.     def __str__(self):
  79.         output = ""
  80.         for item in self:
  81.             output = output + item.full_description + " ; "
  82.         return output
  83.  
  84. class wikiSide:
  85.     def __init__(self):
  86.         self.full_description = ''
  87.         self.updated_description = ''
  88.         self.side = ""
  89.         # Will only contain one element
  90.         self.ai = wikiAiList()
  91.         self.scouts_setting = False
  92.  
  93.     def addAiData(self,sideContent):
  94.         if sideContent <> None:
  95.             aiDetail = ai_block.search(sideContent,0)
  96.             while aiDetail <> None:
  97.                 if applySearch(aiDetail.group(), AI_TIME, 'text') == "":
  98.                     if applySearch(aiDetail.group(), AI_TURNS, 'text') == "":
  99.                         self.ai.append(wikiAi())
  100.                         self.ai[self.getCurrentAiNumber()].addAiData(aiDetail.group())
  101.                         if self.ai[self.getCurrentAiNumber()].scouts <> "":
  102.                             self.scouts_setting = True
  103.                         break
  104.                 searchStart = aiDetail.end()
  105.                 aiDetail = ai_block.search(sideContent,searchStart)
  106.      
  107.  
  108.     def updateAi(self,sides):
  109.         if len(self.ai) == 0:
  110.             self.ai.append(wikiAi())
  111.             space = re.compile('^( |\t)*').search(self.full_description).group()
  112.             indent = getIndent(self.full_description,self.side)
  113.             side_scout_text = AI_SCOUTS_TEXT.replace('\t',indent)
  114.             side_scout_text = side_scout_text.replace('\n','\n' + space)
  115.             self.ai[self.getCurrentAiNumber()].addAiData(side_scout_text)
  116.             self.updated_description = self.updated_description.replace('\n',self.ai[self.getCurrentAiNumber()].full_description + '\n',1)
  117.             updateDescription(self.ai[0],sides)
  118.         else:
  119.             if not self.scouts_setting:
  120.                 space = re.compile('^( |\t)*').search(self.full_description).group()
  121.                 indent = getIndent(self.full_description,self.side)
  122.                 side_scout_text = AI_SCOUTS_TEXT.replace('\t',indent)
  123.                 side_scout_text = side_scout_text.replace('\n','\n' + space)
  124.                 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'))
  125.                 updateDescription(self.ai[0],sides)
  126.             else:
  127.                 if overwrite == 1:
  128.                     updateDescription(self.ai[0],sides)
  129.         if self.ai[0].full_description <> self.ai[0].updated_description:
  130.             self.updated_description = self.updated_description.replace(self.ai[0].full_description,self.ai[0].updated_description,1)
  131.             
  132.     def getCurrentAiNumber(self):
  133.         return len(self.ai)-1
  134.  
  135. class wikiSideList(list):
  136.     def __str__(self):
  137.         output = ""
  138.         for item in self:
  139.             output = output + item.full_description + " ; "
  140.         return output
  141.  
  142. class wikiScenario:
  143.     def __init__(self):
  144.         self.side = wikiSideList()
  145.         self.full_description = ''
  146.         self.updated_description = ''
  147.  
  148.     def parseScenario (self,scenarioContent):
  149.         self.addScenarioData(scenarioContent)
  150.         sideDetail = side_block.search(scenarioContent,0)
  151.         while (sideDetail <> None):
  152.             self.addSideData(sideDetail.group())
  153.             self.addAiData(sideDetail.group())
  154.             searchStart = sideDetail.end()
  155.             sideDetail = side_block.search(scenarioContent,searchStart)
  156.         self.updateAi()
  157.  
  158.     def addScenarioData(self, scenarioContent):
  159.         self.full_description = scenarioContent
  160.         self.updated_description = scenarioContent
  161.  
  162.     def addSideData(self, sideContent):
  163.         canrecruit = applySearch(sideContent, AI_CAN_RECRUIT, 'text')
  164.         if canrecruit <> "" and canrecruit == "0":
  165.           return
  166.         self.side.append(wikiSide())
  167.         self.side[self.getCurrentSideNumber()].full_description = sideContent
  168.         self.side[self.getCurrentSideNumber()].updated_description = sideContent
  169.         self.side[self.getCurrentSideNumber()].side = applySearch(sideContent, AI_SIDE, 'text')
  170.  
  171.     def addAiData(self,aiContent):
  172.         self.side[self.getCurrentSideNumber()].addAiData(aiContent)
  173.  
  174.     def updateAi(self):
  175.         for side in self.side:
  176.             side.updateAi(len(self.side))
  177.         for side in self.side:
  178.             if side.full_description <> side.updated_description:
  179.                 self.updated_description = self.updated_description.replace(side.full_description,side.updated_description,1)
  180.  
  181.     def getCurrentSideNumber(self):
  182.         return len(self.side)-1
  183.  
  184. class wikiScenarioList(list):
  185.     def __str__(self):
  186.         output = ""
  187.         for scenario in self:
  188.             output = output + scenario.full_description + " ; "
  189.         return output
  190.  
  191.     def addScenario(self, scenario):
  192.         self.append(scenario)
  193.  
  194. def parseAll (resourcesFile,dirName, fileList):
  195.     scenarioListIndex = 0
  196.     scenarioList = wikiScenarioList()
  197.     for fileName in fileList:
  198.         if os.path.splitext(fileName)[1] <> '.cfg':
  199.             continue
  200.         if os.path.isdir(os.path.join(dirName,fileName)):
  201.             continue
  202.         f = file(os.path.join(dirName,fileName))
  203.         fileContent = f.read()
  204.         f.close()
  205.         searchStart = 0
  206.         scenario = scenario_block.match(fileContent,searchStart)
  207.         while (scenario <>  None):
  208.             scenarioList.addScenario(wikiScenario())
  209.             scenarioList[scenarioListIndex].parseScenario(scenario.group(0))
  210.             searchStart = scenario.end()
  211.             scenario = scenario_block.search(fileContent,searchStart)
  212.             scenarioListIndex = scenarioListIndex + 1
  213.         updated_file = fileContent
  214.         for scenarioItem in scenarioList:
  215.             if scenarioItem.full_description <> scenarioItem.updated_description:
  216.                 updated_file = updated_file.replace(scenarioItem.full_description,scenarioItem.updated_description)
  217.         if updated_file <> fileContent:
  218.             (basename_out, ext_out) = os.path.splitext(fileName)
  219.             basename_out = basename_out + suffix + ext_out
  220.             f = file(basename_out,'w')
  221.             f.write(updated_file)
  222.             f.close()
  223.  
  224. def printUsage():
  225.     print "scoutDefault.py [-hRO] [-d directory] [-f file] [-x extension]"
  226.     print "-h : print this message"
  227.     print "-R : recursively parse directories"
  228.     print "-O : overwrite village_per_scout value in scenario"
  229.     print "-d : directory to look for file to parse"
  230.     print "-f : name of the file to parse"
  231.     print "-x : suffix to append to filename"
  232.     print "Example of use:"
  233.     print "  ./scoutDefault.py -h"
  234.     print "    Get help"
  235.     print "  ./scoutDefault.py -f 2p_Blitz.cfg -x _new"
  236.     print "    Run the script and write output on 2p_Blitz_new.cfg"
  237.     print "  ./scoutDefault.py -d /usr/local/share/wesnoth/data/scenarios"
  238.     print "    Run the script on all file under that directory"
  239.     print "  ./scoutDefault.py -R -d /usr/local/share/wesnoth"
  240.     print "    Run the script on all directories under that directory"
  241.     print "  ./scoutDefault.py -f 2p_Blitz.cfg -O"
  242.     print "    Run the script on 2p_Blitz.cfg and delete previous value"
  243.  
  244. recursive = 0
  245. entryPoint = os.getcwd()
  246. entryFile = os.listdir(os.getcwd())
  247. resourcesFile = {}
  248. try:
  249.     (opts, argsProper) = getopt.getopt(sys.argv[1:], 'ROhf:d:x:v:"')
  250. except getopt.GetoptError, e:
  251.     print 'Error parsing command-line arguments: %s' % e
  252.     printUsage()
  253.     sys.exit(1)
  254. for (option, parameter) in opts:
  255.     if option == '-h': # Print the commandline help and exit.
  256.         printUsage()
  257.         sys.exit(0)
  258.     elif option == '-R':
  259.         recursive = 1
  260.     elif option == '-O':
  261.         overwrite = 1
  262.     elif option == '-d':
  263.         if not os.path.exists(parameter):
  264.             print 'Error: %s directory does not exist' % parameter
  265.             sys.exit(1)
  266.         elif not os.path.isdir(parameter):
  267.             print 'Error: %s is not a directory' % parameter
  268.             sys.exit(1)
  269.         entryPoint = parameter
  270.         entryFile = os.listdir(entryPoint)
  271.     elif option == '-f':
  272.         entryFile = []
  273.         entryFile.append(os.path.basename(parameter))
  274.         entryPoint = os.path.dirname(parameter)
  275.     elif option == '-x':
  276.         suffix = parameter
  277.  
  278. if recursive == 1:
  279.     os.path.walk(entryPoint,parseAll,resourcesFile)
  280. else:
  281.     parseAll(resourcesFile,entryPoint,entryFile)
  282.