sc2simulator/scenarioMgr/functions.py
def parseBankXml(xmlpath, debug=False):
    """interpret scenario bank XML data into usable python objects"""
    root = xml.etree.ElementTree.parse(xmlpath).getroot()
    bankName = re.sub("\..*", "", os.path.basename(xmlpath)) # strip path and extension
    retBank = Bank(bankName)
    sections = root.getchildren()
    caseNameSection = getSectionByName(sections, "TestCases") # should be the first section, but 'search' anyway
    if not caseNameSection: return retBank # must define the "TestCases" section
    sections.remove(caseNameSection) # this meta section doesn't define scenario + player data
    for key in caseNameSection.getchildren(): # acquire each scenario's meta data
        name = key.get("name")
        s = Scenario(name)
        retBank.addScenario(s)
    for s in sections: # iterate all scenario + player sections for data
        sectionNames = s.get("name").split("|")
        scenarioName = sectionNames[0]
        playerID     = int(re.findall("P(\d+)", sectionNames[1]).pop())
        dataType     = sectionNames[2]
        scenario = retBank[scenarioName]
        if dataType == "UnitIndex":
            for key in s.getchildren(): # ensure all tags are represented as units in this scenario
                tag = key.get("name")
                scenario.addUnit(tag, allowDuplicate=True) # duplicates are allowed because XML scenario data can be unordered
        elif dataType == "UnitData":
            for key in s.getchildren(): # load unit data for all players in this scenario
                unitMeta = key.get("name").split("|")
                unitTag  = int(unitMeta[0])
                unitAttr = unitMeta[1].lower()
                attrs    = xmlChildrenToDict(key)
                if len(attrs) == 1 and list(attrs.keys())[0] == "value":
                    attrs = {unitAttr : attrs["value"]}
                u = scenario.updateUnit(unitTag, owner=playerID, **attrs)
        elif dataType == "Upgrades":
            for key in s.getchildren(): # ensure all tags are represented as units in this scenario
                value = key.get("name")
                try:  scenario.addUpgrade(playerID, value)
                except NotImplementedError as e:
                    print(("WARNING: cannot load scenario upgrade '%s' : %s")%(
                        value, e))
        else:
            print("WARNING: skipped unknown data type: %s"%dataType)
    if debug:
        print("*"*80)
        print(retBank)
        print("*"*80)
        for sName, scenario in retBank.scenarios.items():
            print(scenario)
            for p in scenario.players.values():
                p.display()
            print("*"*80)
    retBank.initPlayers()
    return retBank
interpret scenario bank XML data into usable python objects
interpret scenario bank XML data into usable python objects
interpret scenario bank XML data into usable python objects
def parseBankXml(xmlpath, debug=False):
    root = xml.etree.ElementTree.parse(xmlpath).getroot()
    bankName = re.sub("\..*", "", os.path.basename(xmlpath)) 
    retBank = Bank(bankName)
    sections = root.getchildren()
    caseNameSection = getSectionByName(sections, "TestCases") 
    if not caseNameSection: return retBank 
    sections.remove(caseNameSection) 
    for key in caseNameSection.getchildren(): 
        name = key.get("name")
        s = Scenario(name)
        retBank.addScenario(s)
    for s in sections: 
        sectionNames = s.get("name").split("|")
        scenarioName = sectionNames[0]
        playerID     = int(re.findall("P(\d+)", sectionNames[1]).pop())
        dataType     = sectionNames[2]
        scenario = retBank[scenarioName]
        if dataType == "UnitIndex":
            for key in s.getchildren(): 
                tag = key.get("name")
                scenario.addUnit(tag, allowDuplicate=True) 
        elif dataType == "UnitData":
            for key in s.getchildren(): 
                unitMeta = key.get("name").split("|")
                unitTag  = int(unitMeta[0])
                unitAttr = unitMeta[1].lower()
                attrs    = xmlChildrenToDict(key)
                if len(attrs) == 1 and list(attrs.keys())[0] == "value":
                    attrs = {unitAttr : attrs["value"]}
                u = scenario.updateUnit(unitTag, owner=playerID, **attrs)
        elif dataType == "Upgrades":
            for key in s.getchildren(): 
                value = key.get("name")
                try:  scenario.addUpgrade(playerID, value)
                except NotImplementedError as e:
                    print(("WARNING: cannot load scenario upgrade '%s' : %s")%(
                        value, e))
        else:
            print("WARNING: skipped unknown data type: %s"%dataType)
    if debug:
        print("*"*80)
        print(retBank)
        print("*"*80)
        for sName, scenario in retBank.scenarios.items():
            print(scenario)
            for p in scenario.players.values():
                p.display()
            print("*"*80)
    retBank.initPlayers()
    return retBank
def parseBankXml ( xmlpath , debug = False ) : root = xml . etree . ElementTree . parse ( xmlpath ) . getroot ( ) bankName = re . sub ( "\..*" , "" , os . path . basename ( xmlpath ) ) retBank = Bank ( bankName ) sections = root . getchildren ( ) caseNameSection = getSectionByName ( sections , "TestCases" ) if not caseNameSection : return retBank sections . remove ( caseNameSection ) for key in caseNameSection . getchildren ( ) : name = key . get ( "name" ) s = Scenario ( name ) retBank . addScenario ( s ) for s in sections : sectionNames = s . get ( "name" ) . split ( "|" ) scenarioName = sectionNames [ 0 ] playerID = int ( re . findall ( "P(\d+)" , sectionNames [ 1 ] ) . pop ( ) ) dataType = sectionNames [ 2 ] scenario = retBank [ scenarioName ] if dataType == "UnitIndex" : for key in s . getchildren ( ) : tag = key . get ( "name" ) scenario . addUnit ( tag , allowDuplicate = True ) elif dataType == "UnitData" : for key in s . getchildren ( ) : unitMeta = key . get ( "name" ) . split ( "|" ) unitTag = int ( unitMeta [ 0 ] ) unitAttr = unitMeta [ 1 ] . lower ( ) attrs = xmlChildrenToDict ( key ) if len ( attrs ) == 1 and list ( attrs . keys ( ) ) [ 0 ] == "value" : attrs = { unitAttr : attrs [ "value" ] } u = scenario . updateUnit ( unitTag , owner = playerID , ** attrs ) elif dataType == "Upgrades" : for key in s . getchildren ( ) : value = key . get ( "name" ) try : scenario . addUpgrade ( playerID , value ) except NotImplementedError as e : print ( ( "WARNING: cannot load scenario upgrade '%s' : %s" ) % ( value , e ) ) else : print ( "WARNING: skipped unknown data type: %s" % dataType ) if debug : print ( "*" * 80 ) print ( retBank ) print ( "*" * 80 ) for sName , scenario in retBank . scenarios . items ( ) : print ( scenario ) for p in scenario . players . values ( ) : p . display ( ) print ( "*" * 80 ) retBank . initPlayers ( ) return retBank
interpret scenario bank XML data into usable python objects
interpret scenario bank XML data into usable python objects
"""interpret scenario bank XML data into usable python objects""" # strip path and extension # should be the first section, but 'search' anyway # must define the "TestCases" section # this meta section doesn't define scenario + player data # acquire each scenario's meta data # iterate all scenario + player sections for data # ensure all tags are represented as units in this scenario # duplicates are allowed because XML scenario data can be unordered # load unit data for all players in this scenario # ensure all tags are represented as units in this scenario
xmlpath
debug