Пример #1
0
def limit_option(value, min=0, max=None, warn_default=0, alert_default=0):
    valid_options = ['WARN', 'ALERT']

    # Check min/max parameters
    if min is not None:
        try:
            min = int(min)
        except ValueError:
            raise VdtParamError('min', min)
    if max is not None:
        try:
            max = int(max)
        except ValueError:
            raise VdtParamError('max', max)

    # Check value is a list
    if not isinstance(value, list):
        raise VdtTypeError(value)

    # Check for too many or too few values
    if len(value) > len(valid_options):
        raise VdtValueTooLongError(value)
    # elif len(value) < 1:
    #     raise VdtValueTooShortError(value)
    returnDict = {'WARN': warn_default, 'ALERT': alert_default}
    valueDict = {}
    for entry in value:
        # Check list value is a string
        try:
            entry = str(entry)
        except ValueError:
            raise VdtValueError(entry)

        optionParts = entry.split(':', 1)

        limitType = optionParts[0].strip().upper()
        limitVal = optionParts[1]

        if limitType not in valid_options:
            raise VdtValueError(limitType)
        try:
            limitVal = int(limitVal)
        except ValueError:
            raise VdtTypeError(limitVal)

        # Check limits values fall in range
        if max is not None and limitVal > max:
            raise VdtValueTooBigError(limitVal)
        elif min is not None and limitVal < min:
            raise VdtValueTooSmallError(limitVal)

        # Check duplicate
        if limitType in valueDict:
            raise VdtValueError(value)
        valueDict[limitType] = limitVal

    returnDict.update(valueDict)
    # print returnDict
    return ','.join(
        ['%s: %s' % (key, value) for (key, value) in returnDict.items()])
Пример #2
0
def language_list(value):
    """
    Check that the supplied value is a list of languages,
    or a single language, or a special shortcut parameter.
    In the case that it is a special shortcut name, we return the full list
    of relevant languages for that parameter, or throw a validation error
    if that parameter would return an empty list.
    If a single language code is the parameter, this function will return a list
    with that language code as the only member.

    :param Union[str, list[str]] value: Either a string or a list of strings
    String can be any value that is a key of KOLIBRI_LANGUAGE_INFO
    or one of the special strings represented by ALL_LANGUAGES or SUPPORTED_LANGUAGES
    A list must be a list of these strings.
    """
    # Check the supplied value is a list
    if not isinstance(value, list):
        value = [value]

    out = set()
    errors = []
    for entry in value:
        try:
            entry_list = _process_language_string(entry)
            out.update(entry_list)
        except ValueError:
            errors.append(entry)
    if errors:
        raise VdtValueError(errors)

    if not out:
        raise VdtValueError(value)

    return sorted(list(out))
Пример #3
0
def path(value):
    from kolibri.utils.conf import KOLIBRI_HOME

    if not isinstance(value, string_types):
        raise VdtValueError(repr(value))
    # ensure all path arguments, e.g. under section "Paths", are fully resolved and expanded, relative to KOLIBRI_HOME
    return os.path.join(KOLIBRI_HOME, os.path.expanduser(value))
Пример #4
0
def is_option_list(value, *args):
    """Validator for a list of options"""
    if not isinstance(value, list):
        raise VdtTypeError(value)
    for v in value:
        if v not in args:
            raise VdtValueError(v)
    return value
Пример #5
0
def is_timezone(tzstring):
    """tries to convert tzstring into a pytz timezone

    raises a VdtvalueError if tzstring is not valid
    """
    if not tzstring:
        return get_localzone()
    try:
        return pytz.timezone(tzstring)
    except pytz.UnknownTimeZoneError:
        raise VdtValueError("Unknown timezone {}".format(tzstring))
Пример #6
0
def origin_or_port(value):
    """
    Check that the passed value can either be coerced to an integer to supply
    a port, or is a valid origin string.

    :param Union[integer, str] value: Either an integer or a string
    """
    if value != "":
        try:
            value = validate_port_number(int(value))
        except ValueError:
            url = urlparse(value)
            if not url.scheme or not url.netloc:
                raise VdtValueError(value)
            value = urlunparse((url.scheme, url.netloc, "", "", "", ""))
    return value
