def _is_string_list_or_none(self, value, min=None, max=None): """ Validation function for a list of string values or None. Reference: https://github.com/DiffSK/configobj/blob/master/src/configobj/validate.py """ # Check if value is string if isinstance(value, str): # Convert string value try: value = ast.literal_eval(value) except ValueError: raise ValidateError( 'The value "{}" could not be converted!'.format(value)) except Exception as e: raise e if isinstance(value, list): return [is_string(elem) for elem in is_list(value, min, max)] elif value is None: return value else: raise ValidateError( 'A value of type {} was passed when a string list or None type was expected!' .format(type(value)))
def is_coerced_list(value, min_val=None, max_val=None): """ Checks if a value is a list, if not coerces it to a list """ if not isinstance(value, list): value = [value] return validate.is_list(value, min_val, max_val)
def is_coerced_list(value, min_val=None, max_val=None): """ Checks if a value is a list, if not coerces it to a list """ if isinstance(value, str): value = [value] return validate.is_list(value, min_val, max_val)
def extend_is_dir(value, minimum=None, maximum=None): u""" This function is extended is_dir(). This function was able to take ListType or StringType as argument. """ if isinstance(value, list): return [is_dir(member) for member in validate.is_list(value, minimum, maximum)] else: return is_dir(value)
def extend_is_dir(value, minimum=None, maximum=None): u""" This function is extended is_dir(). This function was able to take ListType or StringType as argument. """ if isinstance(value, list): return [ is_dir(member) for member in validate.is_list(value, minimum, maximum) ] else: return is_dir(value)
def any_checker(value): ''' Convert value to its built-in data type if possible Convert a string value to its built-in data type (integer, float, boolean, str or list of these) if possible Args: value (:obj:`object`): a value to be converted Returns: :obj:`type`: the converted value Raises: :obj:`VdtTypeError`: if the value cannot be converted ''' if not isinstance(value, float) or not math.isnan(value): # if statement needed because `_handle_value` doesn't seem to be able to handle nan value, _ = ConfigObj()._handle_value(value) # parse to integer try: return is_integer(value) except VdtTypeError: pass # parse to float try: return is_float(value) except VdtTypeError: pass # parse to bool try: return is_boolean(value) except VdtTypeError: pass # parse to list try: return [any_checker(val) for val in is_list(value)] except VdtTypeError: pass # parse to string return is_string(value)
def _is_dict_list(self, value, min=None, max=None): """ Validation function for a list of nested dicts. Reference: https://github.com/DiffSK/configobj/blob/master/src/configobj/validate.py """ # Check if value is string if isinstance(value, str): # Convert string value try: value = ast.literal_eval(value) except ValueError: raise ValidateError( 'The value "{}" could not be converted to a dict!'.format( value)) except Exception as e: raise e return [self._is_dict(elem) for elem in is_list(value, min, max)]
def force_list(value, min=None, max=None): r""" Check that a value is a list, coercing strings into a list with one member. You can optionally specify the minimum and maximum number of members. A minimum of greater than one will fail if the user only supplies a string. The difference to :func:`validate.force_list` is that this test will return an empty list instead of `['']` if the config value matches `r'\s*,?\s*'`. """ if not isinstance(value, (list, tuple)): value = [value] rlist = is_list(value, min, max) if rlist == ['']: rlist = [] return rlist
def _is_float_list(self, value, min=None, max=None): """ Validation function for a list of float values. Replacement (extension) of the original 'is_float_list' function. Reference: https://github.com/DiffSK/configobj/blob/master/src/configobj/validate.py """ # Check if value is string if isinstance(value, str): # Convert string value try: value = ast.literal_eval(value) except ValueError: raise ValidateError( 'The value "{}" could not be converted!'.format(value)) except Exception as e: raise e if isinstance(value, list): return [is_float(elem) for elem in is_list(value, min, max)] else: raise ValidateError( 'A value of type {} was passed when a float list was expected!' .format(type(value)))
def is_option_list(sValue, *aOptions): """Validator function for option_list configspec type.""" return [is_option(sMem, *aOptions) for sMem in is_list(sValue)]