Validator

This module provides highlevel checking of parsed data for lowlevel decoders.

It handles the unicode in keys, builds dicts from flat arrays and so on.

class ftp.decoders.validator.Field(keyword, descr, epub=None)[source]

This class is used to represent and parse specific “key: val” pair.

When you create the object, keyword and descr is specified. Optionally also epub parameter, which is corresponding key in EPublication structure.

Assingning value to the class is done by calling check(), which sets the value, if the key parameter matches keyword.

Parameters:
  • keyword (str) – Key for the data pair.
  • descr (str) – Description of the data pair. Used in exceptions.
  • epub (str, default None) – Corresponding keyword in EPublication structure.
keyword = None

Keyword agains check() will try to match.

descr = None

Description of the data pair.

value = None

Internal value. Set when check() successfully matched the keyword.

epub = None

Corresponding key in EPublication structure.

check(key, value)[source]

Check whether key matchs the keyword. If so, set the value to value.

Parameters:
  • key (str) – Key which will be matched with keyword.
  • value (str) – Value which will be assigned to value if keys matches.
Returns:

True/False: Whether the key matched keyword.

is_valid()[source]

Return True if value is set.

Note

value is set by calling check() with proper key.

class ftp.decoders.validator.FieldParser[source]

Class used to make sure, that all fields in metadata are present.

See /api/required for list of required fields.

process(key, val)[source]

Try to look for key in all required and optional fields. If found, set the val.

is_valid()[source]
Returns:True/False whether ALL required fields are set.
get_epublication()[source]
Returns:Structure when the object is_valid().
Return type:EPublication
Raises:MetaParsingException – When the object is not valid.
ftp.decoders.validator.check_structure(data)[source]

Check whether the structure is flat dictionary. If not, try to convert it to dictionary.

Parameters:data – Whatever data you have (dict/tuple/list).
Returns:When the conversion was successful or data was already good.
Return type:dict
Raises:MetaParsingException – When the data couldn’t be converted or had bad structure.