Пример #7
0
def is_color(color):
    """checks if color represents a valid color

    raises a VdtValueError if color is not valid
    """
    # check if color is
    # 1) the default empty value
    # 2) a color name from the 16 color palette
    # 3) a color index from the 256 color palette
    # 4) a HTML-style color code
    if (color == '' or color in COLORS.keys()
            or (color.isdigit() and int(color) >= 0 and int(color) <= 255)
            or (color.startswith('#') and (len(color) == 7 or len(color) == 4)
                and all(c in '01234567890abcdefABCDEF' for c in color[1:]))):
        return color
    raise VdtValueError(color)
Пример #8
0
def monthdisplay_option(option):
    """checks if *option* is a valid value

    :param option: the option the user set in the config file
    :type option: str
    :returns: firstday, firstfullweek
    :rtype: str/bool
    """
    option = option.lower()
    if option == 'firstday':
        return 'firstday'
    elif option == 'firstfullweek':
        return 'firstfullweek'
    else:
        raise VdtValueError(
            "Invalid value '{}' for option 'monthdisplay', must be one of "
            "'firstday' or 'firstfullweek'".format(option))
Пример #9
0
def path_list(value):
    """
    Check that the supplied value is a semicolon-delimited list of paths.
    Note: we do not guarantee that these paths all currently exist.
    """
    if isinstance(value, string_types):
        value = value.split(";")

    if isinstance(value, list):
        errors = []
        for item in value:
            if not isinstance(item, string_types):
                errors.append(repr(item))
        if errors:
            raise VdtValueError(errors)

    return value
Пример #10
0
def option_list(value, options=None, min=None, max=None):
    if options and not isinstance(options, list):
        raise VdtParamError('options', options)

    if min is not None:
        try:
            min = int(min)
        except ValueError:
            raise VdtParamError('min', min)

    if max is not None:
        try:
            max = int(max)
        except ValueError:
            raise VdtParamError('max', max)

    if min < 0:
        raise VdtParamError('min', min)

    if max < min:
        raise VdtParamError('max', max)

    if isinstance(value, str):
        strVal = value.strip()
        value = []
        if strVal:
            value = [strVal]

    if not isinstance(value, list):
        raise VdtTypeError(value)

    if max and len(value) > max:
        raise VdtValueTooLongError(value)
    elif min and len(value) < min:
        raise VdtValueTooShortError(value)

    if not options:
        return value

    for entry in value:
        if entry not in options:
            raise VdtValueError(value)

    return value
Пример #11
0
def weeknumber_option(option):
    """checks if *option* is a valid value

    :param option: the option the user set in the config file
    :type option: str
    :returns: off, left, right
    :rtype: str/bool
    """
    option = option.lower()
    if option == 'left':
        return 'left'
    elif option == 'right':
        return 'right'
    elif option in ['off', 'false', '0', 'no', 'none']:
        return False
    else:
        raise VdtValueError(
            "Invalid value '{}' for option 'weeknumber', must be one of "
            "'off', 'left' or 'right'".format(option))
Пример #12
0
    def _check_preprocess(self, section, validator):
        # Get configured dataset
        dataset = section['dataset']

        # Validate subsections
        for subsection in section.sections:
            if dataset is not None and dataset != 'None':
                if subsection == 'PREPROCESSOR':
                    # Get validation module
                    module = getattr(validate_preprocess,
                                     'validate_' + dataset)

                    # Valiate preprocessor
                    section[subsection] = self._recursive_validation(
                        config=section[subsection],
                        validator=validator,
                        module=module,
                        key=dataset,
                        section_list=['PREPROCESS', subsection])

                elif subsection == 'CLASSMATCHING':
                    section[subsection] = self._recursive_validation(
                        config=section[subsection],
                        validator=validator,
                        module=module,
                        key=subsection,
                        section_list=['PREPROCESS', subsection])

                else:
                    raise VdtValueError(
                        'Validation faild! Subsection {} in section "PREPROCESS" is no valid subsection!'
                        .format(subsection))

            else:
                section[subsection].clear()

        return section.dict()
Пример #13
0
def is_timedelta(string):
    try:
        return guesstimedeltafstr(string)
    except ValueError:
        raise VdtValueError("Invalid timedelta: {}".format(string))
