Exemplo n.º 1
0
def validate_list(field_name, field_data, element_name, validate_element):
    if not isinstance(field_data, list):
        raise IPUZException("Invalid {} value found".format(field_name))
    for element in field_data:
        if not validate_element(element):
            message = "Invalid {} in {} element found".format(
                element_name, field_name)
            raise IPUZException(message)
Exemplo n.º 2
0
def validate_groupspec_dict(field_name, field_data):
    if not isinstance(field_data, dict):
        raise IPUZException("Invalid {} value found".format(field_name))
    for key, value in field_data.items():
        validate_string(field_name, key)
        if not key:
            raise IPUZException("Invalid {} value found".format(field_name))
        if not validate_groupspec(value):
            raise IPUZException("Invalid {} value found".format(field_name))
Exemplo n.º 3
0
def validate_clues(field_name, field_data):
    if not isinstance(field_data, dict):
        raise IPUZException("Invalid {} value found".format(field_name))
    for direction, clues in field_data.items():
        if not validate_direction(direction) or not isinstance(clues, list):
            raise IPUZException("Invalid {} value found".format(field_name))
        for clue in clues:
            if not validate_clue(clue):
                raise IPUZException(
                    "Invalid Clue in {} element found".format(field_name))
Exemplo n.º 4
0
def validate_list_of_lists(field_name, field_data, element_name,
                           validate_element):
    if (not isinstance(field_data, list)
            or any(not isinstance(e, list) for e in field_data)):
        raise IPUZException("Invalid {} value found".format(field_name))
    for line in field_data:
        for element in line:
            if not validate_element(element):
                message = "Invalid {} in {} element found".format(
                    element_name, field_name)
                raise IPUZException(message)
Exemplo n.º 5
0
def validate_stylespec(style_spec):
    if (style_spec is not None and not isinstance(style_spec,
                                                  (dict, six.string_types))):
        raise IPUZException("StyleSpec is not a name, dictionary or None")
    if isinstance(style_spec, dict):
        for key, value in style_spec.items():
            if key not in IPUZ_STYLESPEC_VALIDATORS:
                raise IPUZException(
                    "StyleSpec contains invalid specifier: {}".format(key))
            if not IPUZ_STYLESPEC_VALIDATORS[key](value):
                raise IPUZException(
                    "StyleSpec has an invalid {} value".format(key))
Exemplo n.º 6
0
def validate_dimensions(field_name, field_data):
    if not isinstance(field_data, dict):
        raise IPUZException("Invalid {} value found".format(field_name))
    for key, value in field_data.items():
        if key not in ("width", "height"):
            raise IPUZException("Invalid {} value found".format(field_name))
    for key in ["width", "height"]:
        if key not in field_data:
            raise IPUZException(
                "Mandatory field {} of dimensions is missing".format(key))
        if type(field_data[key]) is not int:
            raise IPUZException(
                "Invalid {} value in dimensions field found".format(key))
        if field_data[key] < 1:
            raise IPUZException(
                "Field {} of dimensions is less than one".format(key))
Exemplo n.º 7
0
def validate_solution(field_name, field_data):
    if (not isinstance(field_data, (dict, list))
            and not isinstance(field_data, six.string_types)):
        raise IPUZException("Invalid {} value found".format(field_name))
    if isinstance(field_data, list):
        validate_list_of_strings(field_name, field_data)
    if isinstance(field_data, dict):
        validate_groupspec_dict(field_name, field_data)
