<s> """<STR_LIT>""" <EOL> from __future__ import division <EOL> from datetime import date , datetime , time , timedelta , tzinfo <EOL> from calendar import timegm <EOL> import re <EOL> from pytz import timezone , utc <EOL> import six <EOL> try : <EOL> from inspect import signature <EOL> except ImportError : <EOL> from funcsigs import signature <EOL> try : <EOL> from threading import TIMEOUT_MAX <EOL> except ImportError : <EOL> TIMEOUT_MAX = <NUM_LIT> <EOL> __all__ = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> class _Undefined ( object ) : <EOL> def __nonzero__ ( self ) : <EOL> return False <EOL> def __bool__ ( self ) : <EOL> return False <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' <EOL> undefined = _Undefined ( ) <EOL> def asint ( text ) : <EOL> """<STR_LIT>""" <EOL> if text is not None : <EOL> return int ( text ) <EOL> def asbool ( obj ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( obj , str ) : <EOL> obj = obj . strip ( ) . lower ( ) <EOL> if obj in ( '<STR_LIT:true>' , '<STR_LIT:yes>' , '<STR_LIT>' , '<STR_LIT:y>' , '<STR_LIT:t>' , '<STR_LIT:1>' ) : <EOL> return True <EOL> if obj in ( '<STR_LIT:false>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:n>' , '<STR_LIT:f>' , '<STR_LIT:0>' ) : <EOL> return False <EOL> raise ValueError ( '<STR_LIT>' % obj ) <EOL> return bool ( obj ) <EOL> def astimezone ( obj ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( obj , six . string_types ) : <EOL> return timezone ( obj ) <EOL> if isinstance ( obj , tzinfo ) : <EOL> if not hasattr ( obj , '<STR_LIT>' ) or not hasattr ( obj , '<STR_LIT>' ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> if obj . zone == '<STR_LIT>' : <EOL> raise ValueError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> return obj <EOL> if obj is not None : <EOL> raise TypeError ( '<STR_LIT>' % obj . __class__ . __name__ ) <EOL> _DATE_REGEX = re . compile ( <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' ) <EOL> def convert_to_datetime ( input , tz , arg_name ) : <EOL> """<STR_LIT>""" <EOL> if input is None : <EOL> return <EOL> elif isinstance ( input , datetime ) : <EOL> datetime_ = input <EOL> elif isinstance ( input , date ) : <EOL> datetime_ = datetime . combine ( input , time ( ) ) <EOL> elif isinstance ( input , six . string_types ) : <EOL> m = _DATE_REGEX . match ( input ) <EOL> if not m : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> values = [ ( k , int ( v or <NUM_LIT:0> ) ) for k , v in m . groupdict ( ) . items ( ) ] <EOL> values = dict ( values ) <EOL> datetime_ = datetime ( ** values ) <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' % ( arg_name , input . __class__ . __name__ ) ) <EOL> if datetime_ . tzinfo is not None : <EOL> return datetime_ <EOL> if tz is None : <EOL> raise ValueError ( <EOL> '<STR_LIT>' % arg_name ) <EOL> if isinstance ( tz , six . string_types ) : <EOL> tz = timezone ( tz ) <EOL> try : <EOL> return tz . localize ( datetime_ , is_dst = None ) <EOL> except AttributeError : <EOL> raise TypeError ( <EOL> '<STR_LIT>' ) <EOL> def datetime_to_utc_timestamp ( timeval ) : <EOL> """<STR_LIT>""" <EOL> if timeval is not None : <EOL> return timegm ( timeval . utctimetuple ( ) ) + timeval . microsecond / <NUM_LIT> <EOL> def utc_timestamp_to_datetime ( timestamp ) : <EOL> """<STR_LIT>""" <EOL> if timestamp is not None : <EOL> return datetime . fromtimestamp ( timestamp , utc ) <EOL> def timedelta_seconds ( delta ) : <EOL> """<STR_LIT>""" <EOL> return delta . days * <NUM_LIT> * <NUM_LIT> * <NUM_LIT> + delta . seconds + delta . microseconds / <NUM_LIT> <EOL> def datetime_ceil ( dateval ) : <EOL> """<STR_LIT>""" <EOL> if dateval . microsecond > <NUM_LIT:0> : <EOL> return dateval + timedelta ( seconds = <NUM_LIT:1> , microseconds = - dateval . microsecond ) <EOL> return dateval <EOL> def datetime_repr ( dateval ) : <EOL> return dateval . strftime ( '<STR_LIT>' ) if dateval else '<STR_LIT:None>' <EOL> def get_callable_name ( func ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( func , '<STR_LIT>' ) : <EOL> return func . __qualname__ <EOL> f_self = getattr ( func , '<STR_LIT>' , None ) or getattr ( func , '<STR_LIT>' , None ) <EOL> if f_self and hasattr ( func , '<STR_LIT>' ) : <EOL> f_class = f_self if isinstance ( f_self , type ) else f_self . __class__ <EOL> else : <EOL> f_class = getattr ( func , '<STR_LIT>' , None ) <EOL> if f_class and hasattr ( func , '<STR_LIT>' ) : <EOL> return '<STR_LIT>' % ( f_class . __name__ , func . __name__ ) <EOL> if hasattr ( func , '<STR_LIT>' ) : <EOL> if hasattr ( func , '<STR_LIT>' ) : <EOL> return func . __name__ <EOL> return func . __class__ . __name__ <EOL> raise TypeError ( '<STR_LIT>' % func ) <EOL> def obj_to_ref ( obj ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> ref = '<STR_LIT>' % ( obj . __module__ , get_callable_name ( obj ) ) <EOL> obj2 = ref_to_obj ( ref ) <EOL> if obj != obj2 : <EOL> raise ValueError <EOL> except Exception : <EOL> raise ValueError ( '<STR_LIT>' % obj ) <EOL> return ref <EOL> def ref_to_obj ( ref ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( ref , six . string_types ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> if '<STR_LIT::>' not in ref : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> modulename , rest = ref . split ( '<STR_LIT::>' , <NUM_LIT:1> ) <EOL> try : <EOL> obj = __import__ ( modulename ) <EOL> except ImportError : <EOL> raise LookupError ( '<STR_LIT>' % ref ) <EOL> try : <EOL> for name in modulename . split ( '<STR_LIT:.>' ) [ <NUM_LIT:1> : ] + rest . split ( '<STR_LIT:.>' ) : <EOL> obj = getattr ( obj , name ) <EOL> return obj <EOL> except Exception : <EOL> raise LookupError ( '<STR_LIT>' % ref ) <EOL> def maybe_ref ( ref ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( ref , str ) : <EOL> return ref <EOL> return ref_to_obj ( ref ) <EOL> if six . PY2 : <EOL> def repr_escape ( string ) : <EOL> if isinstance ( string , six . text_type ) : <EOL> return string . encode ( '<STR_LIT:ascii>' , '<STR_LIT>' ) <EOL> return string <EOL> else : <EOL> def repr_escape ( string ) : <EOL> return string <EOL> def check_callable_args ( func , args , kwargs ) : <EOL> """<STR_LIT>""" <EOL> pos_kwargs_conflicts = [ ] <EOL> positional_only_kwargs = [ ] <EOL> unsatisfied_args = [ ] <EOL> unsatisfied_kwargs = [ ] <EOL> unmatched_args = list ( args ) <EOL> unmatched_kwargs = list ( kwargs ) <EOL> has_varargs = has_var_kwargs = False <EOL> try : <EOL> sig = signature ( func ) <EOL> except ValueError : <EOL> return <EOL> for param in six . itervalues ( sig . parameters ) : <EOL> if param . kind == param . POSITIONAL_OR_KEYWORD : <EOL> if param . name in unmatched_kwargs and unmatched_args : <EOL> pos_kwargs_conflicts . append ( param . name ) <EOL> elif unmatched_args : <EOL> del unmatched_args [ <NUM_LIT:0> ] <EOL> elif param . name in unmatched_kwargs : <EOL> unmatched_kwargs . remove ( param . name ) <EOL> elif param . default is param . empty : <EOL> unsatisfied_args . append ( param . name ) <EOL> elif param . kind == param . POSITIONAL_ONLY : <EOL> if unmatched_args : <EOL> del unmatched_args [ <NUM_LIT:0> ] <EOL> elif param . name in unmatched_kwargs : <EOL> unmatched_kwargs . remove ( param . name ) <EOL> positional_only_kwargs . append ( param . name ) <EOL> elif param . default is param . empty : <EOL> unsatisfied_args . append ( param . name ) <EOL> elif param . kind == param . KEYWORD_ONLY : <EOL> if param . name in unmatched_kwargs : <EOL> unmatched_kwargs . remove ( param . name ) <EOL> elif param . default is param . empty : <EOL> unsatisfied_kwargs . append ( param . name ) <EOL> elif param . kind == param . VAR_POSITIONAL : <EOL> has_varargs = True <EOL> elif param . kind == param . VAR_KEYWORD : <EOL> has_var_kwargs = True <EOL> if pos_kwargs_conflicts : <EOL> raise ValueError ( '<STR_LIT>' % <EOL> '<STR_LIT:U+002CU+0020>' . join ( pos_kwargs_conflicts ) ) <EOL> if positional_only_kwargs : <EOL> raise ValueError ( '<STR_LIT>' % <EOL> '<STR_LIT:U+002CU+0020>' . join ( positional_only_kwargs ) ) <EOL> if unsatisfied_args : <EOL> raise ValueError ( '<STR_LIT>' % <EOL> '<STR_LIT:U+002CU+0020>' . join ( unsatisfied_args ) ) <EOL> if unsatisfied_kwargs : <EOL> raise ValueError ( <EOL> '<STR_LIT>' % <EOL> '<STR_LIT:U+002CU+0020>' . join ( unsatisfied_kwargs ) ) <EOL> if not has_varargs and unmatched_args : <EOL> raise ValueError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % ( len ( args ) - len ( unmatched_args ) , len ( args ) ) ) <EOL> if not has_var_kwargs and unmatched_kwargs : <EOL> raise ValueError ( <EOL> '<STR_LIT>' % <EOL> '<STR_LIT:U+002CU+0020>' . join ( unmatched_kwargs ) ) </s>
