<s> import inspect as mod_inspect <EOL> import datetime as mod_datetime <EOL> from . import utils as mod_utils <EOL> class GPXFieldTypeConverter : <EOL> def __init__ ( self , from_string , to_string ) : <EOL> self . from_string = from_string <EOL> self . to_string = to_string <EOL> def parse_time ( string ) : <EOL> from . import gpx as mod_gpx <EOL> if not string : <EOL> return None <EOL> if '<STR_LIT:T>' in string : <EOL> string = string . replace ( '<STR_LIT:T>' , '<STR_LIT:U+0020>' ) <EOL> if '<STR_LIT>' in string : <EOL> string = string . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> for date_format in mod_gpx . DATE_FORMATS : <EOL> try : <EOL> return mod_datetime . datetime . strptime ( string , date_format ) <EOL> except ValueError as e : <EOL> pass <EOL> raise GPXException ( '<STR_LIT>' % string ) <EOL> class FloatConverter : <EOL> def __init__ ( self ) : <EOL> self . from_string = lambda string : None if string is None else float ( string . strip ( ) ) <EOL> self . to_string = lambda flt : str ( flt ) <EOL> class IntConverter : <EOL> def __init__ ( self ) : <EOL> self . from_string = lambda string : None if string is None else int ( string . strip ( ) ) <EOL> self . to_string = lambda flt : str ( flt ) <EOL> class TimeConverter : <EOL> def from_string ( self , string ) : <EOL> from . import gpx as mod_gpx <EOL> if not string : <EOL> return None <EOL> if '<STR_LIT:T>' in string : <EOL> string = string . replace ( '<STR_LIT:T>' , '<STR_LIT:U+0020>' ) <EOL> if '<STR_LIT>' in string : <EOL> string = string . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> for date_format in mod_gpx . DATE_FORMATS : <EOL> try : <EOL> return mod_datetime . datetime . strptime ( string , date_format ) <EOL> except ValueError as e : <EOL> pass <EOL> return None <EOL> def to_string ( self , time ) : <EOL> from . import gpx as mod_gpx <EOL> return time . strftime ( mod_gpx . DATE_FORMAT ) if time else None <EOL> INT_TYPE = IntConverter ( ) <EOL> FLOAT_TYPE = FloatConverter ( ) <EOL> TIME_TYPE = TimeConverter ( ) <EOL> class AbstractGPXField : <EOL> def __init__ ( self , attribute_field = None , is_list = None ) : <EOL> self . attribute_field = attribute_field <EOL> self . is_list = is_list <EOL> self . attribute = False <EOL> def from_xml ( self , parser , node , version ) : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> def to_xml ( self , value , version ) : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> class GPXField ( AbstractGPXField ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , tag = None , attribute = None , type = None , possible = None , mandatory = None ) : <EOL> AbstractGPXField . __init__ ( self ) <EOL> self . name = name <EOL> if tag and attribute : <EOL> raise GPXException ( '<STR_LIT>' ) <EOL> if attribute : <EOL> self . tag = None <EOL> self . attribute = name if attribute is True else attribute <EOL> elif tag : <EOL> self . tag = name if tag is True else tag <EOL> self . attribute = None <EOL> else : <EOL> self . tag = name <EOL> self . attribute = None <EOL> self . type_converter = type <EOL> self . possible = possible <EOL> self . mandatory = mandatory <EOL> def from_xml ( self , parser , node , version ) : <EOL> if self . attribute : <EOL> result = parser . get_node_attribute ( node , self . attribute ) <EOL> else : <EOL> __node = parser . get_first_child ( node , self . tag ) <EOL> result = parser . get_node_data ( __node ) <EOL> if result is None : <EOL> if self . mandatory : <EOL> from . import gpx as mod_gpx <EOL> raise mod_gpx . GPXException ( '<STR_LIT>' % ( self . name , self . tag ) ) <EOL> return None <EOL> if self . type_converter : <EOL> try : <EOL> result = self . type_converter . from_string ( result ) <EOL> except Exception as e : <EOL> from . import gpx as mod_gpx <EOL> raise mod_gpx . GPXException ( '<STR_LIT>' % ( self . tag , result , e ) ) <EOL> if self . possible : <EOL> if not ( result in self . possible ) : <EOL> from . import gpx as mod_gpx <EOL> raise mod_gpx . GPXException ( '<STR_LIT>' % ( result , self . possible ) ) <EOL> return result <EOL> def to_xml ( self , value , version ) : <EOL> if not value : <EOL> return '<STR_LIT>' <EOL> if self . attribute : <EOL> return '<STR_LIT>' % ( self . attribute , mod_utils . make_str ( value ) ) <EOL> else : <EOL> if self . type_converter : <EOL> value = self . type_converter . to_string ( value ) <EOL> if isinstance ( self . tag , list ) or isinstance ( self . tag , tuple ) : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> return mod_utils . to_xml ( self . tag , content = value , escape = True ) <EOL> class GPXComplexField ( AbstractGPXField ) : <EOL> def __init__ ( self , name , classs , tag = None , is_list = None ) : <EOL> AbstractGPXField . __init__ ( self , is_list = is_list ) <EOL> self . name = name <EOL> self . tag = tag or name <EOL> self . classs = classs <EOL> def from_xml ( self , parser , node , version ) : <EOL> if self . is_list : <EOL> result = [ ] <EOL> for child_node in parser . get_children ( node ) : <EOL> if parser . get_node_name ( child_node ) == self . tag : <EOL> result . append ( gpx_fields_from_xml ( self . classs , parser , child_node , version ) ) <EOL> return result <EOL> else : <EOL> field_node = parser . get_first_child ( node , self . tag ) <EOL> if field_node is None : <EOL> return None <EOL> return gpx_fields_from_xml ( self . classs , parser , field_node , version ) <EOL> def to_xml ( self , value , version ) : <EOL> if self . is_list : <EOL> result = '<STR_LIT>' <EOL> for obj in value : <EOL> result += gpx_fields_to_xml ( obj , self . tag , version ) <EOL> return result <EOL> else : <EOL> return gpx_fields_to_xml ( value , self . tag , version ) <EOL> class GPXEmailField ( AbstractGPXField ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , tag = None ) : <EOL> self . attribute = False <EOL> self . is_list = False <EOL> self . name = name <EOL> self . tag = tag or name <EOL> def from_xml ( self , parser , node , version ) : <EOL> email_node = parser . get_first_child ( node , self . tag ) <EOL> if email_node is None : <EOL> return None <EOL> email_id = parser . get_node_attribute ( email_node , '<STR_LIT:id>' ) <EOL> email_domain = parser . get_node_attribute ( email_node , '<STR_LIT>' ) <EOL> return '<STR_LIT>' % ( email_id , email_domain ) <EOL> def to_xml ( self , value , version ) : <EOL> if not value : <EOL> return '<STR_LIT>' <EOL> if '<STR_LIT:@>' in value : <EOL> pos = value . find ( '<STR_LIT:@>' ) <EOL> email_id = value [ : pos ] <EOL> email_domain = value [ pos + <NUM_LIT:1> : ] <EOL> else : <EOL> email_id = value <EOL> email_domain = '<STR_LIT>' <EOL> return '<STR_LIT>' % ( self . tag , email_id , email_domain ) <EOL> class GPXExtensionsField ( AbstractGPXField ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , tag = None ) : <EOL> self . attribute = False <EOL> self . name = name <EOL> self . is_list = False <EOL> self . tag = tag or '<STR_LIT>' <EOL> def from_xml ( self , parser , node , version ) : <EOL> result = { } <EOL> if node is None : <EOL> return result <EOL> extensions_node = parser . get_first_child ( node , self . tag ) <EOL> if extensions_node is None : <EOL> return result <EOL> children = parser . get_children ( extensions_node ) <EOL> if children is None : <EOL> return result <EOL> for child in children : <EOL> result [ parser . get_node_name ( child ) ] = parser . get_node_data ( child ) <EOL> return result <EOL> def to_xml ( self , value , version ) : <EOL> if value is None or not value : <EOL> return '<STR_LIT>' <EOL> result = '<STR_LIT>' + self . tag + '<STR_LIT:>>' <EOL> for ext_key , ext_value in value . items ( ) : <EOL> result += mod_utils . to_xml ( ext_key , content = ext_value ) <EOL> result += '<STR_LIT>' + self . tag + '<STR_LIT:>>' <EOL> return result <EOL> def gpx_fields_to_xml ( instance , tag , version , custom_attributes = None ) : <EOL> fields = instance . gpx_10_fields <EOL> if version == '<STR_LIT>' : <EOL> fields = instance . gpx_11_fields <EOL> tag_open = bool ( tag ) <EOL> body = '<STR_LIT>' <EOL> if tag : <EOL> body = '<STR_LIT>' + tag <EOL> if custom_attributes : <EOL> for key , value in custom_attributes . items ( ) : <EOL> body += '<STR_LIT>' % ( key , mod_utils . make_str ( value ) ) <EOL> for gpx_field in fields : <EOL> if isinstance ( gpx_field , str ) : <EOL> if tag_open : <EOL> body += '<STR_LIT:>>' <EOL> tag_open = False <EOL> if gpx_field [ <NUM_LIT:0> ] == '<STR_LIT:/>' : <EOL> body += '<STR_LIT>' % gpx_field <EOL> else : <EOL> body += '<STR_LIT>' % gpx_field <EOL> tag_open = True <EOL> else : <EOL> value = getattr ( instance , gpx_field . name ) <EOL> if gpx_field . attribute : <EOL> body += '<STR_LIT:U+0020>' + gpx_field . to_xml ( value , version ) <EOL> elif value : <EOL> if tag_open : <EOL> body += '<STR_LIT:>>' <EOL> tag_open = False <EOL> xml_value = gpx_field . to_xml ( value , version ) <EOL> if xml_value : <EOL> body += xml_value <EOL> if tag : <EOL> if tag_open : <EOL> body += '<STR_LIT:>>' <EOL> body += '<STR_LIT>' + tag + '<STR_LIT:>>' <EOL> return body <EOL> def gpx_fields_from_xml ( class_or_instance , parser , node , version ) : <EOL> if mod_inspect . isclass ( class_or_instance ) : <EOL> result = class_or_instance ( ) <EOL> else : <EOL> result = class_or_instance <EOL> fields = result . gpx_10_fields <EOL> if version == '<STR_LIT>' : <EOL> fields = result . gpx_11_fields <EOL> node_path = [ node ] <EOL> for gpx_field in fields : <EOL> current_node = node_path [ - <NUM_LIT:1> ] <EOL> if isinstance ( gpx_field , str ) : <EOL> if gpx_field . startswith ( '<STR_LIT:/>' ) : <EOL> node_path . pop ( ) <EOL> else : <EOL> if current_node is None : <EOL> node_path . append ( None ) <EOL> else : <EOL> node_path . append ( parser . get_first_child ( current_node , gpx_field ) ) <EOL> else : <EOL> if current_node is not None : <EOL> value = gpx_field . from_xml ( parser , current_node , version ) <EOL> setattr ( result , gpx_field . name , value ) <EOL> elif gpx_field . attribute : <EOL> value = gpx_field . from_xml ( parser , node , version ) <EOL> setattr ( result , gpx_field . name , value ) <EOL> return result <EOL> def gpx_check_slots_and_default_values ( classs ) : <EOL> """<STR_LIT>""" <EOL> fields = classs . gpx_10_fields + classs . gpx_11_fields <EOL> gpx_field_names = [ ] <EOL> instance = classs ( ) <EOL> try : <EOL> attributes = list ( filter ( lambda x : x [ <NUM_LIT:0> ] != '<STR_LIT:_>' , dir ( instance ) ) ) <EOL> attributes = list ( filter ( lambda x : not callable ( getattr ( instance , x ) ) , attributes ) ) <EOL> attributes = list ( filter ( lambda x : not x . startswith ( '<STR_LIT>' ) , attributes ) ) <EOL> except Exception as e : <EOL> raise Exception ( '<STR_LIT>' % ( classs . __name__ , e ) ) <EOL> attributes . sort ( ) <EOL> slots = list ( classs . __slots__ ) <EOL> slots . sort ( ) <EOL> if attributes != slots : <EOL> raise Exception ( '<STR_LIT>' % ( classs . __name__ , attributes , slots ) ) <EOL> for field in fields : <EOL> if not isinstance ( field , str ) : <EOL> if field . is_list : <EOL> value = [ ] <EOL> else : <EOL> value = None <EOL> try : <EOL> actual_value = getattr ( instance , field . name ) <EOL> except : <EOL> raise Exception ( '<STR_LIT>' % ( classs . __name__ , field . name ) ) <EOL> if value != actual_value : <EOL> raise Exception ( '<STR_LIT>' <EOL> % ( classs . __name__ , field . name , actual_value , value ) ) <EOL> if not field . name in gpx_field_names : <EOL> gpx_field_names . append ( field . name ) <EOL> gpx_field_names = tuple ( gpx_field_names ) <EOL> if not hasattr ( classs , '<STR_LIT>' ) or not classs . __slots__ or classs . __slots__ != gpx_field_names : <EOL> try : slots = classs . __slots__ <EOL> except Exception as e : slots = '<STR_LIT>' % e <EOL> raise Exception ( '<STR_LIT>' % ( classs , slots , gpx_field_names ) ) </s>
