<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import <EOL> from __future__ import print_function <EOL> import redhawk . common . node as N <EOL> import redhawk . common . types as T <EOL> import redhawk . utils . util as U <EOL> from . import writer <EOL> import lxml . etree as ET <EOL> def WriteToXML ( tree ) : <EOL> s = XMLWriter ( ) <EOL> return s . WriteTree ( tree ) <EOL> def WriteToFile ( tree , filename ) : <EOL> s = XMLWriter ( ) <EOL> s . GetTree ( tree ) . write ( filename ) <EOL> return <EOL> class XMLWriter ( writer . Writer ) : <EOL> def __init__ ( self ) : <EOL> return <EOL> def __AddChildToElement ( self , child , parent_element ) : <EOL> """<STR_LIT>""" <EOL> if type ( child ) is list : <EOL> group_node = ET . SubElement ( parent_element , "<STR_LIT>" ) <EOL> for c in child : <EOL> self . __AddChildToElement ( c , group_node ) <EOL> else : <EOL> assert ( isinstance ( child , N . Node ) or child == None ) <EOL> if child == None : <EOL> print ( "<STR_LIT>" ) <EOL> ET . SubElement ( parent_element , "<STR_LIT:None>" ) <EOL> else : <EOL> self . __ConvertToElement ( child , parent_element ) <EOL> return <EOL> def __AddPositionToElement ( self , tree , parent_element ) : <EOL> try : <EOL> line , column , f = tree . position . GetLine ( ) , tree . position . GetColumn ( ) , tree . position . GetFile ( ) <EOL> except AttributeError as e : <EOL> line , column , f = None , None , None <EOL> attributes = { <EOL> '<STR_LIT>' : str ( line ) , <EOL> '<STR_LIT>' : str ( column ) , <EOL> '<STR_LIT:file>' : str ( f ) <EOL> } <EOL> position_element = ET . SubElement ( parent_element , "<STR_LIT>" , attrib = attributes ) <EOL> return <EOL> def __ConvertToElement ( self , tree , parent_element ) : <EOL> """<STR_LIT>""" <EOL> attributes = dict ( [ ( key , val ) for ( key , val ) <EOL> in tree . GetXMLAttributes ( ) [ <NUM_LIT:1> ] . items ( ) if type ( val ) is str ] ) <EOL> print ( attributes ) <EOL> print ( tree . GetName ( ) ) <EOL> node_element = ET . SubElement ( parent_element , tree . GetName ( ) , attrib = attributes ) <EOL> self . __AddPositionToElement ( tree , node_element ) <EOL> self . __AddChildToElement ( tree . GetChildren ( ) , node_element ) <EOL> return <EOL> def WriteTree ( self , tree ) : <EOL> """<STR_LIT>""" <EOL> return ET . tostring ( self . GetTree ( tree ) ) <EOL> def GetTree ( self , tree ) : <EOL> root = ET . Element ( "<STR_LIT>" ) <EOL> self . __ConvertToElement ( tree , root ) <EOL> tree = ET . ElementTree ( root ) <EOL> return tree </s>
