maze_xml.py
'''Maze XML Binding.
Coder@Sonnack.com
September 16, 2014
'''
from sys import argv
from logger import logger, info, debug, trace
from maze_obj import MazeObject
from maze_nav import MazeNavigator
from maze_gen import MazeGen1
Log = logger('Maze/Xml')
XmlHeader = r'<?xml version="1.0" encoding="UTF-8"?>'
XmlDocument = r'<document application="Maze" type="%s">'
XmlDocEnd = r'</document>'
XmlRow0 = r'<row index="%d">'
XmlRow1 = r'</row>'
XmlCell = r'<cell row="%d" col="%d" val="%d" />'
XmlWall = r'<wall row="%d" col="%d" val="%d" />'
class MazeXML (MazeNavigator):
'''\
Maze XML Binding class.
methods:
get_xml - get row/col XML (emits maze in any state)
get_xml_paths - get maze path XML (requires generated maze)
internal methods:
travel_maze - travels all maze paths [RECURSIVE!]
'''
def __init__ (self, mz, row=0, col=0):
super(MazeXML,self).__init__(mz, row, col)
def get_xml (self):
a = []
a.append(XmlHeader)
a.append(XmlDocument % 'Cell Dump')
a.append('<property type="comment" text="Maze Dump..." />')
a.append('<cells rows="%d" cols="%d">' % (len(self.mz.cells), len(self.mz.cells[0])))
for rx,row in enumerate(self.mz.cells):
a.append(XmlRow0 % rx)
for cx,cell in enumerate(row):
t = (rx, cx, cell)
a.append(XmlCell % t)
a.append(XmlRow1)
a.append('</cells>')
a.append('<ns_walls rows="%d" cols="%d">' % (len(self.mz.ns_walls), len(self.mz.ns_walls[0])))
for rx,row in enumerate(self.mz.ns_walls):
a.append(XmlRow0 % rx)
for cx,cell in enumerate(row):
t = (rx, cx, cell)
a.append(XmlWall % t)
a.append(XmlRow1)
a.append('</ns_walls>')
a.append('<we_walls rows="%d" cols="%d">' % (len(self.mz.we_walls), len(self.mz.we_walls[0])))
for rx,row in enumerate(self.mz.we_walls):
a.append(XmlRow0 % rx)
for cx,cell in enumerate(row):
t = (rx, cx, cell)
a.append(XmlWall % t)
a.append(XmlRow1)
a.append('</we_walls>')
a.append(XmlDocEnd)
return '\n'.join(a)
def get_xml_paths (self):
a = []
a.append(XmlHeader)
a.append(XmlDocument % 'Maze Paths')
a.append('<property type="comment" text="Maze Tree..." />')
t = []
self.reset_curr_cell()
self.travel_maze(t, a)
a.append(XmlDocEnd)
return '\n'.join(a)
def travel_maze (self, maze_path, buff):
'''Export Maze path tree. NOTE: Maze must be created! [RECURSIVE!]'''
Log.trace('Node: [%d,%d] (%s)' % (self.curr_row, self.curr_col, self.Abbrev[self.direction]))
buff.append('<node row="%d" col="%d" val="%d">' % (self.curr_row, self.curr_col, self.get_curr_cell()))
if self.curr_row or self.curr_col:
self.set_curr_cell(4)
if self.get_left_wall() == 0:
Log.trace('[%d,%d] (%s) turn left' % (self.curr_row, self.curr_col, self.Abbrev[self.direction]))
t = (self.curr_row, self.curr_col, self.direction)
self.turn_left()
self.move_to_next_cell()
maze_path.append(t)
rv = self.travel_maze(maze_path, buff)
self.curr_row, self.curr_col, self.direction = maze_path.pop()
if self.get_next_wall() == 0:
Log.trace('[%d,%d] (%s) go straight' % (self.curr_row, self.curr_col, self.Abbrev[self.direction]))
t = (self.curr_row, self.curr_col, self.direction)
self.move_to_next_cell()
maze_path.append(t)
rv = self.travel_maze(maze_path, buff)
self.curr_row, self.curr_col, self.direction = maze_path.pop()
if self.get_right_wall() == 0:
Log.trace('[%d,%d] (%s) turn right' % (self.curr_row, self.curr_col, self.Abbrev[self.direction]))
t = (self.curr_row, self.curr_col, self.direction)
self.turn_right()
self.move_to_next_cell()
maze_path.append(t)
rv = self.travel_maze(maze_path, buff)
self.curr_row, self.curr_col, self.direction = maze_path.pop()
Log.trace('Node Exit')
buff.append('</node>')
if __name__ == '__main__':
print('autorun: %s' % argv[0])
Log.start('maze_xml.log')
Log.level(info())
mz = MazeObject(10, 10)
mg = MazeGen1(mz)
mx = MazeXML(mz)
mg.generate_maze()
mz.print_maze(Log.fp())
xml = mx.get_xml()
Log.info(xml)
fp = open('maze-cells.xml', mode='w', encoding='utf-8')
fp.write(xml)
fp.close()
xml = mx.get_xml_paths()
Log.info(xml)
fp = open('maze-paths.xml', mode='w', encoding='utf-8')
fp.write(xml)
fp.close()
Log.end()
'''eof'''