Exemplo n.º 8
0
Arquivo: core.py Projeto: maiamcc/ipuz
def read(data, puzzlekinds=None):
    try:
        if data.endswith(')'):
            data = data[data.index('(') + 1:-1]
        json_data = json.loads(data)
        if not isinstance(json_data, dict):
            raise ValueError
    except (AttributeError, ValueError):
        raise IPUZException("No valid JSON could be found")
    for field in IPUZ_MANDATORY_FIELDS:
        if field not in json_data:
            raise IPUZException("Mandatory field {} is missing".format(field))

    validate_version("version", json_data["version"], IPUZ_VERSIONS)
    version = get_version_number(json_data["version"])

    for field, value in json_data.items():
        if field in IPUZ_FIELD_VALIDATORS[version]:
            IPUZ_FIELD_VALIDATORS[version][field](field, value)

    for kind in json_data["kind"]:
        if puzzlekinds is not None and kind not in puzzlekinds:
            raise IPUZException("Unsupported kind value found")

    for kind in json_data["kind"]:
        for official_kind, kind_details in IPUZ_PUZZLEKINDS.items():
            if not kind.startswith(official_kind):
                continue
            for field in kind_details["mandatory"]:
                if field not in json_data:
                    raise IPUZException(
                        "Mandatory field {} is missing".format(field))
            for field, value in json_data.items():
                if field in kind_details["validators"][version]:
                    validator = kind_details["validators"][version][field]
                    try:
                        validator(field, value)
                    except TypeError:
                        validator[0](field, value, *validator[1:])
    return json_data
Exemplo n.º 9
0
def validate_enumeration_field(field_name, field_data):
    if not validate_enumeration(field_data):
        raise IPUZException("Invalid {} value found".format(field_name))
Exemplo n.º 10
0
def validate_non_negative_int(field_name, field_data):
    validate_int(field_name, field_data)
    if field_data < 0:
        raise IPUZException("Invalid {} value found".format(field_name))
Exemplo n.º 11
0
def validate_version(field_name, field_data, valid_versions):
    version_number = get_version_number(field_data)
    if version_number is None:
        raise IPUZException("Invalid version value found")
    if version_number not in valid_versions:
        raise IPUZException("Unsupported version value found")
Exemplo n.º 12
0
def validate_kind(field_name, field_data):
    if not isinstance(field_data, list) or not field_data:
        raise IPUZException("Invalid {} value found".format(field_name))
    for element in field_data:
        if not isinstance(element, six.string_types) or not element:
            raise IPUZException("Invalid {} value found".format(field_name))
Exemplo n.º 13
0
def validate_date(field_name, field_data):
    try:
        datetime.strptime(field_data, '%m/%d/%Y')
    except ValueError:
        raise IPUZException("Invalid date format: {}".format(field_data))
Exemplo n.º 14
0
def validate_elements(field_name, field_data, elements):
    if field_data not in elements:
        raise IPUZException("Invalid {} value found".format(field_name))
Exemplo n.º 15
0
def validate_list_of_strings(field_name, field_data):
    if not isinstance(field_data, list):
        raise IPUZException("Invalid {} value found".format(field_name))
    for element in field_data:
        validate_string(field_name, field_data)
Exemplo n.º 16
0
def validate_empty(field_name, field_data):
    if (type(field_data) is not int
            and not isinstance(field_data, six.string_types)):
        raise IPUZException("Invalid {} value found".format(field_name))
Exemplo n.º 17
0
def validate_dict_of_strings(field_name, field_data):
    if not isinstance(field_data, dict):
        raise IPUZException("Invalid {} value found".format(field_name))
    for key, value in field_data.items():
        validate_string(field_name, key)
        validate_string(field_name, value)
Exemplo n.º 18
0
def validate_string(field_name, field_data):
    if not isinstance(field_data, six.string_types):
        raise IPUZException("Invalid {} value found".format(field_name))
Exemplo n.º 19
0
def validate_styles(field_name, field_data):
    from ipuz.structures import validate_stylespec
    if not isinstance(field_data, dict):
        raise IPUZException("Invalid {} value found".format(field_name))
    for _, stylespec in field_data.items():
        validate_stylespec(stylespec)
Exemplo n.º 20
0
def validate_int(field_name, field_data):
    # type instead of isinstance as bool inherits from int
    if type(field_data) is not int:
        raise IPUZException("Invalid {} value found".format(field_name))
Exemplo n.º 21
0
def validate_bool(field_name, field_data):
    if not isinstance(field_data, bool):
        raise IPUZException("Invalid {} value found".format(field_name))