def __init__(self, mapname='blankworld'): # Instanciate the map self.map = sandbox_map(mapname)
def fromXML(self, doc, scenario): '''! \brief Either parse a XML scenario document at the scenario node level. ''' # Name and file system self.OS['gametag'] = doc.Get(scenario,'name') self.OS['savepath'] = os.path.join(os.getcwd(),'Simulations',self.OS['gametag'].replace(' ','_')) if doc.Get(scenario,'reset'): self.fileStructure(self.OS['gametag']) # Set clock self.clock = doc.Get(scenario, 'clock') self.lastpulse = doc.Get(scenario, 'clock') # Map x = doc.Get(scenario,'map') self.map = sandbox_map(x) # Infrastructure to load inf = doc.Get(scenario,'infrastructures') if inf: self.serializeinfrastructure = True # Make a Copy of this node to the savegame if not os.path.exists(self.OS['savegame'],'infrastructure.xml'): fout = open(os.path.exists(self.OS['savegame'],'infrastructure.xml'),'w') fout.write('<?xml version="1.0"?>\n') fout.write(doc.WriteNode(inf)) fout.close() # Read the data if doc.Get(inf,'default') == 1: # Load the map definition infrastructure if os.access(self.map.infrastructurefile,os.F_OK): self.network.LoadFromXML(sandboXML(read=self.map.infrastructurefile)) else: raise 'DefaultInfrastructureNotFound' for n in doc.Get(inf, 'network', True): # Case of import network nodes if doc.Get(n,'import'): fname = os.path.join(os.getcwd(),'scenario',doc.Get(n,'import')) if not fname.endswith('.xml'): fname += '.xml' if os.access(fname,os.F_OK): self.network.LoadFromXML(sandboXML(fname)) else: # Directly load node into infrastructure self.network.LoadFromXML(doc, n) else: # Load the map definition infrastructure if os.access(self.map.infrastructurefile,os.F_OK): self.network.LoadFromXML(sandboXML(read=self.map.infrastructurefile)) else: raise 'DefaultInfrastructureNotFound' # Load sides and OOB for side in doc.Get(scenario, 'side', True): self.LoadSide(doc, side) # Check for an execute node exe = doc.Get(scenario, 'execute') if exe: for cmd in doc.Get(exe, 'cmd', True): # A list of command methodname = doc.Get(cmd, 'method') # If the method exists, call it right away. if hasattr(self, methodname): getattr(self, methodname)() else: raise SandboxException('ExecuteScenarioError',methodname)