Skip to content

The utilities submodule

This module provides utility functions.

These utility functions are used by the high level functions defined in safedata_validator.zenodo and safedata_validator.server. The functions are used to check that the correct inputs have been supplied in the correct order.

check_file_is_excel(file_path)

Check that the file path provided points to a .xlsx file.

Parameters:

Name Type Description Default
file_path Path

A path to the file to check

required

Returns:

Type Description
bool

A bool specifying whether or not the file is an .xlsx file.

Source code in safedata_validator/utilities.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
def check_file_is_excel(file_path: Path) -> bool:
    """Check that the file path provided points to a .xlsx file.

    Args:
        file_path: A path to the file to check

    Returns:
        A bool specifying whether or not the file is an .xlsx file.
    """

    if not file_path.is_file():
        return False

    if file_path.suffix != ".xlsx":
        return False

    return True

check_file_is_json(file_path)

Check that the file path provided points to a JSON file.

Parameters:

Name Type Description Default
file_path Path

A path to the file to check

required

Returns:

Type Description
bool

A bool specifying whether or not the file is a JSON file.

Source code in safedata_validator/utilities.py
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
def check_file_is_json(file_path: Path) -> bool:
    """Check that the file path provided points to a JSON file.

    Args:
        file_path: A path to the file to check

    Returns:
        A bool specifying whether or not the file is a JSON file.
    """

    if not file_path.is_file():
        return False

    if file_path.suffix != ".json":
        return False

    return True

check_file_is_metadata_json(file_path)

Function to check if a file is a safedata metadata style JSON file.

Parameters:

Name Type Description Default
file_path Path

A path to the file to check

required

Returns:

Type Description
bool

A bool specifying whether or not the file is a safedata metadata style JSON

bool

file.

Source code in safedata_validator/utilities.py
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
def check_file_is_metadata_json(file_path: Path) -> bool:
    """Function to check if a file is a safedata metadata style JSON file.

    Args:
        file_path: A path to the file to check

    Returns:
        A bool specifying whether or not the file is a safedata metadata style JSON
        file.
    """

    # First check that the file is actually JSON
    if not check_file_is_json(file_path):
        return False

    # Minimal set of keys to identify metadata JSON
    required_keys = ["project_ids", "authors", "access", "funders"]

    with open(file_path) as file:
        contents = simplejson.load(file)

        # Check if all required keys are present
        missing_keys = [key for key in required_keys if key not in contents]

        if missing_keys:
            return False
        else:
            return True

check_file_is_zenodo_json(file_path)

Function to check if a file is a Zenodo JSON file.

Parameters:

Name Type Description Default
file_path Path

A path to the file to check

required

Returns:

Type Description
bool

A bool specifying whether or not the file is a Zenodo JSON file.

Source code in safedata_validator/utilities.py
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
def check_file_is_zenodo_json(file_path: Path) -> bool:
    """Function to check if a file is a Zenodo JSON file.

    Args:
        file_path: A path to the file to check

    Returns:
        A bool specifying whether or not the file is a Zenodo JSON file.
    """

    # First check that the file is actually JSON
    if not check_file_is_json(file_path):
        return False

    # Minimal set of keys to identify zenodo JSON
    required_keys = ["created", "modified", "conceptrecid", "metadata"]

    with open(file_path) as file:
        contents = simplejson.load(file)

        # Check if all required keys are present
        missing_keys = [key for key in required_keys if key not in contents]

        if missing_keys:
            return False
        else:
            return True