<s> import six <EOL> str = six . text_type <EOL> from six . moves import urllib <EOL> import dateutil . parser <EOL> import datetime , re , types , time , struct , socket <EOL> from . errors . saklientexception import SaklientException <EOL> class Util ( object ) : <EOL> @ staticmethod <EOL> def exists_path ( obj , path ) : <EOL> a_path = path . split ( "<STR_LIT:.>" ) <EOL> for seg in a_path : <EOL> if obj is None : return False <EOL> if not isinstance ( obj , dict ) : return False <EOL> if seg == "<STR_LIT>" : continue <EOL> if seg not in obj : return False <EOL> obj = obj [ seg ] <EOL> return True <EOL> @ staticmethod <EOL> def get_by_path ( obj , path ) : <EOL> a_path = path . split ( "<STR_LIT:.>" ) <EOL> for seg in a_path : <EOL> if obj is None : return None <EOL> if not isinstance ( obj , dict ) : return None <EOL> if seg == "<STR_LIT>" : continue <EOL> if seg not in obj : return None <EOL> obj = obj [ seg ] <EOL> return obj <EOL> @ staticmethod <EOL> def get_by_path_any ( objects , pathes ) : <EOL> for obj in objects : <EOL> for path in pathes : <EOL> ret = Util . get_by_path ( obj , path ) <EOL> if ret is not None : return ret <EOL> return None <EOL> @ staticmethod <EOL> def set_by_path ( obj , path , value ) : <EOL> a_path = path . split ( "<STR_LIT:.>" ) <EOL> key = a_path . pop ( ) <EOL> for seg in a_path : <EOL> if seg == "<STR_LIT>" : continue <EOL> if seg not in obj : obj [ seg ] = { } <EOL> obj = obj [ seg ] <EOL> obj [ key ] = value <EOL> @ staticmethod <EOL> def create_class_instance ( classPath , arguments ) : <EOL> ret = None <EOL> p = classPath . split ( "<STR_LIT:.>" ) <EOL> c = p . pop ( ) <EOL> m = __import__ ( classPath . lower ( ) , fromlist = [ c ] ) <EOL> ret = getattr ( m , c ) ( * arguments ) <EOL> if ret is None : <EOL> raise Exception . new ( "<STR_LIT>" + classPath ) <EOL> return ret <EOL> @ staticmethod <EOL> def str2date ( s ) : <EOL> if s is None : <EOL> return None <EOL> return dateutil . parser . parse ( s ) <EOL> @ staticmethod <EOL> def date2str ( d ) : <EOL> if d is None : <EOL> return None <EOL> return d . strftime ( "<STR_LIT>" ) <EOL> @ staticmethod <EOL> def ip2long ( ip ) : <EOL> if not isinstance ( ip , ( six . text_type , six . string_types ) ) or not re . match ( r"<STR_LIT>" , ip ) : return None <EOL> ret = None <EOL> try : <EOL> ret = struct . unpack ( "<STR_LIT>" , socket . inet_aton ( ip ) ) [ <NUM_LIT:0> ] <EOL> except : <EOL> ret = None <EOL> return ret <EOL> @ staticmethod <EOL> def long2ip ( num ) : <EOL> if num is None : return None <EOL> ret = None <EOL> v = int ( num ) <EOL> if v < <NUM_LIT:0> : v += <NUM_LIT:1> << <NUM_LIT:32> <EOL> try : <EOL> ret = socket . inet_ntoa ( struct . pack ( '<STR_LIT>' , v ) ) <EOL> except : <EOL> ret = None <EOL> return ret <EOL> @ staticmethod <EOL> def url_encode ( s ) : <EOL> return urllib . parse . quote_plus ( s ) <EOL> @ staticmethod <EOL> def sort_array ( a ) : <EOL> ret = list ( a ) <EOL> ret . sort ( ) <EOL> return ret <EOL> @ staticmethod <EOL> def sleep ( sec ) : <EOL> time . sleep ( sec ) <EOL> @ staticmethod <EOL> def validate_arg_count ( actual , expected ) : <EOL> if actual < expected : raise SaklientException ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> @ staticmethod <EOL> def validate_type ( value , type_name , force = False ) : <EOL> is_ok = False <EOL> if not force : <EOL> if type_name == "<STR_LIT>" or type_name == "<STR_LIT>" or value is None : return <EOL> clazz = type ( value ) <EOL> if ( type_name == "<STR_LIT>" ) : <EOL> is_ok = isinstance ( value , types . FunctionType ) <EOL> elif type_name == "<STR_LIT:str>" : <EOL> is_ok = isinstance ( value , ( six . text_type , six . string_types ) ) <EOL> elif ( type_name in { "<STR_LIT:float>" , "<STR_LIT:int>" , "<STR_LIT:bool>" , "<STR_LIT>" , "<STR_LIT:list>" } ) : <EOL> is_ok = type ( value ) . __name__ == type_name <EOL> else : <EOL> p = type_name . split ( "<STR_LIT:.>" ) <EOL> if <NUM_LIT:1> < len ( p ) : <EOL> c = p . pop ( ) <EOL> m = __import__ ( "<STR_LIT:.>" . join ( p ) , fromlist = [ c ] ) <EOL> is_ok = isinstance ( value , getattr ( m , c ) ) <EOL> else : <EOL> c = globals ( ) [ "<STR_LIT>" ] [ type_name ] <EOL> is_ok = isinstance ( value , c ) <EOL> if not is_ok : <EOL> raise SaklientException ( "<STR_LIT>" , "<STR_LIT>" + type_name + "<STR_LIT>" + str ( type ( value ) ) + "<STR_LIT:)>" ) </s>
