Skip to content

lib_titles

Module to include titles in JSON Schema properties.

_include_titles(schema, definitions_key, verbose=False)

Include property titles, when missing.

This handles both:

  • first-level JSON Schema properties (corresponding to task arguments);
  • properties of JSON Schema definitions (corresponding to task-argument attributes).
PARAMETER DESCRIPTION
schema

TBD

TYPE: _Schema

definitions_key

Either "definitions" (for Pydantic V1) or "$defs" (for Pydantic V2)

TYPE: str

verbose

TYPE: bool DEFAULT: False

Source code in fractal_tasks_core/dev/lib_titles.py
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 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
108
109
110
111
112
113
def _include_titles(
    schema: _Schema,
    definitions_key: str,
    verbose: bool = False,
) -> _Schema:
    """
    Include property titles, when missing.

    This handles both:

    - first-level JSON Schema properties (corresponding to task
        arguments);
    - properties of JSON Schema definitions (corresponding to
        task-argument attributes).

    Args:
        schema: TBD
        definitions_key: Either `"definitions"` (for Pydantic V1) or
            `"$defs"` (for Pydantic V2)
        verbose:
    """
    new_schema = schema.copy()

    if verbose:
        logging.info("[_include_titles] START")
        logging.info(f"[_include_titles] Input schema:\n{schema}")

    # Update first-level properties (that is, task arguments)
    new_properties = _include_titles_for_properties(
        schema["properties"], verbose=verbose
    )
    new_schema["properties"] = new_properties

    if verbose:
        logging.info("[_include_titles] Titles for properties now included.")

    # Update properties of definitions
    if definitions_key in schema.keys():
        new_definitions = schema[definitions_key].copy()
        for def_name, def_schema in new_definitions.items():
            if "properties" not in def_schema.keys():
                if verbose:
                    logging.info(
                        f"Definition schema {def_name} has no 'properties' "
                        "key. Skip."
                    )
            else:
                new_properties = _include_titles_for_properties(
                    def_schema["properties"], verbose=verbose
                )
                new_definitions[def_name]["properties"] = new_properties
        new_schema[definitions_key] = new_definitions

    if verbose:
        logging.info(
            "[_include_titles] Titles for definitions properties now included."
        )
        logging.info("[_include_titles] END")
    return new_schema

_include_titles_for_properties(properties, verbose=False)

Scan through properties of a JSON Schema, and set their title when it is missing.

The title is set to name.title(), where title is a standard string method - see https://docs.python.org/3/library/stdtypes.html#str.title.

PARAMETER DESCRIPTION
properties

TBD

TYPE: dict[str, dict]

Source code in fractal_tasks_core/dev/lib_titles.py
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
def _include_titles_for_properties(
    properties: dict[str, dict],
    verbose: bool = False,
) -> dict[str, dict]:
    """
    Scan through properties of a JSON Schema, and set their title when it is
    missing.

    The title is set to `name.title()`, where `title` is a standard string
    method - see https://docs.python.org/3/library/stdtypes.html#str.title.

    Args:
        properties: TBD
    """
    if verbose:
        logging.info(
            f"[_include_titles_for_properties] Original properties:\n"
            f"{properties}"
        )

    new_properties = properties.copy()
    for prop_name, prop in properties.items():
        if "title" not in prop.keys():
            new_prop = prop.copy()
            new_prop["title"] = prop_name.title()
            new_properties[prop_name] = new_prop
    if verbose:
        logging.info(
            f"[_include_titles_for_properties] New properties:\n"
            f"{new_properties}"
        )
    return new_properties