<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> import logging <EOL> log = logging . getLogger ( __name__ ) <EOL> log . setLevel ( logging . ERROR ) <EOL> class _SimpleTypes ( object ) : <EOL> def __init__ ( self ) : <EOL> self . _valid = { "<STR_LIT>" : self . _load_valid_data_format_types ( ) , <EOL> "<STR_LIT>" : self . _load_valid_up_or_down ( ) , <EOL> "<STR_LIT>" : self . _load_valid_data_types ( ) } <EOL> @ staticmethod <EOL> def _load_valid_data_types ( ) : <EOL> valid = [ "<STR_LIT>" , <EOL> "<STR_LIT:image>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ] <EOL> return valid <EOL> @ staticmethod <EOL> def _load_valid_data_format_types ( ) : <EOL> import mimetypes <EOL> valid = [ "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ] <EOL> valid_mime_types = list ( mimetypes . types_map . values ( ) ) <EOL> valid . extend ( valid_mime_types ) <EOL> return valid <EOL> @ staticmethod <EOL> def _load_valid_up_or_down ( ) : <EOL> return [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> def handle_upOrDown ( self , element ) : <EOL> type_name = "<STR_LIT>" <EOL> valid = self . _valid [ type_name ] <EOL> for attrib in element . attrib : <EOL> attr = attrib <EOL> val = element . attrib [ attr ] <EOL> if val not in valid : <EOL> log . warning ( "<STR_LIT>" , <EOL> val , type_name , valid ) <EOL> return { attr : val } <EOL> def handle_dataFormat ( self , element ) : <EOL> type_name = "<STR_LIT>" <EOL> valid = self . _valid [ type_name ] <EOL> val = element . text <EOL> if val not in valid : <EOL> log . warning ( "<STR_LIT>" , <EOL> val , type_name , valid ) <EOL> return { type_name : val } <EOL> def handle_dataType ( self , element ) : <EOL> type_name = "<STR_LIT>" <EOL> valid = self . _valid [ type_name ] <EOL> val = element . text <EOL> if val . lower ( ) not in valid : <EOL> log . warning ( "<STR_LIT>" , <EOL> val , type_name , valid ) <EOL> return { type_name : val } <EOL> class _ComplexTypes ( object ) : <EOL> @ staticmethod <EOL> def _get_tag_name ( element ) : <EOL> if "<STR_LIT:}>" in element . tag : <EOL> element_name = element . tag . split ( '<STR_LIT:}>' ) [ - <NUM_LIT:1> ] <EOL> else : <EOL> element_name = element . tag <EOL> return element_name <EOL> @ staticmethod <EOL> def _spatial_range_req_children ( ) : <EOL> return [ "<STR_LIT:start>" , "<STR_LIT:size>" ] <EOL> @ staticmethod <EOL> def _spatial_range_opt_children ( ) : <EOL> return [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> @ staticmethod <EOL> def _date_type_formatted_valid_attrs ( ) : <EOL> return [ "<STR_LIT>" , "<STR_LIT:type>" ] <EOL> @ staticmethod <EOL> def _controlled_vocatulary_opt_attrs ( ) : <EOL> return [ "<STR_LIT>" ] <EOL> @ staticmethod <EOL> def _variable_opt_attrs ( ) : <EOL> return [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> @ staticmethod <EOL> def _variable_req_attrs ( ) : <EOL> return [ "<STR_LIT:name>" ] <EOL> @ staticmethod <EOL> def _variables_opt_attrs ( ) : <EOL> return [ "<STR_LIT>" ] <EOL> @ staticmethod <EOL> def _data_size_req_attrs ( ) : <EOL> return [ "<STR_LIT>" ] <EOL> def handle_spatialRange ( self , element ) : <EOL> type_name = "<STR_LIT>" <EOL> req_children = self . _spatial_range_req_children ( ) <EOL> opt_children = self . _spatial_range_opt_children ( ) <EOL> valid = req_children + opt_children <EOL> spatial_range = { } <EOL> for child in element : <EOL> child_name = child . tag <EOL> if child_name in valid : <EOL> if child_name != "<STR_LIT>" : <EOL> spatial_range [ child . tag ] = float ( child . text ) <EOL> else : <EOL> spatial_range [ child . tag ] = child . text <EOL> else : <EOL> log . warning ( "<STR_LIT>" , <EOL> child_name , type_name ) <EOL> return spatial_range <EOL> def handle_controlledVocabulary ( self , element ) : <EOL> type_name = "<STR_LIT>" <EOL> opt_attrs = self . _controlled_vocatulary_opt_attrs ( ) <EOL> val = { } <EOL> for attr in element . attrib : <EOL> if attr not in opt_attrs : <EOL> log . warning ( "<STR_LIT>" , type_name , <EOL> attr ) <EOL> else : <EOL> val [ attr ] = element . attrib [ attr ] <EOL> name = element . text <EOL> tmp = { "<STR_LIT:name>" : name } <EOL> if val : <EOL> tmp . update ( val ) <EOL> return tmp <EOL> def handle_dateTypeFormatted ( self , element ) : <EOL> type_name = "<STR_LIT>" <EOL> valid_attrs = self . _date_type_formatted_valid_attrs ( ) <EOL> val = { } <EOL> for attr in element . attrib : <EOL> if attr not in valid_attrs : <EOL> log . warning ( "<STR_LIT>" , attr , <EOL> type_name ) <EOL> else : <EOL> val [ attr ] = element . attrib [ attr ] <EOL> val [ "<STR_LIT:value>" ] = element . text <EOL> return val <EOL> def handle_sourceType ( self , element ) : <EOL> parsed = { } <EOL> for child in element : <EOL> value = { } <EOL> if child . tag == "<STR_LIT:name>" : <EOL> value = self . handle_controlledVocabulary ( child ) <EOL> elif child . tag == "<STR_LIT>" : <EOL> if "<STR_LIT:url>" in child . attrib : <EOL> value [ "<STR_LIT:url>" ] = child . attrib [ "<STR_LIT:url>" ] <EOL> if "<STR_LIT:email>" in child . attrib : <EOL> value [ "<STR_LIT:email>" ] = child . attrib [ "<STR_LIT:email>" ] <EOL> else : <EOL> log . warning ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> value [ "<STR_LIT:email>" ] = "<STR_LIT>" <EOL> if value : <EOL> parsed . update ( value ) <EOL> return parsed <EOL> def handle_timeCoverageType ( self , element ) : <EOL> parsed = { } <EOL> tags = [ ] <EOL> for child in element : <EOL> tags . append ( child . tag ) <EOL> valid_num_elements = len ( tags ) >= <NUM_LIT:2> & len ( tags ) <= <NUM_LIT:3> <EOL> if valid_num_elements : <EOL> for child in element : <EOL> value = { } <EOL> if child . tag in [ "<STR_LIT:start>" , "<STR_LIT:end>" ] : <EOL> processed = self . handle_dateTypeFormatted ( child ) <EOL> value [ child . tag ] = processed [ "<STR_LIT:value>" ] <EOL> elif child . tag in [ "<STR_LIT>" , "<STR_LIT>" ] : <EOL> value [ child . tag ] = child . text <EOL> parsed . update ( value ) <EOL> else : <EOL> log . warning ( "<STR_LIT>" ) <EOL> return parsed <EOL> def handle_variable ( self , element ) : <EOL> type_name = "<STR_LIT>" <EOL> opt_attrs = self . _variable_opt_attrs ( ) <EOL> req_attrs = self . _variable_req_attrs ( ) <EOL> valid_attrs = opt_attrs + req_attrs <EOL> valid = True <EOL> variable = { } <EOL> for req_attr in req_attrs : <EOL> if req_attr not in element . attrib : <EOL> valid = False <EOL> log . warning ( "<STR_LIT>" , type_name , <EOL> req_attr ) <EOL> if valid : <EOL> if element . text : <EOL> variable [ "<STR_LIT:description>" ] = element . text <EOL> for attr in element . attrib : <EOL> if attr in valid_attrs : <EOL> variable [ attr ] = element . attrib [ attr ] <EOL> return variable <EOL> @ staticmethod <EOL> def handle_variableMap ( element ) : <EOL> type_name = "<STR_LIT>" <EOL> var_map = { } <EOL> for attr in element . attrib : <EOL> var_map [ attr ] = element . attrib [ attr ] <EOL> return var_map <EOL> def handle_variables ( self , element ) : <EOL> type_name = "<STR_LIT>" <EOL> variables = { } <EOL> variable_list = [ ] <EOL> variable_map_list = [ ] <EOL> for child in element : <EOL> child_type = self . _get_tag_name ( child ) <EOL> if child_type == "<STR_LIT>" : <EOL> var = self . handle_variable ( child ) <EOL> variable_list . append ( var ) <EOL> elif child_type == "<STR_LIT>" : <EOL> var_map = self . handle_variableMap ( element ) <EOL> variable_map_list . append ( var_map ) <EOL> opt_attrs = self . _variables_opt_attrs ( ) <EOL> for attr in element . attrib : <EOL> if attr in opt_attrs : <EOL> variables [ attr ] = element . attrib [ attr ] <EOL> if variable_list : <EOL> variables [ "<STR_LIT>" ] = variable_list <EOL> if variable_map_list : <EOL> variables [ "<STR_LIT>" ] = variable_map_list <EOL> return variables <EOL> def handle_dataSize ( self , element ) : <EOL> req_attrs = self . _data_size_req_attrs ( ) <EOL> data_size = { "<STR_LIT:size>" : float ( element . text ) } <EOL> for attr in element . attrib : <EOL> if attr in req_attrs : <EOL> data_size [ attr ] = element . attrib [ attr ] <EOL> return data_size <EOL> class TDSCatalogMetadata ( object ) : <EOL> r"""<STR_LIT>""" <EOL> def __init__ ( self , element , metadata_in = None ) : <EOL> r"""<STR_LIT>""" <EOL> self . _ct = _ComplexTypes ( ) <EOL> self . _st = _SimpleTypes ( ) <EOL> self . _sts = _SimpleTypes . __dict__ <EOL> self . _cts = _ComplexTypes . __dict__ <EOL> inherited = False <EOL> if '<STR_LIT>' in element . attrib : <EOL> inherited = element . attrib [ '<STR_LIT>' ] <EOL> if inherited == '<STR_LIT:true>' : <EOL> inherited = True <EOL> else : <EOL> inherited = False <EOL> if metadata_in and inherited : <EOL> self . metadata = metadata_in <EOL> else : <EOL> self . metadata = { "<STR_LIT>" : inherited } <EOL> element_name = self . _get_tag_name ( element ) <EOL> if element_name == "<STR_LIT>" : <EOL> for child in element : <EOL> self . _parse_element ( child ) <EOL> else : <EOL> self . _parse_element ( element ) <EOL> @ staticmethod <EOL> def _get_tag_name ( element ) : <EOL> if "<STR_LIT:}>" in element . tag : <EOL> element_name = element . tag . split ( '<STR_LIT:}>' ) [ - <NUM_LIT:1> ] <EOL> else : <EOL> element_name = element . tag <EOL> return element_name <EOL> def _get_handler ( self , handler_name ) : <EOL> handler_name = "<STR_LIT>" + handler_name <EOL> if handler_name in self . _cts : <EOL> return getattr ( self . _ct , handler_name ) <EOL> elif handler_name in self . _sts : <EOL> return getattr ( self . _st , handler_name ) <EOL> else : <EOL> msg = "<STR_LIT>" . format ( handler_name ) <EOL> log . error ( msg ) <EOL> def _parse_element ( self , element ) : <EOL> element_name = self . _get_tag_name ( element ) <EOL> parser = { "<STR_LIT>" : self . _parse_documentation , <EOL> "<STR_LIT>" : self . _parse_property , <EOL> "<STR_LIT>" : self . _parse_contributor , <EOL> "<STR_LIT>" : self . _parse_geospatial_coverage , <EOL> "<STR_LIT>" : self . _parse_service_name , <EOL> "<STR_LIT>" : self . _parse_authority , <EOL> "<STR_LIT>" : self . _parse_publisher , <EOL> "<STR_LIT>" : self . _parse_creator , <EOL> "<STR_LIT>" : self . _parse_keyword , <EOL> "<STR_LIT>" : self . _parse_project , <EOL> "<STR_LIT>" : self . _parse_data_format , <EOL> "<STR_LIT>" : self . _parse_data_type , <EOL> "<STR_LIT:date>" : self . _parse_date , <EOL> "<STR_LIT>" : self . _parse_timeCoverage , <EOL> "<STR_LIT>" : self . _parse_variableMap , <EOL> "<STR_LIT>" : self . _parse_variables } <EOL> try : <EOL> parser [ element_name ] ( element ) <EOL> except KeyError : <EOL> log . error ( "<STR_LIT>" , element_name ) <EOL> raise <EOL> def _parse_documentation ( self , element ) : <EOL> xlink_href_attr = '<STR_LIT>' <EOL> xlink_title_attr = '<STR_LIT>' <EOL> known = '<STR_LIT:type>' in element . attrib <EOL> plain_doc = not element . attrib <EOL> md = self . metadata <EOL> md . setdefault ( "<STR_LIT>" , { } ) <EOL> if known or plain_doc : <EOL> if known : <EOL> doc_type = element . attrib [ '<STR_LIT:type>' ] <EOL> else : <EOL> doc_type = "<STR_LIT>" <EOL> md [ "<STR_LIT>" ] . setdefault ( doc_type , [ ] ) . append ( element . text ) <EOL> elif xlink_href_attr in element . attrib : <EOL> title = element . attrib [ xlink_title_attr ] <EOL> href = element . attrib [ xlink_href_attr ] <EOL> xlink = { "<STR_LIT:title>" : title , "<STR_LIT>" : href } <EOL> md [ "<STR_LIT>" ] . setdefault ( '<STR_LIT>' , [ ] ) . append ( xlink ) <EOL> self . metadata = md <EOL> def _parse_property ( self , element ) : <EOL> name = element . attrib [ "<STR_LIT:name>" ] <EOL> value = element . attrib [ "<STR_LIT:value>" ] <EOL> self . metadata . setdefault ( "<STR_LIT>" , { } ) [ name ] = value <EOL> def _parse_contributor ( self , element ) : <EOL> element_type = "<STR_LIT>" <EOL> role = element . attrib [ "<STR_LIT>" ] <EOL> name = element . text <EOL> self . metadata . setdefault ( element_type , { } ) . setdefault ( role , [ ] ) . append ( name ) <EOL> def _parse_geospatial_coverage ( self , element ) : <EOL> element_type = "<STR_LIT>" <EOL> md = { } <EOL> elements = { "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:name>" : "<STR_LIT>" <EOL> } <EOL> attrs = { "<STR_LIT>" : "<STR_LIT>" } <EOL> if element . attrib : <EOL> for attr in element . attrib : <EOL> if attr in attrs : <EOL> handler_name = attrs [ attr ] <EOL> handler = self . _get_handler ( handler_name ) <EOL> value = handler ( element ) <EOL> md . update ( { attr : value } ) <EOL> else : <EOL> log . warning ( "<STR_LIT>" , attr , <EOL> element_type ) <EOL> for child in element : <EOL> child_name = child . tag <EOL> if child_name in elements : <EOL> handler_name = elements [ child_name ] <EOL> handler = self . _get_handler ( handler_name ) <EOL> value = handler ( child ) <EOL> md . update ( value ) <EOL> self . metadata . setdefault ( element_type , [ ] ) . append ( md ) <EOL> def _parse_service_name ( self , element ) : <EOL> element_type = "<STR_LIT>" <EOL> self . metadata [ element_type ] = element . text <EOL> def _parse_authority ( self , element ) : <EOL> element_type = "<STR_LIT>" <EOL> self . metadata . setdefault ( element_type , [ ] ) . append ( element . text ) <EOL> def _parse_publisher ( self , element ) : <EOL> element_type = "<STR_LIT>" <EOL> parsed = self . _ct . handle_sourceType ( element ) <EOL> self . metadata . setdefault ( element_type , [ ] ) . append ( parsed ) <EOL> def _parse_creator ( self , element ) : <EOL> element_type = "<STR_LIT>" <EOL> parsed = self . _ct . handle_sourceType ( element ) <EOL> self . metadata . setdefault ( element_type , [ ] ) . append ( parsed ) <EOL> def _parse_keyword ( self , element ) : <EOL> element_type = "<STR_LIT>" <EOL> parsed = self . _ct . handle_controlledVocabulary ( element ) <EOL> self . metadata . setdefault ( element_type , [ ] ) . append ( parsed ) <EOL> def _parse_project ( self , element ) : <EOL> element_type = "<STR_LIT>" <EOL> parsed = self . _ct . handle_controlledVocabulary ( element ) <EOL> self . metadata . setdefault ( element_type , [ ] ) . append ( parsed ) <EOL> def _parse_data_format ( self , element ) : <EOL> element_type = "<STR_LIT>" <EOL> parsed = self . _st . handle_dataFormat ( element ) <EOL> self . metadata . update ( parsed ) <EOL> def _parse_data_type ( self , element ) : <EOL> element_type = "<STR_LIT>" <EOL> parsed = self . _st . handle_dataType ( element ) <EOL> self . metadata . update ( parsed ) <EOL> def _parse_date ( self , element ) : <EOL> element_type = "<STR_LIT:date>" <EOL> parsed = self . _ct . handle_dateTypeFormatted ( element ) <EOL> self . metadata . setdefault ( element_type , [ ] ) . append ( parsed ) <EOL> def _parse_timeCoverage ( self , element ) : <EOL> element_type = "<STR_LIT>" <EOL> parsed = self . _ct . handle_timeCoverageType ( element ) <EOL> self . metadata . setdefault ( element_type , [ ] ) . append ( parsed ) <EOL> def _parse_variableMap ( self , element ) : <EOL> element_type = "<STR_LIT>" <EOL> parsed = self . _ct . handle_variableMap ( element ) <EOL> self . metadata . setdefault ( element_type , [ ] ) . append ( parsed ) <EOL> def _parse_variables ( self , element ) : <EOL> element_type = "<STR_LIT>" <EOL> parsed = self . _ct . handle_variables ( element ) <EOL> for variable in parsed [ "<STR_LIT>" ] : <EOL> var_name = variable [ "<STR_LIT:name>" ] <EOL> variable . pop ( "<STR_LIT:name>" , None ) <EOL> self . metadata . setdefault ( element_type , { } ) [ var_name ] = variable </s>
