(value, type)
return rval
Generic evaluation of types; returns true if the value is of type,
        or false if it is not.

        Returns a tuple of (bool, obj), where bool determines success and obj
        is the value returned as type.
Generic evaluation of types; returns true if the value is of type,
        or false if it is not.
Generic evaluation of types ; returns true if the value is of type or false if it is not .
def eval_type(value, type):
    """ Generic evaluation of types; returns true if the value is of type,
        or false if it is not.

        Returns a tuple of (bool, obj), where bool determines success and obj
        is the value returned as type.
    """
    rval = (False, None)
    if type == "int":
        try:
            rval = (True, int(value))
        except:
            rval = (False, None)
    elif type == "bool":
        if value in ['True', 'true', '1']:
            rval = (True, True)
        elif value in ['False', 'false', '0']:
            rval = (True, False)
    elif type == "ip":
        ip = value.split('.')
        if len(ip) != 4:
            rval = (False, None)
        else:
            try:
                socket.inet_aton(value)
                rval = (True, value)
            except:
                rval = (False, None)
    elif type == "str":
        # anything can be a string
        rval = (True, str(value))
    elif type == "ipmask":
        ip = value.split('.')
        if len(ip) != 4:
            rval = (False, None)
        else:
            try:
                int(ip[0])
                int(ip[1])
                int(ip[2])
                rval = (True, value) if '/' in ip[3] else (False, None)
            except:
                rval = (False, None)
    elif type == "regex":
        try:
            tmp = re.compile(value)
            rval = (True, tmp)
        except re.error:
            rval = (False, None)
    elif type == 'list':
        # comma delimited
        try:
            rval = (True, value.split(','))
        except:
            rval = (False, None)
    elif type == 'file':
        if does_file_exist(value):
            rval = (True, value)
    else:
        Error('Unrecognized type: %s' % type)
    return rval
def eval_type ( value , type ) : rval = ( False , None ) if type == "int" : try : rval = ( True , int ( value ) ) except : rval = ( False , None ) elif type == "bool" : if value in [ 'True' , 'true' , '1' ] : rval = ( True , True ) elif value in [ 'False' , 'false' , '0' ] : rval = ( True , False ) elif type == "ip" : ip = value . split ( '.' ) if len ( ip ) != 4 : rval = ( False , None ) else : try : socket . inet_aton ( value ) rval = ( True , value ) except : rval = ( False , None ) elif type == "str" : # anything can be a string rval = ( True , str ( value ) ) elif type == "ipmask" : ip = value . split ( '.' ) if len ( ip ) != 4 : rval = ( False , None ) else : try : int ( ip [ 0 ] ) int ( ip [ 1 ] ) int ( ip [ 2 ] ) rval = ( True , value ) if '/' in ip [ 3 ] else ( False , None ) except : rval = ( False , None ) elif type == "regex" : try : tmp = re . compile ( value ) rval = ( True , tmp ) except re . error : rval = ( False , None ) elif type == 'list' : # comma delimited try : rval = ( True , value . split ( ',' ) ) except : rval = ( False , None ) elif type == 'file' : if does_file_exist ( value ) : rval = ( True , value ) else : Error ( 'Unrecognized type: %s' % type ) return rval