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)
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))
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))
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)
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))
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))
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)
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
def validate_enumeration_field(field_name, field_data): if not validate_enumeration(field_data): raise IPUZException("Invalid {} value found".format(field_name))
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))
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")
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))
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))
def validate_elements(field_name, field_data, elements): if field_data not in elements: raise IPUZException("Invalid {} value found".format(field_name))
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)
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))
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)
def validate_string(field_name, field_data): if not isinstance(field_data, six.string_types): raise IPUZException("Invalid {} value found".format(field_name))
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)
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))
def validate_bool(field_name, field_data): if not isinstance(field_data, bool): raise IPUZException("Invalid {} value found".format(field_name))