<s> import os <EOL> import sys <EOL> import binascii <EOL> import socket <EOL> import time <EOL> import ssl <EOL> from datetime import datetime , timedelta <EOL> from functools import wraps <EOL> from libcloud . utils . py3 import httplib <EOL> from libcloud . common . exceptions import RateLimitReachedError <EOL> from libcloud . common . providers import get_driver as _get_driver <EOL> from libcloud . common . providers import set_driver as _set_driver <EOL> __all__ = [ <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> ] <EOL> TRANSIENT_SSL_ERROR = '<STR_LIT>' <EOL> class TransientSSLError ( ssl . SSLError ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> DEFAULT_TIMEOUT = <NUM_LIT:30> <EOL> DEFAULT_DELAY = <NUM_LIT:1> <EOL> DEFAULT_BACKOFF = <NUM_LIT:1> <EOL> RETRY_EXCEPTIONS = ( RateLimitReachedError , socket . error , socket . gaierror , <EOL> httplib . NotConnected , httplib . ImproperConnectionState , <EOL> TransientSSLError ) <EOL> def find ( l , predicate ) : <EOL> results = [ x for x in l if predicate ( x ) ] <EOL> return results [ <NUM_LIT:0> ] if len ( results ) > <NUM_LIT:0> else None <EOL> get_driver = _get_driver <EOL> set_driver = _set_driver <EOL> def merge_valid_keys ( params , valid_keys , extra ) : <EOL> """<STR_LIT>""" <EOL> merged = { } <EOL> if not extra : <EOL> return merged <EOL> for key in valid_keys : <EOL> if key in extra : <EOL> params [ key ] = extra [ key ] <EOL> merged [ key ] = extra [ key ] <EOL> return merged <EOL> def get_new_obj ( obj , klass , attributes ) : <EOL> """<STR_LIT>""" <EOL> kwargs = { } <EOL> for key , value in list ( obj . __dict__ . items ( ) ) : <EOL> if isinstance ( value , dict ) : <EOL> kwargs [ key ] = value . copy ( ) <EOL> elif isinstance ( value , ( tuple , list ) ) : <EOL> kwargs [ key ] = value [ : ] <EOL> else : <EOL> kwargs [ key ] = value <EOL> for key , value in list ( attributes . items ( ) ) : <EOL> if value is None : <EOL> continue <EOL> if isinstance ( value , dict ) : <EOL> kwargs_value = kwargs . get ( key , { } ) <EOL> for key1 , value2 in list ( value . items ( ) ) : <EOL> if value2 is None : <EOL> continue <EOL> kwargs_value [ key1 ] = value2 <EOL> kwargs [ key ] = kwargs_value <EOL> else : <EOL> kwargs [ key ] = value <EOL> return klass ( ** kwargs ) <EOL> def str2dicts ( data ) : <EOL> """<STR_LIT>""" <EOL> list_data = [ ] <EOL> list_data . append ( { } ) <EOL> d = list_data [ - <NUM_LIT:1> ] <EOL> lines = data . split ( '<STR_LIT:\n>' ) <EOL> for line in lines : <EOL> line = line . strip ( ) <EOL> if not line : <EOL> d = { } <EOL> list_data . append ( d ) <EOL> d = list_data [ - <NUM_LIT:1> ] <EOL> continue <EOL> whitespace = line . find ( '<STR_LIT:U+0020>' ) <EOL> if not whitespace : <EOL> continue <EOL> key = line [ <NUM_LIT:0> : whitespace ] <EOL> value = line [ whitespace + <NUM_LIT:1> : ] <EOL> d . update ( { key : value } ) <EOL> list_data = [ val for val in list_data if val != { } ] <EOL> return list_data <EOL> def str2list ( data ) : <EOL> """<STR_LIT>""" <EOL> list_data = [ ] <EOL> for line in data . split ( '<STR_LIT:\n>' ) : <EOL> line = line . strip ( ) <EOL> if not line : <EOL> continue <EOL> try : <EOL> splitted = line . split ( '<STR_LIT:U+0020>' ) <EOL> value = splitted [ <NUM_LIT:1> ] <EOL> except Exception : <EOL> continue <EOL> list_data . append ( value ) <EOL> return list_data <EOL> def dict2str ( data ) : <EOL> """<STR_LIT>""" <EOL> result = '<STR_LIT>' <EOL> for k in data : <EOL> if data [ k ] is not None : <EOL> result += '<STR_LIT>' % ( str ( k ) , str ( data [ k ] ) ) <EOL> else : <EOL> result += '<STR_LIT>' % str ( k ) <EOL> return result <EOL> def reverse_dict ( dictionary ) : <EOL> return dict ( [ ( value , key ) for key , value in list ( dictionary . items ( ) ) ] ) <EOL> def lowercase_keys ( dictionary ) : <EOL> return dict ( ( ( k . lower ( ) , v ) for k , v in dictionary . items ( ) ) ) <EOL> def get_secure_random_string ( size ) : <EOL> """<STR_LIT>""" <EOL> value = os . urandom ( size ) <EOL> value = binascii . hexlify ( value ) <EOL> value = value . decode ( '<STR_LIT:utf-8>' ) [ : size ] <EOL> return value <EOL> class ReprMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> _repr_attributes = [ ] <EOL> def __repr__ ( self ) : <EOL> attributes = [ ] <EOL> for attribute in self . _repr_attributes : <EOL> value = getattr ( self , attribute , None ) <EOL> attributes . append ( '<STR_LIT>' % ( attribute , value ) ) <EOL> values = ( self . __class__ . __name__ , '<STR_LIT:U+002CU+0020>' . join ( attributes ) ) <EOL> result = '<STR_LIT>' % values <EOL> return result <EOL> def __str__ ( self ) : <EOL> return str ( self . __repr__ ( ) ) <EOL> def retry ( retry_exceptions = RETRY_EXCEPTIONS , retry_delay = DEFAULT_DELAY , <EOL> timeout = DEFAULT_TIMEOUT , backoff = DEFAULT_BACKOFF ) : <EOL> """<STR_LIT>""" <EOL> if retry_exceptions is None : <EOL> retry_exceptions = RETRY_EXCEPTIONS <EOL> if retry_delay is None : <EOL> retry_delay = DEFAULT_DELAY <EOL> if timeout is None : <EOL> timeout = DEFAULT_TIMEOUT <EOL> if backoff is None : <EOL> backoff = DEFAULT_BACKOFF <EOL> timeout = max ( timeout , <NUM_LIT:0> ) <EOL> def transform_ssl_error ( func , * args , ** kwargs ) : <EOL> try : <EOL> return func ( * args , ** kwargs ) <EOL> except ssl . SSLError : <EOL> exc = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> if TRANSIENT_SSL_ERROR in str ( exc ) : <EOL> raise TransientSSLError ( * exc . args ) <EOL> raise exc <EOL> def decorator ( func ) : <EOL> @ wraps ( func ) <EOL> def retry_loop ( * args , ** kwargs ) : <EOL> current_delay = retry_delay <EOL> end = datetime . now ( ) + timedelta ( seconds = timeout ) <EOL> while True : <EOL> try : <EOL> return transform_ssl_error ( func , * args , ** kwargs ) <EOL> except retry_exceptions : <EOL> exc = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> if isinstance ( exc , RateLimitReachedError ) : <EOL> time . sleep ( exc . retry_after ) <EOL> current_delay = retry_delay <EOL> end = datetime . now ( ) + timedelta ( <EOL> seconds = exc . retry_after + timeout ) <EOL> elif datetime . now ( ) >= end : <EOL> raise <EOL> else : <EOL> time . sleep ( current_delay ) <EOL> current_delay *= backoff <EOL> return retry_loop <EOL> return decorator </s>
