<s> """<STR_LIT>""" <EOL> import httplib <EOL> import logging <EOL> import socket <EOL> import ssl <EOL> import urllib <EOL> import io <EOL> import sys <EOL> from datetime import datetime <EOL> from functools import wraps <EOL> from StringIO import StringIO <EOL> from contextlib import contextmanager <EOL> from xml . etree . ElementTree import XML <EOL> from data import record <EOL> __all__ = [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> ] <EOL> DEFAULT_HOST = "<STR_LIT:localhost>" <EOL> DEFAULT_PORT = "<STR_LIT>" <EOL> DEFAULT_SCHEME = "<STR_LIT>" <EOL> def _log_duration ( f ) : <EOL> @ wraps ( f ) <EOL> def new_f ( * args , ** kwargs ) : <EOL> start_time = datetime . now ( ) <EOL> val = f ( * args , ** kwargs ) <EOL> end_time = datetime . now ( ) <EOL> logging . debug ( "<STR_LIT>" , end_time - start_time ) <EOL> return val <EOL> return new_f <EOL> class _NoAuthenticationToken ( object ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class UrlEncoded ( str ) : <EOL> """<STR_LIT>""" <EOL> def __new__ ( self , val = '<STR_LIT>' , skip_encode = False , encode_slash = False ) : <EOL> if isinstance ( val , UrlEncoded ) : <EOL> return val <EOL> elif skip_encode : <EOL> return str . __new__ ( self , val ) <EOL> elif encode_slash : <EOL> return str . __new__ ( self , urllib . quote_plus ( val ) ) <EOL> else : <EOL> return str . __new__ ( self , urllib . quote ( val ) ) <EOL> def __add__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( other , UrlEncoded ) : <EOL> return UrlEncoded ( str . __add__ ( self , other ) , skip_encode = True ) <EOL> else : <EOL> return UrlEncoded ( str . __add__ ( self , urllib . quote ( other ) ) , skip_encode = True ) <EOL> def __radd__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( other , UrlEncoded ) : <EOL> return UrlEncoded ( str . __radd__ ( self , other ) , skip_encode = True ) <EOL> else : <EOL> return UrlEncoded ( str . __add__ ( urllib . quote ( other ) , self ) , skip_encode = True ) <EOL> def __mod__ ( self , fields ) : <EOL> """<STR_LIT>""" <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % repr ( urllib . unquote ( str ( self ) ) ) <EOL> @ contextmanager <EOL> def _handle_auth_error ( msg ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> yield <EOL> except HTTPError as he : <EOL> if he . status == <NUM_LIT> : <EOL> raise AuthenticationError ( msg , he ) <EOL> else : <EOL> raise <EOL> def _authentication ( request_fun ) : <EOL> """<STR_LIT>""" <EOL> @ wraps ( request_fun ) <EOL> def wrapper ( self , * args , ** kwargs ) : <EOL> if self . token is _NoAuthenticationToken : <EOL> if self . autologin and self . username and self . password : <EOL> self . login ( ) <EOL> else : <EOL> with _handle_auth_error ( "<STR_LIT>" ) : <EOL> return request_fun ( self , * args , ** kwargs ) <EOL> try : <EOL> return request_fun ( self , * args , ** kwargs ) <EOL> except HTTPError as he : <EOL> if he . status == <NUM_LIT> and self . autologin : <EOL> with _handle_auth_error ( "<STR_LIT>" ) : <EOL> self . login ( ) <EOL> with _handle_auth_error ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) : <EOL> return request_fun ( self , * args , ** kwargs ) <EOL> elif he . status == <NUM_LIT> and not self . autologin : <EOL> raise AuthenticationError ( <EOL> "<STR_LIT>" , he ) <EOL> else : <EOL> raise <EOL> return wrapper <EOL> def _authority ( scheme = DEFAULT_SCHEME , host = DEFAULT_HOST , port = DEFAULT_PORT ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT::>' in host : <EOL> host = '<STR_LIT:[>' + host + '<STR_LIT:]>' <EOL> return UrlEncoded ( "<STR_LIT>" % ( scheme , host , port ) , skip_encode = True ) <EOL> def namespace ( sharing = None , owner = None , app = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if sharing in [ "<STR_LIT>" ] : <EOL> return record ( { '<STR_LIT>' : sharing , '<STR_LIT>' : "<STR_LIT>" , '<STR_LIT>' : "<STR_LIT>" } ) <EOL> if sharing in [ "<STR_LIT>" , "<STR_LIT>" ] : <EOL> return record ( { '<STR_LIT>' : sharing , '<STR_LIT>' : "<STR_LIT>" , '<STR_LIT>' : app } ) <EOL> if sharing in [ "<STR_LIT:user>" , None ] : <EOL> return record ( { '<STR_LIT>' : sharing , '<STR_LIT>' : owner , '<STR_LIT>' : app } ) <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> class Context ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , handler = None , ** kwargs ) : <EOL> self . http = HttpLib ( handler ) <EOL> self . token = kwargs . get ( "<STR_LIT>" , _NoAuthenticationToken ) <EOL> if self . token is None : <EOL> self . token = _NoAuthenticationToken <EOL> self . scheme = kwargs . get ( "<STR_LIT>" , DEFAULT_SCHEME ) <EOL> self . host = kwargs . get ( "<STR_LIT:host>" , DEFAULT_HOST ) <EOL> self . port = int ( kwargs . get ( "<STR_LIT:port>" , DEFAULT_PORT ) ) <EOL> self . authority = _authority ( self . scheme , self . host , self . port ) <EOL> self . namespace = namespace ( ** kwargs ) <EOL> self . username = kwargs . get ( "<STR_LIT:username>" , "<STR_LIT>" ) <EOL> self . password = kwargs . get ( "<STR_LIT:password>" , "<STR_LIT>" ) <EOL> self . autologin = kwargs . get ( "<STR_LIT>" , False ) <EOL> @ property <EOL> def _auth_headers ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . token is _NoAuthenticationToken : <EOL> return [ ] <EOL> else : <EOL> if self . token . startswith ( '<STR_LIT>' ) : <EOL> token = self . token <EOL> else : <EOL> token = '<STR_LIT>' % self . token <EOL> return [ ( "<STR_LIT>" , token ) ] <EOL> def connect ( self ) : <EOL> """<STR_LIT>""" <EOL> sock = socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) <EOL> if self . scheme == "<STR_LIT>" : <EOL> sock = ssl . wrap_socket ( sock ) <EOL> sock . connect ( ( socket . gethostbyname ( self . host ) , self . port ) ) <EOL> return sock <EOL> @ _authentication <EOL> @ _log_duration <EOL> def delete ( self , path_segment , owner = None , app = None , sharing = None , ** query ) : <EOL> """<STR_LIT>""" <EOL> path = self . authority + self . _abspath ( path_segment , owner = owner , <EOL> app = app , sharing = sharing ) <EOL> logging . debug ( "<STR_LIT>" , path , repr ( query ) ) <EOL> response = self . http . delete ( path , self . _auth_headers , ** query ) <EOL> return response <EOL> @ _authentication <EOL> @ _log_duration <EOL> def get ( self , path_segment , owner = None , app = None , sharing = None , ** query ) : <EOL> """<STR_LIT>""" <EOL> path = self . authority + self . _abspath ( path_segment , owner = owner , <EOL> app = app , sharing = sharing ) <EOL> logging . debug ( "<STR_LIT>" , path , repr ( query ) ) <EOL> response = self . http . get ( path , self . _auth_headers , ** query ) <EOL> return response <EOL> @ _authentication <EOL> @ _log_duration <EOL> def post ( self , path_segment , owner = None , app = None , sharing = None , headers = None , ** query ) : <EOL> """<STR_LIT>""" <EOL> if headers is None : <EOL> headers = [ ] <EOL> path = self . authority + self . _abspath ( path_segment , owner = owner , app = app , sharing = sharing ) <EOL> logging . debug ( "<STR_LIT>" , path , repr ( query ) ) <EOL> all_headers = headers + self . _auth_headers <EOL> response = self . http . post ( path , all_headers , ** query ) <EOL> return response <EOL> @ _authentication <EOL> @ _log_duration <EOL> def request ( self , path_segment , method = "<STR_LIT:GET>" , headers = None , body = "<STR_LIT>" , <EOL> owner = None , app = None , sharing = None ) : <EOL> """<STR_LIT>""" <EOL> if headers is None : <EOL> headers = [ ] <EOL> path = self . authority + self . _abspath ( path_segment , owner = owner , <EOL> app = app , sharing = sharing ) <EOL> all_headers = headers + self . _auth_headers <EOL> logging . debug ( "<STR_LIT>" , <EOL> method , path , str ( all_headers ) , repr ( body ) ) <EOL> response = self . http . request ( path , <EOL> { '<STR_LIT>' : method , <EOL> '<STR_LIT>' : all_headers , <EOL> '<STR_LIT:body>' : body } ) <EOL> return response <EOL> def login ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . token is not _NoAuthenticationToken and ( not self . username and not self . password ) : <EOL> return <EOL> try : <EOL> response = self . http . post ( <EOL> self . authority + self . _abspath ( "<STR_LIT>" ) , <EOL> username = self . username , <EOL> password = self . password ) <EOL> body = response . body . read ( ) <EOL> session = XML ( body ) . findtext ( "<STR_LIT>" ) <EOL> self . token = "<STR_LIT>" % session <EOL> return self <EOL> except HTTPError as he : <EOL> if he . status == <NUM_LIT> : <EOL> raise AuthenticationError ( "<STR_LIT>" , he ) <EOL> else : <EOL> raise <EOL> def logout ( self ) : <EOL> """<STR_LIT>""" <EOL> self . token = _NoAuthenticationToken <EOL> return self <EOL> def _abspath ( self , path_segment , <EOL> owner = None , app = None , sharing = None ) : <EOL> """<STR_LIT>""" <EOL> skip_encode = isinstance ( path_segment , UrlEncoded ) <EOL> if path_segment . startswith ( '<STR_LIT:/>' ) : <EOL> return UrlEncoded ( path_segment , skip_encode = skip_encode ) <EOL> if owner or app or sharing : <EOL> ns = namespace ( owner = owner , app = app , sharing = sharing ) <EOL> else : <EOL> ns = self . namespace <EOL> if ns . app is None and ns . owner is None : <EOL> return UrlEncoded ( "<STR_LIT>" % path_segment , skip_encode = skip_encode ) <EOL> oname = "<STR_LIT>" if ns . owner is None else ns . owner <EOL> aname = "<STR_LIT>" if ns . app is None else ns . app <EOL> path = UrlEncoded ( "<STR_LIT>" % ( oname , aname , path_segment ) , <EOL> skip_encode = skip_encode ) <EOL> return path <EOL> def connect ( ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> c = Context ( ** kwargs ) <EOL> c . login ( ) <EOL> return c <EOL> class HTTPError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , response , _message = None ) : <EOL> status = response . status <EOL> reason = response . reason <EOL> body = response . body . read ( ) <EOL> detail = XML ( body ) . findtext ( "<STR_LIT>" ) <EOL> message = "<STR_LIT>" % ( <EOL> status , reason , "<STR_LIT>" if detail is None else "<STR_LIT>" % detail ) <EOL> Exception . __init__ ( self , _message or message ) <EOL> self . status = status <EOL> self . reason = reason <EOL> self . headers = response . headers <EOL> self . body = body <EOL> self . _response = response <EOL> class AuthenticationError ( HTTPError ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , message , cause ) : <EOL> cause . _response . body = StringIO ( cause . body ) <EOL> HTTPError . __init__ ( self , cause . _response , message ) <EOL> def _encode ( ** kwargs ) : <EOL> items = [ ] <EOL> for key , value in kwargs . iteritems ( ) : <EOL> if isinstance ( value , list ) : <EOL> items . extend ( [ ( key , item ) for item in value ] ) <EOL> else : <EOL> items . append ( ( key , value ) ) <EOL> return urllib . urlencode ( items ) <EOL> def _spliturl ( url ) : <EOL> scheme , opaque = urllib . splittype ( url ) <EOL> netloc , path = urllib . splithost ( opaque ) <EOL> host , port = urllib . splitport ( netloc ) <EOL> if host . startswith ( '<STR_LIT:[>' ) and host . endswith ( '<STR_LIT:]>' ) : host = host [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> if port is None : port = DEFAULT_PORT <EOL> return scheme , host , port , path <EOL> class HttpLib ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , custom_handler = None ) : <EOL> self . handler = handler ( ) if custom_handler is None else custom_handler <EOL> def delete ( self , url , headers = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if headers is None : headers = [ ] <EOL> if kwargs : <EOL> url = url + UrlEncoded ( '<STR_LIT:?>' + _encode ( ** kwargs ) , skip_encode = True ) <EOL> message = { <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : headers , <EOL> } <EOL> return self . request ( url , message ) <EOL> def get ( self , url , headers = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if headers is None : headers = [ ] <EOL> if kwargs : <EOL> url = url + UrlEncoded ( '<STR_LIT:?>' + _encode ( ** kwargs ) , skip_encode = True ) <EOL> return self . request ( url , { '<STR_LIT>' : "<STR_LIT:GET>" , '<STR_LIT>' : headers } ) <EOL> def post ( self , url , headers = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if headers is None : headers = [ ] <EOL> headers . append ( ( "<STR_LIT:Content-Type>" , "<STR_LIT>" ) ) , <EOL> if '<STR_LIT:body>' in kwargs : <EOL> body = kwargs . pop ( '<STR_LIT:body>' ) <EOL> if len ( kwargs ) > <NUM_LIT:0> : <EOL> url = url + UrlEncoded ( '<STR_LIT:?>' + _encode ( ** kwargs ) , skip_encode = True ) <EOL> else : <EOL> body = _encode ( ** kwargs ) <EOL> message = { <EOL> '<STR_LIT>' : "<STR_LIT:POST>" , <EOL> '<STR_LIT>' : headers , <EOL> '<STR_LIT:body>' : body <EOL> } <EOL> return self . request ( url , message ) <EOL> def request ( self , url , message , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> response = self . handler ( url , message , ** kwargs ) <EOL> response = record ( response ) <EOL> if <NUM_LIT> <= response . status : <EOL> raise HTTPError ( response ) <EOL> return response <EOL> class ResponseReader ( io . RawIOBase ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , response ) : <EOL> self . _response = response <EOL> self . _buffer = '<STR_LIT>' <EOL> def __str__ ( self ) : <EOL> return self . read ( ) <EOL> @ property <EOL> def empty ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . peek ( <NUM_LIT:1> ) == "<STR_LIT>" <EOL> def peek ( self , size ) : <EOL> """<STR_LIT>""" <EOL> c = self . read ( size ) <EOL> self . _buffer = self . _buffer + c <EOL> return c <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _response . close ( ) <EOL> def read ( self , size = None ) : <EOL> """<STR_LIT>""" <EOL> r = self . _buffer <EOL> self . _buffer = '<STR_LIT>' <EOL> if size is not None : <EOL> size -= len ( r ) <EOL> r = r + self . _response . read ( size ) <EOL> return r <EOL> def readable ( self ) : <EOL> """<STR_LIT>""" <EOL> return True <EOL> def readinto ( self , byte_array ) : <EOL> """<STR_LIT>""" <EOL> max_size = len ( byte_array ) <EOL> data = self . read ( max_size ) <EOL> bytes_read = len ( data ) <EOL> byte_array [ : bytes_read ] = data <EOL> return bytes_read <EOL> def handler ( key_file = None , cert_file = None , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> def connect ( scheme , host , port ) : <EOL> kwargs = { } <EOL> if timeout is not None : kwargs [ '<STR_LIT>' ] = timeout <EOL> if scheme == "<STR_LIT:http>" : <EOL> return httplib . HTTPConnection ( host , port , ** kwargs ) <EOL> if scheme == "<STR_LIT>" : <EOL> if key_file is not None : kwargs [ '<STR_LIT>' ] = key_file <EOL> if cert_file is not None : kwargs [ '<STR_LIT>' ] = cert_file <EOL> if sys . version_info >= ( <NUM_LIT:2> , <NUM_LIT:7> , <NUM_LIT:9> ) and key_file is None and cert_file is None : <EOL> kwargs [ '<STR_LIT>' ] = ssl . _create_unverified_context ( ) <EOL> return httplib . HTTPSConnection ( host , port , ** kwargs ) <EOL> raise ValueError ( "<STR_LIT>" % scheme ) <EOL> def request ( url , message , ** kwargs ) : <EOL> scheme , host , port , path = _spliturl ( url ) <EOL> body = message . get ( "<STR_LIT:body>" , "<STR_LIT>" ) <EOL> head = { <EOL> "<STR_LIT>" : str ( len ( body ) ) , <EOL> "<STR_LIT>" : host , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } <EOL> for key , value in message [ "<STR_LIT>" ] : <EOL> head [ key ] = value <EOL> method = message . get ( "<STR_LIT>" , "<STR_LIT:GET>" ) <EOL> connection = connect ( scheme , host , port ) <EOL> try : <EOL> connection . request ( method , path , body , head ) <EOL> if timeout is not None : <EOL> connection . sock . settimeout ( timeout ) <EOL> response = connection . getresponse ( ) <EOL> finally : <EOL> connection . close ( ) <EOL> return { <EOL> "<STR_LIT:status>" : response . status , <EOL> "<STR_LIT>" : response . reason , <EOL> "<STR_LIT>" : response . getheaders ( ) , <EOL> "<STR_LIT:body>" : ResponseReader ( response ) , <EOL> } <EOL> return request </s>
