<s> import re , sys , string <EOL> try : <EOL> unicode ( "<STR_LIT>" ) <EOL> except NameError : <EOL> def encode ( s , encoding ) : <EOL> return s <EOL> _escape = re . compile ( r"<STR_LIT>" ) <EOL> else : <EOL> def encode ( s , encoding ) : <EOL> return s . encode ( encoding ) <EOL> _escape = re . compile ( eval ( r'<STR_LIT>' ) ) <EOL> def encode_entity ( text , pattern = _escape ) : <EOL> def escape_entities ( m ) : <EOL> out = [ ] <EOL> for char in m . group ( ) : <EOL> out . append ( "<STR_LIT>" % ord ( char ) ) <EOL> return string . join ( out , "<STR_LIT>" ) <EOL> return encode ( pattern . sub ( escape_entities , text ) , "<STR_LIT:ascii>" ) <EOL> del _escape <EOL> def escape_cdata ( s , encoding = None , replace = string . replace ) : <EOL> s = replace ( s , "<STR_LIT:&>" , "<STR_LIT>" ) <EOL> s = replace ( s , "<STR_LIT:<>" , "<STR_LIT>" ) <EOL> s = replace ( s , "<STR_LIT:>>" , "<STR_LIT>" ) <EOL> if encoding : <EOL> try : <EOL> return encode ( s , encoding ) <EOL> except UnicodeError : <EOL> return encode_entity ( s ) <EOL> return s <EOL> def escape_attrib ( s , encoding = None , replace = string . replace ) : <EOL> s = replace ( s , "<STR_LIT:&>" , "<STR_LIT>" ) <EOL> s = replace ( s , "<STR_LIT:'>" , "<STR_LIT>" ) <EOL> s = replace ( s , "<STR_LIT>" , "<STR_LIT>" ) <EOL> s = replace ( s , "<STR_LIT:<>" , "<STR_LIT>" ) <EOL> s = replace ( s , "<STR_LIT:>>" , "<STR_LIT>" ) <EOL> if encoding : <EOL> try : <EOL> return encode ( s , encoding ) <EOL> except UnicodeError : <EOL> return encode_entity ( s ) <EOL> return s <EOL> class XMLWriter : <EOL> def __init__ ( self , file , encoding = "<STR_LIT>" ) : <EOL> if not hasattr ( file , "<STR_LIT>" ) : <EOL> file = open ( file , "<STR_LIT:w>" ) <EOL> self . __write = file . write <EOL> if hasattr ( file , "<STR_LIT>" ) : <EOL> self . flush = file . flush <EOL> self . __open = <NUM_LIT:0> <EOL> self . __tags = [ ] <EOL> self . __data = [ ] <EOL> self . __encoding = encoding <EOL> def __flush ( self ) : <EOL> if self . __open : <EOL> self . __write ( "<STR_LIT:>>" ) <EOL> self . __open = <NUM_LIT:0> <EOL> if self . __data : <EOL> data = string . join ( self . __data , "<STR_LIT>" ) <EOL> self . __write ( escape_cdata ( data , self . __encoding ) ) <EOL> self . __data = [ ] <EOL> def declaration ( self ) : <EOL> encoding = self . __encoding <EOL> if encoding == "<STR_LIT>" or encoding == "<STR_LIT:utf-8>" : <EOL> self . __write ( "<STR_LIT>" ) <EOL> else : <EOL> self . __write ( "<STR_LIT>" % encoding ) <EOL> def start ( self , tag , attrib = { } , ** extra ) : <EOL> self . __flush ( ) <EOL> tag = escape_cdata ( tag , self . __encoding ) <EOL> self . __data = [ ] <EOL> self . __tags . append ( tag ) <EOL> self . __write ( "<STR_LIT>" % tag ) <EOL> if attrib or extra : <EOL> attrib = attrib . copy ( ) <EOL> attrib . update ( extra ) <EOL> attrib = attrib . items ( ) <EOL> attrib . sort ( ) <EOL> for k , v in attrib : <EOL> k = escape_cdata ( k , self . __encoding ) <EOL> v = escape_attrib ( v , self . __encoding ) <EOL> self . __write ( "<STR_LIT>" % ( k , v ) ) <EOL> self . __open = <NUM_LIT:1> <EOL> return len ( self . __tags ) - <NUM_LIT:1> <EOL> def comment ( self , comment ) : <EOL> self . __flush ( ) <EOL> self . __write ( "<STR_LIT>" % escape_cdata ( comment , self . __encoding ) ) <EOL> def data ( self , text ) : <EOL> self . __data . append ( text ) <EOL> def end ( self , tag = None ) : <EOL> if tag : <EOL> assert self . __tags , "<STR_LIT>" % tag <EOL> assert escape_cdata ( tag , self . __encoding ) == self . __tags [ - <NUM_LIT:1> ] , "<STR_LIT>" % ( self . __tags [ - <NUM_LIT:1> ] , tag ) <EOL> else : <EOL> assert self . __tags , "<STR_LIT>" <EOL> tag = self . __tags . pop ( ) <EOL> if self . __data : <EOL> self . __flush ( ) <EOL> elif self . __open : <EOL> self . __open = <NUM_LIT:0> <EOL> self . __write ( "<STR_LIT>" ) <EOL> return <EOL> self . __write ( "<STR_LIT>" % tag ) <EOL> def close ( self , id ) : <EOL> while len ( self . __tags ) > id : <EOL> self . end ( ) <EOL> def element ( self , tag , text = None , attrib = { } , ** extra ) : <EOL> apply ( self . start , ( tag , attrib ) , extra ) <EOL> if text : <EOL> self . data ( text ) <EOL> self . end ( ) <EOL> def flush ( self ) : <EOL> pass </s>
