<s> """<STR_LIT>""" <EOL> from __future__ import unicode_literals <EOL> import sys <EOL> if sys . version > '<STR_LIT:3>' : <EOL> basestring = str <EOL> unicode = str <EOL> import logging <EOL> import re <EOL> import time <EOL> import xml . dom . minidom <EOL> from . import __author__ , __copyright__ , __license__ , __version__ <EOL> from . helpers import TYPE_MAP , TYPE_MARSHAL_FN , TYPE_UNMARSHAL_FN , REVERSE_TYPE_MAP , OrderedDict , Date , Decimal <EOL> log = logging . getLogger ( __name__ ) <EOL> class SimpleXMLElement ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , text = None , elements = None , document = None , <EOL> namespace = None , prefix = None , namespaces_map = { } , jetty = False ) : <EOL> """<STR_LIT>""" <EOL> self . __namespaces_map = namespaces_map <EOL> _rx = "<STR_LIT:|>" . join ( namespaces_map . keys ( ) ) <EOL> self . __ns_rx = re . compile ( r"<STR_LIT>" % _rx ) <EOL> self . __ns = namespace <EOL> self . __prefix = prefix <EOL> self . __jetty = jetty <EOL> if text is not None : <EOL> try : <EOL> self . __document = xml . dom . minidom . parseString ( text ) <EOL> except : <EOL> log . error ( text ) <EOL> raise <EOL> self . __elements = [ self . __document . documentElement ] <EOL> else : <EOL> self . __elements = elements <EOL> self . __document = document <EOL> def add_child ( self , name , text = None , ns = True ) : <EOL> """<STR_LIT>""" <EOL> if not ns or self . __ns is False : <EOL> element = self . __document . createElement ( name ) <EOL> else : <EOL> if isinstance ( ns , basestring ) : <EOL> element = self . __document . createElement ( name ) <EOL> if ns : <EOL> element . setAttribute ( "<STR_LIT>" , ns ) <EOL> elif self . __prefix : <EOL> element = self . __document . createElementNS ( self . __ns , "<STR_LIT>" % ( self . __prefix , name ) ) <EOL> else : <EOL> element = self . __document . createElementNS ( self . __ns , name ) <EOL> if text is not None : <EOL> element . appendChild ( self . __document . createTextNode ( text ) ) <EOL> self . _element . appendChild ( element ) <EOL> return SimpleXMLElement ( <EOL> elements = [ element ] , <EOL> document = self . __document , <EOL> namespace = self . __ns , <EOL> prefix = self . __prefix , <EOL> jetty = self . __jetty , <EOL> namespaces_map = self . __namespaces_map <EOL> ) <EOL> def __setattr__ ( self , tag , text ) : <EOL> """<STR_LIT>""" <EOL> if tag . startswith ( "<STR_LIT:_>" ) : <EOL> object . __setattr__ ( self , tag , text ) <EOL> else : <EOL> self . add_child ( tag , text ) <EOL> def __delattr__ ( self , tag ) : <EOL> """<STR_LIT>""" <EOL> elements = [ __element for __element in self . _element . childNodes <EOL> if __element . nodeType == __element . ELEMENT_NODE ] <EOL> for element in elements : <EOL> self . _element . removeChild ( element ) <EOL> def add_comment ( self , data ) : <EOL> """<STR_LIT>""" <EOL> comment = self . __document . createComment ( data ) <EOL> self . _element . appendChild ( comment ) <EOL> def as_xml ( self , filename = None , pretty = False ) : <EOL> """<STR_LIT>""" <EOL> if not pretty : <EOL> return self . __document . toxml ( '<STR_LIT>' ) <EOL> else : <EOL> return self . __document . toprettyxml ( encoding = '<STR_LIT>' ) <EOL> def __repr__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _element . toxml ( '<STR_LIT>' ) <EOL> def get_name ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _element . tagName <EOL> def get_local_name ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _element . localName <EOL> def get_prefix ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _element . prefix <EOL> def get_namespace_uri ( self , ns ) : <EOL> """<STR_LIT>""" <EOL> element = self . _element <EOL> while element is not None and element . attributes is not None : <EOL> try : <EOL> return element . attributes [ '<STR_LIT>' % ns ] . value <EOL> except KeyError : <EOL> element = element . parentNode <EOL> def attributes ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _element . attributes <EOL> def __getitem__ ( self , item ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( item , basestring ) : <EOL> if self . _element . hasAttribute ( item ) : <EOL> return self . _element . attributes [ item ] . value <EOL> elif isinstance ( item , slice ) : <EOL> return list ( self . _element . attributes . items ( ) ) [ item ] <EOL> else : <EOL> element = self . __elements [ item ] <EOL> return SimpleXMLElement ( <EOL> elements = [ element ] , <EOL> document = self . __document , <EOL> namespace = self . __ns , <EOL> prefix = self . __prefix , <EOL> jetty = self . __jetty , <EOL> namespaces_map = self . __namespaces_map <EOL> ) <EOL> def add_attribute ( self , name , value ) : <EOL> """<STR_LIT>""" <EOL> self . _element . setAttribute ( name , value ) <EOL> def __setitem__ ( self , item , value ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( item , basestring ) : <EOL> self . add_attribute ( item , value ) <EOL> elif isinstance ( item , slice ) : <EOL> for k , v in value . items ( ) : <EOL> self . add_attribute ( k , v ) <EOL> def __call__ ( self , tag = None , ns = None , children = False , root = False , <EOL> error = True , ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if root : <EOL> return SimpleXMLElement ( <EOL> elements = [ self . __document . documentElement ] , <EOL> document = self . __document , <EOL> namespace = self . __ns , <EOL> prefix = self . __prefix , <EOL> jetty = self . __jetty , <EOL> namespaces_map = self . __namespaces_map <EOL> ) <EOL> if tag is None : <EOL> return self . __iter__ ( ) <EOL> if children : <EOL> return self . children ( ) <EOL> elements = None <EOL> if isinstance ( tag , int ) : <EOL> elements = [ self . __elements [ tag ] ] <EOL> if ns and not elements : <EOL> for ns_uri in isinstance ( ns , ( tuple , list ) ) and ns or ( ns , ) : <EOL> elements = self . _element . getElementsByTagNameNS ( ns_uri , tag ) <EOL> if elements : <EOL> break <EOL> if self . __ns and not elements : <EOL> elements = self . _element . getElementsByTagNameNS ( self . __ns , tag ) <EOL> if not elements : <EOL> elements = self . _element . getElementsByTagName ( tag ) <EOL> if not elements : <EOL> if error : <EOL> raise AttributeError ( "<STR_LIT>" ) <EOL> else : <EOL> return <EOL> return SimpleXMLElement ( <EOL> elements = elements , <EOL> document = self . __document , <EOL> namespace = self . __ns , <EOL> prefix = self . __prefix , <EOL> jetty = self . __jetty , <EOL> namespaces_map = self . __namespaces_map ) <EOL> except AttributeError as e : <EOL> raise AttributeError ( "<STR_LIT>" % ( tag , e ) ) <EOL> def __getattr__ ( self , tag ) : <EOL> """<STR_LIT>""" <EOL> return self . __call__ ( tag ) <EOL> def __iter__ ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> for __element in self . __elements : <EOL> yield SimpleXMLElement ( <EOL> elements = [ __element ] , <EOL> document = self . __document , <EOL> namespace = self . __ns , <EOL> prefix = self . __prefix , <EOL> jetty = self . __jetty , <EOL> namespaces_map = self . __namespaces_map ) <EOL> except : <EOL> raise <EOL> def __dir__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ node . tagName for node <EOL> in self . _element . childNodes <EOL> if node . nodeType != node . TEXT_NODE ] <EOL> def children ( self ) : <EOL> """<STR_LIT>""" <EOL> elements = [ __element for __element in self . _element . childNodes <EOL> if __element . nodeType == __element . ELEMENT_NODE ] <EOL> if not elements : <EOL> return None <EOL> return SimpleXMLElement ( <EOL> elements = elements , <EOL> document = self . __document , <EOL> namespace = self . __ns , <EOL> prefix = self . __prefix , <EOL> jetty = self . __jetty , <EOL> namespaces_map = self . __namespaces_map <EOL> ) <EOL> def __len__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return len ( self . __elements ) <EOL> def __contains__ ( self , item ) : <EOL> """<STR_LIT>""" <EOL> return self . _element . getElementsByTagName ( item ) <EOL> def __unicode__ ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _element . childNodes : <EOL> rc = "<STR_LIT>" <EOL> for node in self . _element . childNodes : <EOL> if node . nodeType == node . TEXT_NODE : <EOL> rc = rc + node . data <EOL> return rc <EOL> return '<STR_LIT>' <EOL> def __str__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __unicode__ ( ) <EOL> def __int__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return int ( self . __str__ ( ) ) <EOL> def __float__ ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return float ( self . __str__ ( ) ) <EOL> except : <EOL> raise IndexError ( self . _element . toxml ( ) ) <EOL> _element = property ( lambda self : self . __elements [ <NUM_LIT:0> ] ) <EOL> def unmarshall ( self , types , strict = True ) : <EOL> """<STR_LIT>""" <EOL> d = { } <EOL> for node in self ( ) : <EOL> name = str ( node . get_local_name ( ) ) <EOL> ref_name_type = None <EOL> if '<STR_LIT>' in node . attributes ( ) . keys ( ) : <EOL> href = node [ '<STR_LIT>' ] [ <NUM_LIT:1> : ] <EOL> for ref_node in self ( root = True ) ( "<STR_LIT>" ) : <EOL> if ref_node [ '<STR_LIT:id>' ] == href : <EOL> node = ref_node <EOL> ref_name_type = ref_node [ '<STR_LIT>' ] . split ( "<STR_LIT::>" ) [ <NUM_LIT:1> ] <EOL> break <EOL> try : <EOL> if isinstance ( types , dict ) : <EOL> fn = types [ name ] <EOL> if any ( [ k for k , v in node [ : ] if '<STR_LIT>' in k ] ) and not isinstance ( fn , list ) : <EOL> fn = [ fn ] <EOL> else : <EOL> fn = types <EOL> except ( KeyError , ) as e : <EOL> if '<STR_LIT>' in node . attributes ( ) . keys ( ) : <EOL> xsd_type = node [ '<STR_LIT>' ] . split ( "<STR_LIT::>" ) [ <NUM_LIT:1> ] <EOL> try : <EOL> fn = REVERSE_TYPE_MAP [ xsd_type ] <EOL> except : <EOL> fn = None <EOL> elif strict : <EOL> raise TypeError ( "<STR_LIT>" % ( name , ) ) <EOL> else : <EOL> fn = str <EOL> if isinstance ( fn , list ) : <EOL> value = d . setdefault ( name , [ ] ) <EOL> children = node . children ( ) <EOL> if fn and not isinstance ( fn [ <NUM_LIT:0> ] , dict ) : <EOL> for child in ( children or [ ] ) : <EOL> tmp_dict = child . unmarshall ( fn [ <NUM_LIT:0> ] , strict ) <EOL> value . extend ( tmp_dict . values ( ) ) <EOL> elif ( self . __jetty and len ( fn [ <NUM_LIT:0> ] ) > <NUM_LIT:1> ) : <EOL> for parent in node : <EOL> tmp_dict = { } <EOL> for child in ( node . children ( ) or [ ] ) : <EOL> tmp_dict . update ( child . unmarshall ( fn [ <NUM_LIT:0> ] , strict ) ) <EOL> value . append ( tmp_dict ) <EOL> else : <EOL> for child in ( children or [ ] ) : <EOL> value . append ( child . unmarshall ( fn [ <NUM_LIT:0> ] , strict ) ) <EOL> elif isinstance ( fn , tuple ) : <EOL> value = [ ] <EOL> _d = { } <EOL> children = node . children ( ) <EOL> as_dict = len ( fn ) == <NUM_LIT:1> and isinstance ( fn [ <NUM_LIT:0> ] , dict ) <EOL> for child in ( children and children ( ) or [ ] ) : <EOL> if as_dict : <EOL> _d . update ( child . unmarshall ( fn [ <NUM_LIT:0> ] , strict ) ) <EOL> else : <EOL> value . append ( child . unmarshall ( fn [ <NUM_LIT:0> ] , strict ) ) <EOL> if as_dict : <EOL> value . append ( _d ) <EOL> if name in d : <EOL> _tmp = list ( d [ name ] ) <EOL> _tmp . extend ( value ) <EOL> value = tuple ( _tmp ) <EOL> else : <EOL> value = tuple ( value ) <EOL> elif isinstance ( fn , dict ) : <EOL> children = node . children ( ) <EOL> value = children and children . unmarshall ( fn , strict ) <EOL> else : <EOL> if fn is None : <EOL> value = node <EOL> elif unicode ( node ) or ( fn == str and unicode ( node ) != '<STR_LIT>' ) : <EOL> try : <EOL> fn = TYPE_UNMARSHAL_FN . get ( fn , fn ) <EOL> if fn == str : <EOL> value = unicode ( node ) <EOL> else : <EOL> value = fn ( unicode ( node ) ) <EOL> except ( ValueError , TypeError ) as e : <EOL> raise ValueError ( "<STR_LIT>" % ( name , e ) ) <EOL> else : <EOL> value = None <EOL> d [ name ] = value <EOL> return d <EOL> def _update_ns ( self , name ) : <EOL> """<STR_LIT>""" <EOL> pref = self . __ns_rx . search ( name ) <EOL> if pref : <EOL> pref = pref . groups ( ) [ <NUM_LIT:0> ] <EOL> try : <EOL> name = name . replace ( pref , self . __namespaces_map [ pref ] ) <EOL> except KeyError : <EOL> log . warning ( '<STR_LIT>' % name ) <EOL> return name <EOL> def marshall ( self , name , value , add_child = True , add_comments = False , <EOL> ns = False , add_children_ns = True ) : <EOL> """<STR_LIT>""" <EOL> name = self . _update_ns ( name ) <EOL> if isinstance ( value , dict ) : <EOL> child = add_child and self . add_child ( name , ns = ns ) or self <EOL> for k , v in value . items ( ) : <EOL> if not add_children_ns : <EOL> ns = False <EOL> else : <EOL> ns = getattr ( value , '<STR_LIT>' , None ) <EOL> child . marshall ( k , v , add_comments = add_comments , ns = ns ) <EOL> elif isinstance ( value , tuple ) : <EOL> child = add_child and self . add_child ( name , ns = ns ) or self <EOL> if not add_children_ns : <EOL> ns = False <EOL> for k , v in value : <EOL> getattr ( self , name ) . marshall ( k , v , add_comments = add_comments , ns = ns ) <EOL> elif isinstance ( value , list ) : <EOL> child = self . add_child ( name , ns = ns ) <EOL> if not add_children_ns : <EOL> ns = False <EOL> if add_comments : <EOL> child . add_comment ( "<STR_LIT>" ) <EOL> for t in value : <EOL> child . marshall ( name , t , False , add_comments = add_comments , ns = ns ) <EOL> elif isinstance ( value , basestring ) : <EOL> self . add_child ( name , value , ns = ns ) <EOL> elif value is None : <EOL> self . add_child ( name , ns = ns ) <EOL> elif value in TYPE_MAP . keys ( ) : <EOL> child = self . add_child ( name , ns = ns ) <EOL> child . add_comment ( TYPE_MAP [ value ] ) <EOL> else : <EOL> fn = TYPE_MARSHAL_FN . get ( type ( value ) , str ) <EOL> self . add_child ( name , fn ( value ) , ns = ns ) <EOL> def import_node ( self , other ) : <EOL> x = self . __document . importNode ( other . _element , True ) <EOL> self . _element . appendChild ( x ) </s>
