Skip to content

pretty_print

Attributes

Classes

PrettyPrintConfig

Bases: KiaraModuleConfig

Source code in kiara/modules/included_core_modules/pretty_print.py
23
24
25
26
27
28
29
30
31
32
class PrettyPrintConfig(KiaraModuleConfig):

    source_type: str = Field(description="The value type of the source value.")
    target_type: str = Field(description="The value type of the rendered value.")

    @validator("source_type")
    def validate_source_type(cls, value):
        if value == "render_config":
            raise ValueError(f"Invalid source type: {value}.")
        return value

Attributes

source_type: str = Field(description='The value type of the source value.') class-attribute
target_type: str = Field(description='The value type of the rendered value.') class-attribute

Functions

validate_source_type(value)
Source code in kiara/modules/included_core_modules/pretty_print.py
28
29
30
31
32
@validator("source_type")
def validate_source_type(cls, value):
    if value == "render_config":
        raise ValueError(f"Invalid source type: {value}.")
    return value

PrettyPrintModule

Bases: KiaraModule

Source code in kiara/modules/included_core_modules/pretty_print.py
 35
 36
 37
 38
 39
 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
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
class PrettyPrintModule(KiaraModule):

    _module_type_name: str = None  # type: ignore
    _config_cls = PrettyPrintConfig

    @classmethod
    def retrieve_supported_render_combinations(cls) -> Iterable[Tuple[str, str]]:

        result = []
        for attr in dir(cls):
            if (
                len(attr) <= 19
                or not attr.startswith("pretty_print__")
                or "__as__" not in attr
            ):
                continue

            attr = attr[14:]
            end_start_type = attr.find("__as__")
            source_type = attr[0:end_start_type]
            target_type = attr[end_start_type + 6 :]
            result.append((source_type, target_type))
        return result

    # def create_persistence_config_schema(self) -> Optional[Mapping[str, Mapping[str, Any]]]:
    #     return None

    def _retrieve_module_characteristics(self) -> ModuleCharacteristics:
        return DEFAULT_IDEMPOTENT_INTERNAL_MODULE_CHARACTERISTICS

    def create_inputs_schema(
        self,
    ) -> Mapping[str, Union[ValueSchema, Mapping[str, Any]]]:

        source_type = self.get_config_value("source_type")
        assert source_type not in ["target", "base_name"]

        schema = {
            "value": {"type": source_type, "doc": "The value to render."},
            "render_config": {
                "type": "any",
                "doc": "Value type dependent render configuration.",
                "optional": True,
            },
        }

        return schema

    def create_outputs_schema(
        self,
    ) -> Mapping[str, Union[ValueSchema, Mapping[str, Any]]]:

        return {
            "rendered_value": {
                "type": self.get_config_value("target_type"),
                "doc": "The rendered value.",
            }
        }

    def process(self, inputs: ValueMap, outputs: ValueMap):

        source_type = self.get_config_value("source_type")
        target_type = self.get_config_value("target_type")

        value = inputs.get_value_obj("value")
        render_config = inputs.get_value_data("render_config")

        func_name = f"pretty_print__{source_type}__as__{target_type}"

        func = getattr(self, func_name)
        # TODO: check function signature is valid

        if render_config is None:
            render_config = {}

        result = func(value=value, render_config=render_config)

        outputs.set_value("rendered_value", result)

Attributes

_config_cls = PrettyPrintConfig class-attribute

Functions

retrieve_supported_render_combinations() -> Iterable[Tuple[str, str]] classmethod
Source code in kiara/modules/included_core_modules/pretty_print.py
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
@classmethod
def retrieve_supported_render_combinations(cls) -> Iterable[Tuple[str, str]]:

    result = []
    for attr in dir(cls):
        if (
            len(attr) <= 19
            or not attr.startswith("pretty_print__")
            or "__as__" not in attr
        ):
            continue

        attr = attr[14:]
        end_start_type = attr.find("__as__")
        source_type = attr[0:end_start_type]
        target_type = attr[end_start_type + 6 :]
        result.append((source_type, target_type))
    return result
create_inputs_schema() -> Mapping[str, Union[ValueSchema, Mapping[str, Any]]]
Source code in kiara/modules/included_core_modules/pretty_print.py
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
def create_inputs_schema(
    self,
) -> Mapping[str, Union[ValueSchema, Mapping[str, Any]]]:

    source_type = self.get_config_value("source_type")
    assert source_type not in ["target", "base_name"]

    schema = {
        "value": {"type": source_type, "doc": "The value to render."},
        "render_config": {
            "type": "any",
            "doc": "Value type dependent render configuration.",
            "optional": True,
        },
    }

    return schema
create_outputs_schema() -> Mapping[str, Union[ValueSchema, Mapping[str, Any]]]
Source code in kiara/modules/included_core_modules/pretty_print.py
83
84
85
86
87
88
89
90
91
92
def create_outputs_schema(
    self,
) -> Mapping[str, Union[ValueSchema, Mapping[str, Any]]]:

    return {
        "rendered_value": {
            "type": self.get_config_value("target_type"),
            "doc": "The rendered value.",
        }
    }
