<s> """<STR_LIT>""" <EOL> import cgi , sys <EOL> from owslib . util import openURL <EOL> try : <EOL> from urllib import urlencode <EOL> except ImportError : <EOL> from urllib . parse import urlencode <EOL> from owslib . etree import etree <EOL> from owslib . namespaces import Namespaces <EOL> from owslib . util import which_etree , findall <EOL> MYNS = Namespaces ( ) <EOL> XS_NAMESPACE = MYNS . get_namespace ( '<STR_LIT>' ) <EOL> GML_NAMESPACES = ( MYNS . get_namespace ( '<STR_LIT>' ) , <EOL> MYNS . get_namespace ( '<STR_LIT>' ) , <EOL> MYNS . get_namespace ( '<STR_LIT>' ) ) <EOL> def get_schema ( url , typename , version = '<STR_LIT>' , timeout = <NUM_LIT:30> ) : <EOL> """<STR_LIT>""" <EOL> url = _get_describefeaturetype_url ( url , version , typename ) <EOL> res = openURL ( url , timeout = timeout ) <EOL> root = etree . fromstring ( res . read ( ) ) <EOL> type_element = findall ( root , '<STR_LIT>' % XS_NAMESPACE , <EOL> attribute_name = '<STR_LIT:name>' , attribute_value = typename ) [ <NUM_LIT:0> ] <EOL> complex_type = type_element . attrib [ '<STR_LIT:type>' ] . split ( "<STR_LIT::>" ) [ <NUM_LIT:1> ] <EOL> elements = _get_elements ( complex_type , root ) <EOL> nsmap = None <EOL> if hasattr ( root , '<STR_LIT>' ) : <EOL> nsmap = root . nsmap <EOL> return _construct_schema ( elements , nsmap ) <EOL> def _get_elements ( complex_type , root ) : <EOL> """<STR_LIT>""" <EOL> found_elements = [ ] <EOL> element = findall ( root , '<STR_LIT>' % XS_NAMESPACE , <EOL> attribute_name = '<STR_LIT:name>' , attribute_value = complex_type ) [ <NUM_LIT:0> ] <EOL> found_elements = findall ( element , '<STR_LIT>' % XS_NAMESPACE ) <EOL> return found_elements <EOL> def _construct_schema ( elements , nsmap ) : <EOL> """<STR_LIT>""" <EOL> schema = { <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : None <EOL> } <EOL> schema_key = None <EOL> gml_key = None <EOL> if nsmap : <EOL> for key in nsmap : <EOL> if nsmap [ key ] == XS_NAMESPACE : <EOL> schema_key = key <EOL> if nsmap [ key ] in GML_NAMESPACES : <EOL> gml_key = key <EOL> else : <EOL> gml_key = '<STR_LIT>' <EOL> schema_key = '<STR_LIT>' <EOL> for element in elements : <EOL> data_type = element . attrib [ '<STR_LIT:type>' ] . replace ( gml_key + '<STR_LIT::>' , '<STR_LIT>' ) <EOL> name = element . attrib [ '<STR_LIT:name>' ] <EOL> if data_type == '<STR_LIT>' : <EOL> schema [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> elif data_type == '<STR_LIT>' : <EOL> schema [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> elif data_type == '<STR_LIT>' : <EOL> schema [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> elif data_type == '<STR_LIT>' : <EOL> schema [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> elif data_type == '<STR_LIT>' : <EOL> schema [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> elif data_type == '<STR_LIT>' : <EOL> schema [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> elif data_type == '<STR_LIT>' : <EOL> schema [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> elif data_type == '<STR_LIT>' : <EOL> schema [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> elif data_type == '<STR_LIT>' : <EOL> schema [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> else : <EOL> schema [ '<STR_LIT>' ] [ name ] = data_type . replace ( schema_key + '<STR_LIT::>' , '<STR_LIT>' ) <EOL> if schema [ '<STR_LIT>' ] or schema [ '<STR_LIT>' ] : <EOL> return schema <EOL> else : <EOL> return None <EOL> def _get_describefeaturetype_url ( url , version , typename ) : <EOL> """<STR_LIT>""" <EOL> query_string = [ ] <EOL> if url . find ( '<STR_LIT:?>' ) != - <NUM_LIT:1> : <EOL> query_string = cgi . parse_qsl ( url . split ( '<STR_LIT:?>' ) [ <NUM_LIT:1> ] ) <EOL> params = [ x [ <NUM_LIT:0> ] for x in query_string ] <EOL> if '<STR_LIT>' not in params : <EOL> query_string . append ( ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> if '<STR_LIT>' not in params : <EOL> query_string . append ( ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> if '<STR_LIT:version>' not in params : <EOL> query_string . append ( ( '<STR_LIT:version>' , version ) ) <EOL> query_string . append ( ( '<STR_LIT>' , typename ) ) <EOL> urlqs = urlencode ( tuple ( query_string ) ) <EOL> return url . split ( '<STR_LIT:?>' ) [ <NUM_LIT:0> ] + '<STR_LIT:?>' + urlqs </s>
