maze_xml.py

'''Maze XML Binding.

Coder@Sonnack.com
September 16, 2014
'''
####################################################################################################
from sys import argv
from logger import loggerinfodebugtrace
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" />'


##================================================================================================##
## MAZE NAVIGATOR
##================================================================================================##
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__ (selfmzrow=0col=0):
        super(MazeXML,self).__init__(mzrowcol)

    def get_xml (self):
        a = []
        a.append(XmlHeader)
        a.append(XmlDocument % 'Cell Dump')
        a.append('<property type="comment" text="Maze Dump..." />')

        # Cells...
        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 = (rxcxcell)
                a.append(XmlCell % t)
            a.append(XmlRow1)
        a.append('</cells>')

        # NS Walls...
        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 = (rxcxcell)
                a.append(XmlWall % t)
            a.append(XmlRow1)
        a.append('</ns_walls>')

        # WE 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 = (rxcxcell)
                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(ta)
        a.append(XmlDocEnd)
        return '\n'.join(a)

    def travel_maze (selfmaze_pathbuff):
        '''Export Maze path tree. NOTE: Maze must be created! [RECURSIVE!]'''
        Log.trace('Node: [%d,%d] (%s)' % (self.curr_rowself.curr_colself.Abbrev[self.direction]))

        buff.append('<node row="%d" col="%d" val="%d">' % (self.curr_rowself.curr_colself.get_curr_cell()))

        if self.curr_row or self.curr_col:
            self.set_curr_cell(4)

        # Can we move left?
        if self.get_left_wall() == 0:
            Log.trace('[%d,%d] (%s) turn left' % (self.curr_rowself.curr_colself.Abbrev[self.direction]))
            t = (self.curr_rowself.curr_colself.direction)
            self.turn_left()
            self.move_to_next_cell()
            maze_path.append(t)
            # *** Recurse! ***
            rv = self.travel_maze(maze_pathbuff)
            self.curr_rowself.curr_colself.direction = maze_path.pop()

        # How about moving forward?
        if self.get_next_wall() == 0:
            Log.trace('[%d,%d] (%s) go straight' % (self.curr_rowself.curr_colself.Abbrev[self.direction]))
            t = (self.curr_rowself.curr_colself.direction)
            self.move_to_next_cell()
            maze_path.append(t)
            # *** Recurse! ***
            rv = self.travel_maze(maze_pathbuff)
            self.curr_rowself.curr_colself.direction = maze_path.pop()

        # Can we at least move right?
        if self.get_right_wall() == 0:
            Log.trace('[%d,%d] (%s) turn right' % (self.curr_rowself.curr_colself.Abbrev[self.direction]))
            t = (self.curr_rowself.curr_colself.direction)
            self.turn_right()
            self.move_to_next_cell()
            maze_path.append(t)
            # *** Recurse! ***
            rv = self.travel_maze(maze_pathbuff)
            self.curr_rowself.curr_colself.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(1010)
    mg = MazeGen1(mz)
    mx = MazeXML(mz)

    # Create a Maze...
    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'''