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 createFiles(self, files): """ create the files that was specified """ if is_string(files): files = files.replace("__NAME__", self.name) fileList = files.split(",") for f in fileList: doc = open(f, "w") # set some init stuff here doc.close() else: raise Exception("Invalid file name")
def __init__(self, name, path): if is_string(name): self.name = name else: raise Exception("Invalid name of project") if is_string(path): self.path = path else: raise Exception("Invalid name of project path") self.savedPath = os.getcwd() os.chdir(path) try: os.mkdir(name) except OSError: cin = raw_input("This project already exist.\nDo you want delete this project? O/n ") if cin in ("o", "O"): shutil.rmtree(name) os.mkdir(name) else: return 0 os.chdir(name) self.stripFolder(pyletonConfig)
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 check_dir_exists(value): value = is_string(value) if not isdir(value): raise ValidateError('Directory {} doesnt exist'.format(value)) return value
def validate(self, value): return is_string(value.strip(), min=self.min, max=self.max)
def test_should_raise_exception_for_text_below_range(self): with self.assertRaises(VdtValueTooShortError): some_text = "wrong" min_range = 11 max_range = 23 is_string(some_text, min_range, max_range)
def test_should_raise_exception_for_text_above_range(self): with self.assertRaises(VdtValueTooLongError): some_text = "something wrong" min_range = 0 max_range = 11 is_string(some_text, min_range, max_range)
def test_should_validate_text_range(self): some_text = "something" min_range = 0 max_range = 11 isvalid = is_string(some_text, min_range, max_range) self.assertTrue(isvalid)
def test_should_validate_string(self): string = "Hello" is_valid = is_string(string) self.assertTrue(is_valid)