<s> from __future__ import division <EOL> import os <EOL> from xml . dom import minidom , Node <EOL> from auto_gen_objects import Object , Property , Choice <EOL> class AutoGenParser ( object ) : <EOL> def __init__ ( self ) : <EOL> pass <EOL> def parse ( self , dir ) : <EOL> objects = { } <EOL> for file in os . listdir ( dir ) : <EOL> if file . lower ( ) . endswith ( '<STR_LIT>' ) : <EOL> filename = os . path . join ( dir , file ) <EOL> dom = minidom . parse ( filename ) <EOL> domObjects = dom . getElementsByTagName ( '<STR_LIT:object>' ) <EOL> for node in domObjects : <EOL> curObject = self . parseObject ( node ) <EOL> objects [ curObject . getName ( ) ] = curObject <EOL> for obj in objects . itervalues ( ) : <EOL> for prop in obj . properties : <EOL> if prop . getReference ( ) : <EOL> try : <EOL> prop . setReferencedObject ( objects [ prop . getReference ( ) ] ) <EOL> except KeyError : <EOL> print '<STR_LIT>' , prop . getReference ( ) <EOL> for choice in obj . choices : <EOL> for prop in choice . properties : <EOL> if prop . getReference ( ) : <EOL> try : <EOL> prop . setReferencedObject ( <EOL> objects [ prop . getReference ( ) ] ) <EOL> except KeyError : <EOL> print '<STR_LIT>' , prop . getReference ( ) <EOL> return objects . values ( ) <EOL> def parseObject ( self , node ) : <EOL> params = { } <EOL> properties = [ ] <EOL> choices = [ ] <EOL> layouts = None <EOL> for attr in node . attributes . keys ( ) : <EOL> params [ attr ] = node . attributes . get ( attr ) . value <EOL> for child in node . childNodes : <EOL> if child . nodeType == Node . ELEMENT_NODE : <EOL> if child . nodeName == '<STR_LIT>' : <EOL> layouts = self . parseLayouts ( child ) <EOL> elif child . nodeName == '<STR_LIT>' : <EOL> property = self . parseProperty ( child ) <EOL> properties . append ( property ) <EOL> elif child . nodeName == '<STR_LIT>' : <EOL> choice = self . parseChoice ( child ) <EOL> choices . append ( choice ) <EOL> return Object ( params , properties , layouts , choices ) <EOL> def parseLayouts ( self , node ) : <EOL> layouts = { } <EOL> for child in node . childNodes : <EOL> if child . nodeType == Node . ELEMENT_NODE : <EOL> layouts [ child . nodeName ] = self . parseDataToDict ( child ) <EOL> return layouts <EOL> def parseProperty ( self , node ) : <EOL> params = { } <EOL> specs = { } <EOL> for attr in node . attributes . keys ( ) : <EOL> params [ attr ] = node . attributes . get ( attr ) . value <EOL> for child in node . childNodes : <EOL> if child . nodeType == Node . ELEMENT_NODE : <EOL> specs [ child . nodeName ] = self . parseDataToDict ( child ) <EOL> return Property ( params , specs ) <EOL> def parseChoice ( self , node ) : <EOL> params = { } <EOL> properties = [ ] <EOL> for attr in node . attributes . keys ( ) : <EOL> params [ attr ] = node . attributes . get ( attr ) . value <EOL> for child in node . childNodes : <EOL> if child . nodeType == Node . ELEMENT_NODE and child . nodeName == '<STR_LIT>' : <EOL> properties . append ( self . parseProperty ( child ) ) <EOL> return Choice ( params , properties ) <EOL> def parseDataToDict ( self , node ) : <EOL> dict = { } <EOL> if node . nodeType == Node . ELEMENT_NODE : <EOL> for attr in node . attributes . keys ( ) : <EOL> dict [ attr ] = node . attributes . get ( attr ) . value <EOL> for child in node . childNodes : <EOL> if child . nodeType == Node . ELEMENT_NODE : <EOL> dict [ child . nodeName ] = child . childNodes [ <NUM_LIT:0> ] . data <EOL> return dict </s>