Пример #14
0
def sample_option(value, samples_default=1, interval_default=1):

    valid_options = ['SAMPLES', 'INTERVAL']

    # Check samples_default and interval_default parameters
    try:
        samples_default = int(samples_default)
    except ValueError:
        raise VdtParamError('samples_default', samples_default)
    if samples_default < 1:
        raise VdtParamError('samples_default', samples_default)
    try:
        interval_default = int(interval_default)
    except ValueError:
        raise VdtParamError('interval_default', interval_default)
    if interval_default < 1:
        raise VdtParamError('interval_default', interval_default)

    returnDict = {'SAMPLES': samples_default, 'INTERVAL': interval_default}

    if value and not isinstance(value, list):
        raise VdtTypeError(value)

    if value is None or len(value) == 0:
        return returnDict

    if len(value) > len(valid_options):
        raise VdtValueTooLongError(value)

    updateDict = {}
    for entry in value:
        try:
            entry = str(entry)
        except ValueError:
            raise VdtValueError(entry)

        optionParts = entry.split(':', 1)
        if len(optionParts) != 2:
            raise VdtValueError(entry)

        limitType = optionParts[0].strip().upper()
        limitVal = optionParts[1]

        if limitType not in valid_options:
            raise VdtValueError(limitType)
        try:
            limitVal = int(limitVal)
        except ValueError:
            raise VdtTypeError(limitVal)

        if limitVal < 1:
            raise VdtValueTooSmallError(limitVal)

        if limitType in updateDict:
            raise VdtValueError(value)

        updateDict[limitType] = limitVal

    returnDict.update(updateDict)
    return ','.join(
        ['%s: %s' % (key, value) for (key, value) in returnDict.items()])
Пример #15
0
def is_timedelta(string):
    try:
        return guesstimedeltafstr(string)
    except ValueError:
        raise VdtValueError(f"Invalid timedelta: {string}")
Пример #16
0
def yes_no(value):
    testValue = value.strip().upper()
    if testValue not in ('Y', 'N'):
        raise VdtValueError(value)
    return testValue == 'Y'
Пример #17
0
def validate_port_number(value):
    if 0 <= value <= 65535:
        return value
    raise VdtValueError(value)
Пример #18
0
    def _check_train(self, section, validator):
        # Validate subsections
        for subsection in section.sections:
            if subsection == 'OPTIMIZER':
                # Get validation module
                module = validate_train.validate_optimizer

                # Set default optimizer if subsection is empty
                if not section[subsection]:
                    section[subsection] = {'Adam': {}}

                # Validate subsection
                section[subsection] = self._recursive_validation(
                    config=section[subsection],
                    validator=validator,
                    module=module,
                    key=subsection,
                    section_list=['TRAIN', subsection])

            elif subsection == 'LOSSES':
                # Get validation module
                module = validate_train.validate_losses

                # Set default loss function if subsection is empty
                if not section[subsection]:
                    section[subsection] = {'CategoricalCrossentropy': {}}

                # Validate subsection
                section[subsection] = self._recursive_validation(
                    config=section[subsection],
                    validator=validator,
                    module=module,
                    key=subsection,
                    section_list=['TRAIN', subsection])

            elif subsection == 'METRICS':
                # Get validation module
                module = validate_train.validate_metrics

                # Validate subsection
                section[subsection] = self._recursive_validation(
                    config=section[subsection],
                    validator=validator,
                    module=module,
                    key=subsection,
                    section_list=['TRAIN', subsection])

            elif subsection == 'CALLBACKS':
                # Get validation module
                module = validate_train.validate_callbacks

                # Validate subsection
                section[subsection] = self._recursive_validation(
                    config=section[subsection],
                    validator=validator,
                    module=module,
                    key=subsection,
                    section_list=['TRAIN', subsection])

            elif subsection.isupper():
                raise VdtValueError(
                    'Validation faild! Subsection {} in section "TRAIN" is no valid subsection!'
                    .format(subsection))

        return section.dict()
Пример #19
0
def url_prefix(value):
    if not isinstance(value, string_types):
        raise VdtValueError(value)
    return value.lstrip("/").rstrip("/") + "/"