<s> """<STR_LIT>""" <EOL> import sys <EOL> import urllib <EOL> try : <EOL> from urllib . parse import urlparse , urlunparse , urljoin <EOL> except ImportError : <EOL> from urlparse import urlparse , urlunparse , urljoin <EOL> import os . path <EOL> import datetime <EOL> import distutils . dir_util <EOL> import re <EOL> import time <EOL> import logging <EOL> import requests <EOL> from . mapper import Mapper <EOL> from . sitemap import Sitemap <EOL> from . client import Client , ClientFatalError <EOL> from . client_state import ClientState <EOL> from . resource import Resource <EOL> from . w3c_datetime import str_to_datetime , datetime_to_str <EOL> class XResource ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , uri , acceptable_capabilities = None , checks = None , context = None ) : <EOL> self . uri = urljoin ( context , uri ) <EOL> self . acceptable_capabilities = acceptable_capabilities <EOL> self . checks = checks <EOL> class HeadResponse ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . status_code = None <EOL> self . headers = { } <EOL> class ExplorerQuit ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class Explorer ( Client ) : <EOL> """<STR_LIT>""" <EOL> def explore ( self ) : <EOL> """<STR_LIT>""" <EOL> starts = [ ] <EOL> if ( self . sitemap_name is not None ) : <EOL> print ( "<STR_LIT>" % ( uri ) ) <EOL> starts . append ( XResource ( uri ) ) <EOL> elif ( len ( self . mapper ) > <NUM_LIT:0> ) : <EOL> uri = self . mapper . default_src_uri ( ) <EOL> ( scheme , netloc , path , params , query , fragment ) = urlparse ( uri ) <EOL> if ( not scheme and not netloc ) : <EOL> if ( os . path . isdir ( path ) ) : <EOL> print ( "<STR_LIT>" % ( path ) ) <EOL> for name in [ '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] : <EOL> file = os . path . join ( path , name ) <EOL> if ( os . path . isfile ( file ) ) : <EOL> starts . append ( XResource ( file ) ) <EOL> if ( len ( starts ) == <NUM_LIT:0> ) : <EOL> raise ClientFatalError ( "<STR_LIT>" % <EOL> ( path ) ) <EOL> else : <EOL> print ( "<STR_LIT>" % ( path ) ) <EOL> starts . append ( XResource ( path ) ) <EOL> else : <EOL> well_known = urlunparse ( [ scheme , netloc , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> if ( not path ) : <EOL> starts . append ( XResource ( well_known , [ '<STR_LIT>' , '<STR_LIT>' ] ) ) <EOL> else : <EOL> starts . append ( XResource ( uri ) ) <EOL> starts . append ( XResource ( well_known , [ '<STR_LIT>' , '<STR_LIT>' ] ) ) <EOL> print ( "<STR_LIT>" ) <EOL> else : <EOL> raise ClientFatalError ( "<STR_LIT>" ) <EOL> try : <EOL> for start in starts : <EOL> history = [ start ] <EOL> input = None <EOL> while ( len ( history ) > <NUM_LIT:0> ) : <EOL> print <EOL> xr = history . pop ( ) <EOL> new_xr = self . explore_uri ( xr , len ( history ) > <NUM_LIT:0> ) <EOL> if ( new_xr ) : <EOL> history . append ( xr ) <EOL> history . append ( new_xr ) <EOL> except ExplorerQuit : <EOL> pass <EOL> print ( "<STR_LIT>" ) <EOL> def explore_uri ( self , explorer_resource , show_back = True ) : <EOL> """<STR_LIT>""" <EOL> uri = explorer_resource . uri <EOL> caps = explorer_resource . acceptable_capabilities <EOL> checks = explorer_resource . checks <EOL> print ( "<STR_LIT>" % ( uri ) ) <EOL> options = { } <EOL> capability = None <EOL> try : <EOL> if ( caps == '<STR_LIT>' ) : <EOL> self . explore_show_head ( uri , check_headers = checks ) <EOL> else : <EOL> s = Sitemap ( ) <EOL> list = s . parse_xml ( urllib . urlopen ( uri ) ) <EOL> ( options , capability ) = self . explore_show_summary ( list , s . parsed_index , caps , context = uri ) <EOL> except IOError as e : <EOL> print ( "<STR_LIT>" % ( uri , str ( e ) ) ) <EOL> except Exception as e : <EOL> print ( "<STR_LIT>" % ( uri , str ( e ) ) ) <EOL> while ( True ) : <EOL> num_prompt = '<STR_LIT>' if ( len ( options ) == <NUM_LIT:0> ) else '<STR_LIT>' <EOL> up_prompt = '<STR_LIT>' if ( show_back ) else '<STR_LIT>' <EOL> input = raw_input ( "<STR_LIT>" % ( num_prompt , up_prompt ) ) <EOL> if ( input in options . keys ( ) ) : <EOL> break <EOL> if ( input == '<STR_LIT:q>' ) : <EOL> raise ExplorerQuit ( ) <EOL> if ( input == '<STR_LIT:b>' ) : <EOL> return ( None ) <EOL> checks = { } <EOL> r = options [ input ] <EOL> if ( r . capability is None ) : <EOL> if ( capability in [ '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] ) : <EOL> caps = '<STR_LIT>' <EOL> else : <EOL> caps = self . allowed_entries ( capability ) <EOL> elif ( r . capability is '<STR_LIT>' ) : <EOL> caps = r . capability <EOL> else : <EOL> caps = [ r . capability ] <EOL> if ( r . length is not None ) : <EOL> checks [ '<STR_LIT>' ] = r . length <EOL> if ( r . lastmod is not None ) : <EOL> checks [ '<STR_LIT>' ] = r . lastmod <EOL> if ( r . mime_type is not None ) : <EOL> checks [ '<STR_LIT>' ] = r . mime_type <EOL> return ( XResource ( options [ input ] . uri , caps , checks ) ) <EOL> def explore_show_summary ( self , list , index = False , expected = None , context = None ) : <EOL> """<STR_LIT>""" <EOL> num_entries = len ( list . resources ) <EOL> capability = '<STR_LIT>' <EOL> if ( '<STR_LIT>' in list . md ) : <EOL> capability = list . md [ '<STR_LIT>' ] <EOL> if ( index ) : <EOL> capability += '<STR_LIT:index>' <EOL> print ( "<STR_LIT>" % ( capability , num_entries ) ) <EOL> if ( expected is not None and capability not in expected ) : <EOL> print ( "<STR_LIT>" % ( '<STR_LIT:U+002C>' . join ( expected ) ) ) <EOL> if ( capability not in [ '<STR_LIT:description>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] ) : <EOL> print ( "<STR_LIT>" % ( capability ) ) <EOL> to_show = num_entries <EOL> if ( num_entries > <NUM_LIT> ) : <EOL> to_show = <NUM_LIT:20> <EOL> entry_caps = self . allowed_entries ( capability ) ; <EOL> options = { } <EOL> n = <NUM_LIT:0> <EOL> ln_describedby = list . link ( '<STR_LIT>' ) <EOL> if ( ln_describedby ) : <EOL> if ( '<STR_LIT>' in ln_describedby ) : <EOL> uri = ln_describedby [ '<STR_LIT>' ] <EOL> print ( "<STR_LIT>" % ( '<STR_LIT:d>' , uri ) ) <EOL> uri = self . expand_relative_uri ( context , uri ) <EOL> options [ '<STR_LIT:d>' ] = Resource ( uri , capability = '<STR_LIT>' ) <EOL> else : <EOL> print ( "<STR_LIT>" ) <EOL> ln_up = list . link ( '<STR_LIT>' ) <EOL> if ( ln_up ) : <EOL> if ( '<STR_LIT>' in ln_up ) : <EOL> uri = ln_up [ '<STR_LIT>' ] <EOL> print ( "<STR_LIT>" % ( '<STR_LIT:u>' , uri ) ) <EOL> uri = self . expand_relative_uri ( context , uri ) <EOL> options [ '<STR_LIT:u>' ] = Resource ( uri ) <EOL> else : <EOL> print ( "<STR_LIT>" ) <EOL> ln_index = list . link ( '<STR_LIT:index>' ) <EOL> if ( ln_index ) : <EOL> if ( '<STR_LIT>' in ln_index ) : <EOL> uri = ln_index [ '<STR_LIT>' ] <EOL> print ( "<STR_LIT>" % ( '<STR_LIT:i>' , uri ) ) <EOL> uri = self . expand_relative_uri ( context , uri ) <EOL> options [ '<STR_LIT:i>' ] = Resource ( uri ) <EOL> else : <EOL> print ( "<STR_LIT>" ) <EOL> for r in list . resources : <EOL> if ( n >= to_show ) : <EOL> print ( "<STR_LIT>" % ( num_entries - n ) ) <EOL> break <EOL> n += <NUM_LIT:1> <EOL> options [ str ( n ) ] = r <EOL> print ( "<STR_LIT>" % ( n , r . uri ) ) <EOL> if ( self . verbose ) : <EOL> print ( "<STR_LIT:U+0020>" + str ( r ) ) <EOL> r . uri = self . expand_relative_uri ( context , r . uri ) <EOL> if ( r . capability is not None ) : <EOL> warning = '<STR_LIT>' <EOL> if ( r . capability not in entry_caps ) : <EOL> warning = "<STR_LIT>" % ( '<STR_LIT>' . join ( entry_caps ) ) <EOL> print ( "<STR_LIT>" % ( r . capability , warning ) ) <EOL> elif ( len ( entry_caps ) == <NUM_LIT:1> ) : <EOL> r . capability = entry_caps [ <NUM_LIT:0> ] <EOL> print ( "<STR_LIT>" % ( r . capability ) ) <EOL> return ( options , capability ) <EOL> def explore_show_head ( self , uri , check_headers = None ) : <EOL> """<STR_LIT>""" <EOL> print ( "<STR_LIT>" % ( uri ) ) <EOL> if ( re . match ( r'<STR_LIT>' , uri ) ) : <EOL> response = requests . head ( uri ) <EOL> else : <EOL> response = self . head_on_file ( uri ) <EOL> print ( "<STR_LIT>" % ( response . status_code ) ) <EOL> if ( response . status_code == '<STR_LIT>' ) : <EOL> for header in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if header in response . headers : <EOL> check_str = '<STR_LIT>' <EOL> if ( check_headers is not None and <EOL> header in check_headers ) : <EOL> if ( response . headers [ header ] == check_headers [ header ] ) : <EOL> check_str = '<STR_LIT>' <EOL> else : <EOL> check_str = '<STR_LIT>' % ( check_headers [ header ] ) <EOL> print ( "<STR_LIT>" % ( header , response . headers [ header ] , check_str ) ) <EOL> def head_on_file ( self , file ) : <EOL> """<STR_LIT>""" <EOL> response = HeadResponse ( ) <EOL> if ( not os . path . isfile ( file ) ) : <EOL> response . status_code = '<STR_LIT>' <EOL> else : <EOL> response . status_code = '<STR_LIT>' <EOL> response . headers [ '<STR_LIT>' ] = datetime_to_str ( os . path . getmtime ( file ) ) <EOL> response . headers [ '<STR_LIT>' ] = os . path . getsize ( file ) <EOL> return ( response ) <EOL> def allowed_entries ( self , capability ) : <EOL> """<STR_LIT>""" <EOL> index = re . match ( r'<STR_LIT>' , capability ) <EOL> archive = re . match ( '<STR_LIT>' , capability ) <EOL> if ( capability == '<STR_LIT>' ) : <EOL> return ( [ ] ) <EOL> elif ( index ) : <EOL> return ( index . group ( <NUM_LIT:0> ) ) <EOL> elif ( archive ) : <EOL> return ( archive . group ( <NUM_LIT:0> ) ) <EOL> elif ( capability == '<STR_LIT:description>' ) : <EOL> return ( [ '<STR_LIT>' ] ) <EOL> elif ( capability == '<STR_LIT>' ) : <EOL> return ( [ '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> return ( [ ] ) <EOL> def expand_relative_uri ( self , context , uri ) : <EOL> """<STR_LIT>""" <EOL> full_uri = urljoin ( context , uri ) <EOL> if ( full_uri != uri ) : <EOL> print ( "<STR_LIT>" % ( full_uri ) ) <EOL> uri = full_uri <EOL> return ( uri ) </s>