process(inputs: ValueMap, outputs: ValueMap)
Source code in kiara/modules/included_core_modules/pretty_print.py
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
def process(self, inputs: ValueMap, outputs: ValueMap):

    source_type = self.get_config_value("source_type")
    target_type = self.get_config_value("target_type")

    value = inputs.get_value_obj("value")
    render_config = inputs.get_value_data("render_config")

    func_name = f"pretty_print__{source_type}__as__{target_type}"

    func = getattr(self, func_name)
    # TODO: check function signature is valid

    if render_config is None:
        render_config = {}

    result = func(value=value, render_config=render_config)

    outputs.set_value("rendered_value", result)

ValueTypePrettyPrintModule

Bases: KiaraModule

Source code in kiara/modules/included_core_modules/pretty_print.py
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
class ValueTypePrettyPrintModule(KiaraModule):

    _module_type_name = "pretty_print.value"
    _config_cls = PrettyPrintConfig

    def _retrieve_module_characteristics(self) -> ModuleCharacteristics:
        return DEFAULT_IDEMPOTENT_INTERNAL_MODULE_CHARACTERISTICS

    def create_inputs_schema(
        self,
    ) -> Mapping[str, Union[ValueSchema, Mapping[str, Any]]]:

        source_type = self.get_config_value("source_type")
        assert source_type not in ["target", "base_name"]

        schema = {
            "value": {
                "type": source_type,
                "doc": "The value to render.",
                "optional": True,
            },
            "render_config": {
                "type": "any",
                "doc": "Value type dependent render configuration.",
                "optional": True,
            },
        }

        return schema

    def create_outputs_schema(
        self,
    ) -> Mapping[str, Union[ValueSchema, Mapping[str, Any]]]:

        return {
            "rendered_value": {
                "type": self.get_config_value("target_type"),
                "doc": "The rendered value.",
            }
        }

    def process(self, inputs: ValueMap, outputs: ValueMap):

        # source_type = self.get_config_value("source_type")
        target_type = self.get_config_value("target_type")

        source_value = inputs.get_value_obj("value")
        render_config = inputs.get_value_obj("render_config")

        if not source_value.is_set:
            outputs.set_value("rendered_value", "-- none/not set --")
            return

        try:
            data_type_cls = source_value.data_type_info.data_type_class.get_class()
            data_type = data_type_cls(**source_value.value_schema.type_config)
        except Exception as e:
            source_data_type = source_value.data_type_name
            log_message("data_type.unknown", data_type=source_data_type, error=e)

            from kiara.data_types.included_core_types import AnyType

            data_type = AnyType()

        func_name = f"pretty_print_as__{target_type}"
        func = getattr(data_type, func_name)

        render_config_dict = render_config.data
        if render_config_dict is None:
            render_config_dict = {}

        result = func(value=source_value, render_config=render_config_dict)
        # TODO: check we have the correct type?
        outputs.set_value("rendered_value", result)

Attributes

_config_cls = PrettyPrintConfig class-attribute

Functions

create_inputs_schema() -> Mapping[str, Union[ValueSchema, Mapping[str, Any]]]
Source code in kiara/modules/included_core_modules/pretty_print.py
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
def create_inputs_schema(
    self,
) -> Mapping[str, Union[ValueSchema, Mapping[str, Any]]]:

    source_type = self.get_config_value("source_type")
    assert source_type not in ["target", "base_name"]

    schema = {
        "value": {
            "type": source_type,
            "doc": "The value to render.",
            "optional": True,
        },
        "render_config": {
            "type": "any",
            "doc": "Value type dependent render configuration.",
            "optional": True,
        },
    }

    return schema
create_outputs_schema() -> Mapping[str, Union[ValueSchema, Mapping[str, Any]]]
Source code in kiara/modules/included_core_modules/pretty_print.py
145
146
147
148
149
150
151
152
153
154
def create_outputs_schema(
    self,
) -> Mapping[str, Union[ValueSchema, Mapping[str, Any]]]:

    return {
        "rendered_value": {
            "type": self.get_config_value("target_type"),
            "doc": "The rendered value.",
        }
    }
process(inputs: ValueMap, outputs: ValueMap)
Source code in kiara/modules/included_core_modules/pretty_print.py
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
def process(self, inputs: ValueMap, outputs: ValueMap):

    # source_type = self.get_config_value("source_type")
    target_type = self.get_config_value("target_type")

    source_value = inputs.get_value_obj("value")
    render_config = inputs.get_value_obj("render_config")

    if not source_value.is_set:
        outputs.set_value("rendered_value", "-- none/not set --")
        return

    try:
        data_type_cls = source_value.data_type_info.data_type_class.get_class()
        data_type = data_type_cls(**source_value.value_schema.type_config)
    except Exception as e:
        source_data_type = source_value.data_type_name
        log_message("data_type.unknown", data_type=source_data_type, error=e)

        from kiara.data_types.included_core_types import AnyType

        data_type = AnyType()

    func_name = f"pretty_print_as__{target_type}"
    func = getattr(data_type, func_name)

    render_config_dict = render_config.data
    if render_config_dict is None:
        render_config_dict = {}

    result = func(value=source_value, render_config=render_config_dict)
    # TODO: check we have the correct type?
    outputs.set_value("rendered_value", result)

PrettyPrintAnyValueModule

Bases: PrettyPrintModule

Source code in kiara/modules/included_core_modules/pretty_print.py
191
192
193
class PrettyPrintAnyValueModule(PrettyPrintModule):

    _module_type_name = "pretty_print.any.value"

Functions