Skip to content

_titles

_include_titles(schema, verbose=False)

Include property titles, when missing.

This handles both first-level JSON Schema properties (corresponding to task arguments) and properties of JSON Schema definitions (corresponding to task-argument attributes).

PARAMETER DESCRIPTION
schema

Original JSON Schema.

TYPE: JSONdictType

verbose

Whether to print more logs.

TYPE: bool DEFAULT: False

Source code in src/fractal_task_tools/_titles.py
40
41
42
43
44
45
46
47
48
49
50
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
def _include_titles(
    schema: JSONdictType,
    verbose: bool = False,
) -> JSONdictType:
    """
    Include property titles, when missing.

    This handles both first-level JSON Schema properties (corresponding to task
    arguments) and properties of JSON Schema definitions (corresponding to
    task-argument attributes).

    Args:
        schema: Original JSON Schema.
        verbose: Whether to print more logs.
    """
    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 src/fractal_task_tools/_titles.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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{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{new_properties}"
        )
    return new_properties