예제 #1
0
    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)))
예제 #2
0
파일: __init__.py 프로젝트: Naora/pyleton
 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")
예제 #3
0
파일: __init__.py 프로젝트: Naora/pyleton
    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)
예제 #4
0
파일: core.py 프로젝트: highdxy/wc_utils
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)
예제 #5
0
 def check_dir_exists(value):
     value = is_string(value)
     if not isdir(value):
         raise ValidateError('Directory {} doesnt exist'.format(value))
     return value
예제 #6
0
 def validate(self, value):
     return is_string(value.strip(), min=self.min, max=self.max)
예제 #7
0
 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)
예제 #8
0
 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)
예제 #9
0
 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)
예제 #10
0
 def test_should_validate_string(self):
     string = "Hello"
     is_valid = is_string(string)
     self.assertTrue(is_valid)
예제 #11
0
 def validate(self, value):
     return is_string(value.strip(), min=self.min, max=self.max)