<s> class XMLObjectType ( type ) : <EOL> def __getitem__ ( cls , item ) : <EOL> return cls ( ) [ item ] <EOL> class XMLObject ( object ) : <EOL> __metaclass__ = XMLObjectType <EOL> collapsable = True <EOL> name = u"<STR_LIT>" <EOL> def __init__ ( self , name , ** options ) : <EOL> self . options = options <EOL> self . name = name <EOL> self . children = [ ] <EOL> def __getitem__ ( self , items ) : <EOL> if isinstance ( items , basestring ) or isinstance ( items , XMLObject ) : <EOL> items = ( items , ) <EOL> for item in items : <EOL> if isinstance ( item , tuple ) or isinstance ( item , list ) : <EOL> self . children . extend ( item ) <EOL> else : <EOL> self . children . append ( item ) <EOL> return self <EOL> def structure ( self ) : <EOL> return self <EOL> def render ( self , depth = <NUM_LIT:0> ) : <EOL> struct = self . structure ( ) <EOL> return struct . render ( depth ) <EOL> def __unicode__ ( self ) : <EOL> return self . render ( None ) <EOL> class XMLTag ( XMLObject ) : <EOL> name = "<STR_LIT>" <EOL> def render ( self , depth = <NUM_LIT:0> ) : <EOL> children = [ ] <EOL> next_depth = depth + <NUM_LIT:1> if self . name else depth <EOL> for child in self . children : <EOL> if isinstance ( child , XMLObject ) : <EOL> child = child . render ( depth + <NUM_LIT:1> ) <EOL> else : <EOL> child = unicode ( child ) <EOL> children . append ( child ) <EOL> children = u"<STR_LIT>" . join ( children ) <EOL> if self . name : <EOL> options = u"<STR_LIT>" . join ( [ u'<STR_LIT>' % ( opt , val ) for ( opt , val ) in self . options . items ( ) ] ) <EOL> if ( not children ) and self . collapsable : <EOL> return u"<STR_LIT>" % ( self . name , options ) <EOL> else : <EOL> return u"<STR_LIT>" % { <EOL> '<STR_LIT:name>' : self . name , '<STR_LIT>' : options , '<STR_LIT>' : children } <EOL> else : <EOL> return children </s>
