<s> __author__ = '<STR_LIT>' <EOL> import inspect <EOL> try : <EOL> from xml . etree import cElementTree as ElementTree <EOL> except ImportError : <EOL> try : <EOL> import cElementTree as ElementTree <EOL> except ImportError : <EOL> try : <EOL> from xml . etree import ElementTree <EOL> except ImportError : <EOL> from elementtree import ElementTree <EOL> STRING_ENCODING = '<STR_LIT:utf-8>' <EOL> class XmlElement ( object ) : <EOL> """<STR_LIT>""" <EOL> _qname = None <EOL> _other_elements = None <EOL> _other_attributes = None <EOL> _rule_set = None <EOL> _members = None <EOL> text = None <EOL> def __init__ ( self , text = None , * args , ** kwargs ) : <EOL> if ( '<STR_LIT>' not in self . __class__ . __dict__ <EOL> or self . __class__ . _members is None ) : <EOL> self . __class__ . _members = tuple ( self . __class__ . _list_xml_members ( ) ) <EOL> for member_name , member_type in self . __class__ . _members : <EOL> if member_name in kwargs : <EOL> setattr ( self , member_name , kwargs [ member_name ] ) <EOL> else : <EOL> if isinstance ( member_type , list ) : <EOL> setattr ( self , member_name , [ ] ) <EOL> else : <EOL> setattr ( self , member_name , None ) <EOL> self . _other_elements = [ ] <EOL> self . _other_attributes = { } <EOL> if text is not None : <EOL> self . text = text <EOL> def _list_xml_members ( cls ) : <EOL> """<STR_LIT>""" <EOL> members = [ ] <EOL> for pair in inspect . getmembers ( cls ) : <EOL> if not pair [ <NUM_LIT:0> ] . startswith ( '<STR_LIT:_>' ) and pair [ <NUM_LIT:0> ] != '<STR_LIT:text>' : <EOL> member_type = pair [ <NUM_LIT:1> ] <EOL> if ( isinstance ( member_type , tuple ) or isinstance ( member_type , list ) <EOL> or isinstance ( member_type , ( str , unicode ) ) <EOL> or ( inspect . isclass ( member_type ) <EOL> and issubclass ( member_type , XmlElement ) ) ) : <EOL> members . append ( pair ) <EOL> return members <EOL> _list_xml_members = classmethod ( _list_xml_members ) <EOL> def _get_rules ( cls , version ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' not in cls . __dict__ or cls . _rule_set is None : <EOL> cls . _rule_set = [ None , None ] <EOL> if version > <NUM_LIT:2> : <EOL> return cls . _get_rules ( <NUM_LIT:2> ) <EOL> if cls . _rule_set [ version - <NUM_LIT:1> ] is None : <EOL> elements = { } <EOL> attributes = { } <EOL> if ( '<STR_LIT>' not in cls . __dict__ or cls . _members is None ) : <EOL> cls . _members = tuple ( cls . _list_xml_members ( ) ) <EOL> for member_name , target in cls . _members : <EOL> if isinstance ( target , list ) : <EOL> elements [ _get_qname ( target [ <NUM_LIT:0> ] , version ) ] = ( member_name , target [ <NUM_LIT:0> ] , <EOL> True ) <EOL> elif isinstance ( target , tuple ) : <EOL> if version <= len ( target ) : <EOL> attributes [ target [ version - <NUM_LIT:1> ] ] = member_name <EOL> else : <EOL> attributes [ target [ - <NUM_LIT:1> ] ] = member_name <EOL> elif isinstance ( target , ( str , unicode ) ) : <EOL> attributes [ target ] = member_name <EOL> elif issubclass ( target , XmlElement ) : <EOL> elements [ _get_qname ( target , version ) ] = ( member_name , target , False ) <EOL> version_rules = ( _get_qname ( cls , version ) , elements , attributes ) <EOL> cls . _rule_set [ version - <NUM_LIT:1> ] = version_rules <EOL> return version_rules <EOL> else : <EOL> return cls . _rule_set [ version - <NUM_LIT:1> ] <EOL> _get_rules = classmethod ( _get_rules ) <EOL> def get_elements ( self , tag = None , namespace = None , version = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> matches = [ ] <EOL> ignored1 , elements , ignored2 = self . __class__ . _get_rules ( version ) <EOL> if elements : <EOL> for qname , element_def in elements . iteritems ( ) : <EOL> member = getattr ( self , element_def [ <NUM_LIT:0> ] ) <EOL> if member : <EOL> if _qname_matches ( tag , namespace , qname ) : <EOL> if element_def [ <NUM_LIT:2> ] : <EOL> matches . extend ( member ) <EOL> else : <EOL> matches . append ( member ) <EOL> for element in self . _other_elements : <EOL> if _qname_matches ( tag , namespace , element . _qname ) : <EOL> matches . append ( element ) <EOL> return matches <EOL> GetElements = get_elements <EOL> FindExtensions = get_elements <EOL> FindChildren = get_elements <EOL> def get_attributes ( self , tag = None , namespace = None , version = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> matches = [ ] <EOL> ignored1 , ignored2 , attributes = self . __class__ . _get_rules ( version ) <EOL> if attributes : <EOL> for qname , attribute_def in attributes . iteritems ( ) : <EOL> member = getattr ( self , attribute_def [ <NUM_LIT:0> ] ) <EOL> if member : <EOL> if _qname_matches ( tag , namespace , qname ) : <EOL> matches . append ( XmlAttribute ( qname , member ) ) <EOL> for qname , value in self . _other_attributes . iteritems ( ) : <EOL> if _qname_matches ( tag , namespace , qname ) : <EOL> matches . append ( XmlAttribute ( qname , value ) ) <EOL> return matches <EOL> GetAttributes = get_attributes <EOL> def _harvest_tree ( self , tree , version = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> qname , elements , attributes = self . __class__ . _get_rules ( version ) <EOL> for element in tree : <EOL> if elements and element . tag in elements : <EOL> definition = elements [ element . tag ] <EOL> if definition [ <NUM_LIT:2> ] : <EOL> if getattr ( self , definition [ <NUM_LIT:0> ] ) is None : <EOL> setattr ( self , definition [ <NUM_LIT:0> ] , [ ] ) <EOL> getattr ( self , definition [ <NUM_LIT:0> ] ) . append ( _xml_element_from_tree ( element , <EOL> definition [ <NUM_LIT:1> ] , version ) ) <EOL> else : <EOL> setattr ( self , definition [ <NUM_LIT:0> ] , _xml_element_from_tree ( element , <EOL> definition [ <NUM_LIT:1> ] , version ) ) <EOL> else : <EOL> self . _other_elements . append ( _xml_element_from_tree ( element , XmlElement , <EOL> version ) ) <EOL> for attrib , value in tree . attrib . iteritems ( ) : <EOL> if attributes and attrib in attributes : <EOL> setattr ( self , attributes [ attrib ] , value ) <EOL> else : <EOL> self . _other_attributes [ attrib ] = value <EOL> if tree . text : <EOL> self . text = tree . text <EOL> def _to_tree ( self , version = <NUM_LIT:1> , encoding = None ) : <EOL> new_tree = ElementTree . Element ( _get_qname ( self , version ) ) <EOL> self . _attach_members ( new_tree , version , encoding ) <EOL> return new_tree <EOL> def _attach_members ( self , tree , version = <NUM_LIT:1> , encoding = None ) : <EOL> """<STR_LIT>""" <EOL> qname , elements , attributes = self . __class__ . _get_rules ( version ) <EOL> encoding = encoding or STRING_ENCODING <EOL> if elements : <EOL> for tag , element_def in elements . iteritems ( ) : <EOL> member = getattr ( self , element_def [ <NUM_LIT:0> ] ) <EOL> if member and element_def [ <NUM_LIT:2> ] : <EOL> for instance in member : <EOL> instance . _become_child ( tree , version ) <EOL> elif member : <EOL> member . _become_child ( tree , version ) <EOL> if attributes : <EOL> for attribute_tag , member_name in attributes . iteritems ( ) : <EOL> value = getattr ( self , member_name ) <EOL> if value : <EOL> tree . attrib [ attribute_tag ] = value <EOL> for element in self . _other_elements : <EOL> element . _become_child ( tree , version ) <EOL> for key , value in self . _other_attributes . iteritems ( ) : <EOL> if not isinstance ( value , unicode ) : <EOL> value = value . decode ( encoding ) <EOL> tree . attrib [ key ] = value <EOL> if self . text : <EOL> if isinstance ( self . text , unicode ) : <EOL> tree . text = self . text <EOL> else : <EOL> tree . text = self . text . decode ( encoding ) <EOL> def to_string ( self , version = <NUM_LIT:1> , encoding = None ) : <EOL> """<STR_LIT>""" <EOL> return ElementTree . tostring ( self . _to_tree ( version , encoding ) ) <EOL> ToString = to_string <EOL> def __str__ ( self ) : <EOL> return self . to_string ( ) <EOL> def _become_child ( self , tree , version = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> new_child = ElementTree . Element ( '<STR_LIT>' ) <EOL> tree . append ( new_child ) <EOL> new_child . tag = _get_qname ( self , version ) <EOL> self . _attach_members ( new_child , version ) <EOL> def __get_extension_elements ( self ) : <EOL> return self . _other_elements <EOL> def __set_extension_elements ( self , elements ) : <EOL> self . _other_elements = elements <EOL> extension_elements = property ( __get_extension_elements , <EOL> __set_extension_elements , <EOL> """<STR_LIT>""" ) <EOL> def __get_extension_attributes ( self ) : <EOL> return self . _other_attributes <EOL> def __set_extension_attributes ( self , attributes ) : <EOL> self . _other_attributes = attributes <EOL> extension_attributes = property ( __get_extension_attributes , <EOL> __set_extension_attributes , <EOL> """<STR_LIT>""" ) <EOL> def _get_tag ( self , version = <NUM_LIT:1> ) : <EOL> qname = _get_qname ( self , version ) <EOL> return qname [ qname . find ( '<STR_LIT:}>' ) + <NUM_LIT:1> : ] <EOL> def _get_namespace ( self , version = <NUM_LIT:1> ) : <EOL> qname = _get_qname ( self , version ) <EOL> if qname . startswith ( '<STR_LIT:{>' ) : <EOL> return qname [ <NUM_LIT:1> : qname . find ( '<STR_LIT:}>' ) ] <EOL> else : <EOL> return None <EOL> def _set_tag ( self , tag ) : <EOL> if isinstance ( self . _qname , tuple ) : <EOL> self . _qname = self . _qname . copy ( ) <EOL> if self . _qname [ <NUM_LIT:0> ] . startswith ( '<STR_LIT:{>' ) : <EOL> self . _qname [ <NUM_LIT:0> ] = '<STR_LIT>' % ( self . _get_namespace ( <NUM_LIT:1> ) , tag ) <EOL> else : <EOL> self . _qname [ <NUM_LIT:0> ] = tag <EOL> else : <EOL> if self . _qname . startswith ( '<STR_LIT:{>' ) : <EOL> self . _qname = '<STR_LIT>' % ( self . _get_namespace ( ) , tag ) <EOL> else : <EOL> self . _qname = tag <EOL> def _set_namespace ( self , namespace ) : <EOL> if isinstance ( self . _qname , tuple ) : <EOL> self . _qname = self . _qname . copy ( ) <EOL> if namespace : <EOL> self . _qname [ <NUM_LIT:0> ] = '<STR_LIT>' % ( namespace , self . _get_tag ( <NUM_LIT:1> ) ) <EOL> else : <EOL> self . _qname [ <NUM_LIT:0> ] = self . _get_tag ( <NUM_LIT:1> ) <EOL> else : <EOL> if namespace : <EOL> self . _qname = '<STR_LIT>' % ( namespace , self . _get_tag ( <NUM_LIT:1> ) ) <EOL> else : <EOL> self . _qname = self . _get_tag ( <NUM_LIT:1> ) <EOL> tag = property ( _get_tag , _set_tag , <EOL> """<STR_LIT>""" ) <EOL> namespace = property ( _get_namespace , _set_namespace , <EOL> """<STR_LIT>""" ) <EOL> children = extension_elements <EOL> attributes = extension_attributes <EOL> def _get_qname ( element , version ) : <EOL> if isinstance ( element . _qname , tuple ) : <EOL> if version <= len ( element . _qname ) : <EOL> return element . _qname [ version - <NUM_LIT:1> ] <EOL> else : <EOL> return element . _qname [ - <NUM_LIT:1> ] <EOL> else : <EOL> return element . _qname <EOL> def _qname_matches ( tag , namespace , qname ) : <EOL> """<STR_LIT>""" <EOL> if qname is None : <EOL> member_tag = None <EOL> member_namespace = None <EOL> else : <EOL> if qname . startswith ( '<STR_LIT:{>' ) : <EOL> member_namespace = qname [ <NUM_LIT:1> : qname . index ( '<STR_LIT:}>' ) ] <EOL> member_tag = qname [ qname . index ( '<STR_LIT:}>' ) + <NUM_LIT:1> : ] <EOL> else : <EOL> member_namespace = None <EOL> member_tag = qname <EOL> return ( ( tag is None and namespace is None ) <EOL> or ( namespace is None and member_tag == tag ) <EOL> or ( tag is None and member_namespace == namespace ) <EOL> or ( tag is None and namespace == '<STR_LIT>' <EOL> and member_namespace is None ) <EOL> or ( tag == member_tag <EOL> and namespace == member_namespace ) <EOL> or ( tag == member_tag and namespace == '<STR_LIT>' <EOL> and member_namespace is None ) ) <EOL> def parse ( xml_string , target_class = None , version = <NUM_LIT:1> , encoding = None ) : <EOL> """<STR_LIT>""" <EOL> if target_class is None : <EOL> target_class = XmlElement <EOL> if isinstance ( xml_string , unicode ) : <EOL> if encoding is None : <EOL> xml_string = xml_string . encode ( STRING_ENCODING ) <EOL> else : <EOL> xml_string = xml_string . encode ( encoding ) <EOL> tree = ElementTree . fromstring ( xml_string ) <EOL> return _xml_element_from_tree ( tree , target_class , version ) <EOL> Parse = parse <EOL> xml_element_from_string = parse <EOL> XmlElementFromString = xml_element_from_string <EOL> def _xml_element_from_tree ( tree , target_class , version = <NUM_LIT:1> ) : <EOL> if target_class . _qname is None : <EOL> instance = target_class ( ) <EOL> instance . _qname = tree . tag <EOL> instance . _harvest_tree ( tree , version ) <EOL> return instance <EOL> elif tree . tag == _get_qname ( target_class , version ) : <EOL> instance = target_class ( ) <EOL> instance . _harvest_tree ( tree , version ) <EOL> return instance <EOL> return None <EOL> class XmlAttribute ( object ) : <EOL> def __init__ ( self , qname , value ) : <EOL> self . _qname = qname <EOL> self . value = value </s>
