Skip to content

documentation

Attributes

Classes

AuthorModel

Bases: BaseModel

Details about an author of a resource.

Source code in src/kiara/models/documentation.py
27
28
29
30
31
32
33
34
35
class AuthorModel(BaseModel):
    """Details about an author of a resource."""

    model_config = ConfigDict(title="Author")

    name: str = Field(description="The full name of the author.")
    email: Union[EmailStr, None] = Field(
        description="The email address of the author", default=None
    )

Attributes

model_config = ConfigDict(title='Author') class-attribute instance-attribute
name: str = Field(description='The full name of the author.') class-attribute instance-attribute
email: Union[EmailStr, None] = Field(description='The email address of the author', default=None) class-attribute instance-attribute

LinkModel

Bases: BaseModel

A description and url for a reference of any kind.

Source code in src/kiara/models/documentation.py
38
39
40
41
42
43
44
45
46
47
48
class LinkModel(BaseModel):
    """A description and url for a reference of any kind."""

    model_config = ConfigDict(title="Link")

    # TODO: use AnyUrl instead of str
    url: str = Field(description="The url.")
    desc: Union[str, None] = Field(
        description="A short description of the link content.",
        default=DEFAULT_NO_DESC_VALUE,
    )

Attributes

model_config = ConfigDict(title='Link') class-attribute instance-attribute
url: str = Field(description='The url.') class-attribute instance-attribute
desc: Union[str, None] = Field(description='A short description of the link content.', default=DEFAULT_NO_DESC_VALUE) class-attribute instance-attribute

AuthorsMetadataModel

Bases: KiaraModel

Information about all authors of a resource.

Source code in src/kiara/models/documentation.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
78
79
80
81
class AuthorsMetadataModel(KiaraModel):
    """Information about all authors of a resource."""

    _kiara_model_id: ClassVar[str] = "metadata.authors"
    model_config = ConfigDict(extra="ignore", title="Authors")

    _metadata_key: ClassVar[str] = "origin"

    @classmethod
    def from_class(cls, item_cls: Type) -> "AuthorsMetadataModel":
        data = get_metadata_for_python_module_or_class(item_cls)  # type: ignore
        merged = merge_dicts(*data)
        return cls(**merged)

    authors: List[AuthorModel] = Field(
        description="The authors/creators of this item.", default_factory=list
    )

    def create_renderable(self, **config: Any) -> RenderableType:
        table = Table(show_header=False, box=box.SIMPLE)
        table.add_column("Name")
        table.add_column("Email", style="i")

        for author in reversed(self.authors):
            if author.email:
                authors: Tuple[str, Union[str, EmailStr]] = (author.name, author.email)
            else:
                authors = (author.name, "")
            table.add_row(*authors)

        return table

Attributes

model_config = ConfigDict(extra='ignore', title='Authors') class-attribute instance-attribute
authors: List[AuthorModel] = Field(description='The authors/creators of this item.', default_factory=list) class-attribute instance-attribute

Functions

from_class(item_cls: Type) -> AuthorsMetadataModel classmethod
Source code in src/kiara/models/documentation.py
59
60
61
62
63
@classmethod
def from_class(cls, item_cls: Type) -> "AuthorsMetadataModel":
    data = get_metadata_for_python_module_or_class(item_cls)  # type: ignore
    merged = merge_dicts(*data)
    return cls(**merged)
create_renderable(**config: Any) -> RenderableType
Source code in src/kiara/models/documentation.py
69
70
71
72
73
74
75
76
77
78
79
80
81
def create_renderable(self, **config: Any) -> RenderableType:
    table = Table(show_header=False, box=box.SIMPLE)
    table.add_column("Name")
    table.add_column("Email", style="i")

    for author in reversed(self.authors):
        if author.email:
            authors: Tuple[str, Union[str, EmailStr]] = (author.name, author.email)
        else:
            authors = (author.name, "")
        table.add_row(*authors)

    return table

ContextMetadataModel

Bases: KiaraModel

Information about the context of a resource.

Source code in src/kiara/models/documentation.py
 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
114
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
class ContextMetadataModel(KiaraModel):
    """Information about the context of a resource."""

    _kiara_model_id: ClassVar = "metadata.context"
    model_config = ConfigDict(extra="ignore", title="Context")

    @classmethod
    def from_class(cls, item_cls: Type):
        data = get_metadata_for_python_module_or_class(item_cls)  # type: ignore
        merged = merge_dicts(*data)
        return cls(**merged)

    _metadata_key: ClassVar[str] = "properties"

    references: Dict[str, LinkModel] = Field(
        description="References for the item.", default_factory=dict
    )
    tags: List[str] = Field(
        description="A list of tags for the item.", default_factory=list
    )
    labels: Dict[str, str] = Field(
        description="A list of labels for the item.", default_factory=dict
    )

    def create_renderable(self, **config: Any) -> RenderableType:
        table = Table(show_header=False, box=box.SIMPLE)
        table.add_column("Key", style="i")
        table.add_column("Value")

        if self.tags:
            table.add_row("Tags", ", ".join(self.tags))
        if self.labels:
            labels = []
            for k, v in self.labels.items():
                labels.append(f"[i]{k}[/i]: {v}")
            table.add_row("Labels", "\n".join(labels))

        if self.references:
            references = []
            for _k, _v in self.references.items():
                link = f"[link={_v.url}]{_v.url}[/link]"
                references.append(f"[i]{_k}[/i]: {link}")
            table.add_row("References", "\n".join(references))

        return table

    def add_reference(
        self,
        ref_type: str,
        url: str,
        desc: Union[str, None] = None,
        force: bool = False,
    ):
        if ref_type in self.references.keys() and not force:
            raise Exception(f"Reference of type '{ref_type}' already present.")
        link = LinkModel(url=url, desc=desc)
        self.references[ref_type] = link

    def get_url_for_reference(self, ref: str) -> Union[str, None]:
        link = self.references.get(ref, None)
        if not link:
            return None

        return link.url

Attributes

model_config = ConfigDict(extra='ignore', title='Context') class-attribute instance-attribute
references: Dict[str, LinkModel] = Field(description='References for the item.', default_factory=dict) class-attribute instance-attribute
tags: List[str] = Field(description='A list of tags for the item.', default_factory=list) class-attribute instance-attribute
labels: Dict[str, str] = Field(description='A list of labels for the item.', default_factory=dict) class-attribute instance-attribute

Functions

from_class(item_cls: Type) classmethod
Source code in src/kiara/models/documentation.py
90
91
92
93
94
@classmethod
def from_class(cls, item_cls: Type):
    data = get_metadata_for_python_module_or_class(item_cls)  # type: ignore
    merged = merge_dicts(*data)
    return cls(**merged)
create_renderable(**config: Any) -> RenderableType
Source code in src/kiara/models/documentation.py
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
def create_renderable(self, **config: Any) -> RenderableType:
    table = Table(show_header=False, box=box.SIMPLE)
    table.add_column("Key", style="i")
    table.add_column("Value")

    if self.tags:
        table.add_row("Tags", ", ".join(self.tags))
    if self.labels:
        labels = []
        for k, v in self.labels.items():
            labels.append(f"[i]{k}[/i]: {v}")
        table.add_row("Labels", "\n".join(labels))

    if self.references:
        references = []
        for _k, _v in self.references.items():
            link = f"[link={_v.url}]{_v.url}[/link]"
            references.append(f"[i]{_k}[/i]: {link}")
        table.add_row("References", "\n".join(references))

    return table
add_reference(ref_type: str, url: str, desc: Union[str, None] = None, force: bool = False)
Source code in src/kiara/models/documentation.py
130
131
132
133
134
135
136
137
138
139
140
def add_reference(
    self,
    ref_type: str,
    url: str,
    desc: Union[str, None] = None,
    force: bool = False,
):
    if ref_type in self.references.keys() and not force:
        raise Exception(f"Reference of type '{ref_type}' already present.")
    link = LinkModel(url=url, desc=desc)
    self.references[ref_type] = link
get_url_for_reference(ref: str) -> Union[str, None]
Source code in src/kiara/models/documentation.py
142
143
144
145
146
147
def get_url_for_reference(self, ref: str) -> Union[str, None]:
    link = self.references.get(ref, None)
    if not link:
        return None

    return link.url

DocumentationMetadataModel

Bases: KiaraModel

Documentation about a resource.

Source code in src/kiara/models/documentation.py
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
class DocumentationMetadataModel(KiaraModel):
    """Documentation about a resource."""

    model_config = ConfigDict(title="Documentation")

    _kiara_model_id: ClassVar = "metadata.documentation"

    _metadata_key: ClassVar[str] = "documentation"

    @classmethod
    def from_class_doc(cls, item_cls: Type) -> "DocumentationMetadataModel":
        doc: Union[str, None] = None

        if hasattr(item_cls, "type_doc"):
            if callable(item_cls.type_doc):
                try:
                    doc = item_cls.type_doc()
                except Exception as e:
                    log_exception(e)
            else:
                doc = item_cls.type_doc

        if not doc:
            doc = item_cls.__doc__

        if not doc:
            doc = DEFAULT_NO_DESC_VALUE

        doc = inspect.cleandoc(doc)

        return cls.from_string(doc)

    @classmethod
    def from_function(cls, func: Callable) -> "DocumentationMetadataModel":
        doc = func.__doc__

        if not doc:
            doc = DEFAULT_NO_DESC_VALUE

        doc = inspect.cleandoc(doc)
        return cls.from_string(doc)

    @classmethod
    def from_string(cls, doc: Union[str, None]) -> "DocumentationMetadataModel":
        if not doc:
            doc = DEFAULT_NO_DESC_VALUE

        if "\n" in doc:
            desc, doc = doc.split("\n", maxsplit=1)
        else:
            desc = doc
            doc = None

        if doc:
            doc = doc.strip()

        return cls(description=desc.strip(), doc=doc)

    @classmethod
    def from_dict(cls, data: Mapping) -> "DocumentationMetadataModel":
        doc = data.get("doc", None)
        desc = data.get("description", None)
        if desc is None:
            desc = data.get("desc", None)

        if not doc and not desc:
            return cls.from_string(DEFAULT_NO_DESC_VALUE)
        elif doc and not desc:
            return cls.from_string(doc)
        elif desc and not doc:
            return cls.from_string(desc)
        else:
            return cls(description=desc, doc=doc)

    @classmethod
    def create(cls, item: Union[Any, None] = None) -> "DocumentationMetadataModel":
        if not item:
            return cls.from_string(DEFAULT_NO_DESC_VALUE)
        elif isinstance(item, DocumentationMetadataModel):
            return item
        elif isinstance(item, Mapping):
            return cls.from_dict(item)
        if isinstance(item, type):
            return cls.from_class_doc(item)
        elif isinstance(item, str):
            return cls.from_string(item)
        else:
            raise TypeError(f"Can't create documentation from type '{type(item)}'.")

    description: str = Field(
        description="Short description of the item.", default=DEFAULT_NO_DESC_VALUE
    )
    doc: Union[str, None] = Field(
        description="Detailed documentation of the item (in markdown).", default=None
    )

    @property
    def is_set(self) -> bool:
        if self.description and self.description != DEFAULT_NO_DESC_VALUE:
            return True
        else:
            return False

    def _retrieve_data_to_hash(self) -> Any:
        return self.full_doc

    @property
    def full_doc(self):
        if self.doc:
            return f"{self.description}\n\n{self.doc}"
        else:
            return self.description

    def create_renderable(self, **config: Any) -> RenderableType:
        return Markdown(self.full_doc)

Attributes

model_config = ConfigDict(title='Documentation') class-attribute instance-attribute
description: str = Field(description='Short description of the item.', default=DEFAULT_NO_DESC_VALUE) class-attribute instance-attribute
doc: Union[str, None] = Field(description='Detailed documentation of the item (in markdown).', default=None) class-attribute instance-attribute
is_set: bool property
full_doc property

Functions

from_class_doc(item_cls: Type) -> DocumentationMetadataModel classmethod
Source code in src/kiara/models/documentation.py
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
@classmethod
def from_class_doc(cls, item_cls: Type) -> "DocumentationMetadataModel":
    doc: Union[str, None] = None

    if hasattr(item_cls, "type_doc"):
        if callable(item_cls.type_doc):
            try:
                doc = item_cls.type_doc()
            except Exception as e:
                log_exception(e)
        else:
            doc = item_cls.type_doc

    if not doc:
        doc = item_cls.__doc__

    if not doc:
        doc = DEFAULT_NO_DESC_VALUE

    doc = inspect.cleandoc(doc)

    return cls.from_string(doc)
from_function(func: Callable) -> DocumentationMetadataModel classmethod
Source code in src/kiara/models/documentation.py
182
183
184
185
186
187
188
189
190
@classmethod
def from_function(cls, func: Callable) -> "DocumentationMetadataModel":
    doc = func.__doc__

    if not doc:
        doc = DEFAULT_NO_DESC_VALUE

    doc = inspect.cleandoc(doc)
    return cls.from_string(doc)
from_string(doc: Union[str, None]) -> DocumentationMetadataModel classmethod
Source code in src/kiara/models/documentation.py
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
@classmethod
def from_string(cls, doc: Union[str, None]) -> "DocumentationMetadataModel":
    if not doc:
        doc = DEFAULT_NO_DESC_VALUE

    if "\n" in doc:
        desc, doc = doc.split("\n", maxsplit=1)
    else:
        desc = doc
        doc = None

    if doc:
        doc = doc.strip()

    return cls(description=desc.strip(), doc=doc)
from_dict(data: Mapping) -> DocumentationMetadataModel classmethod
Source code in src/kiara/models/documentation.py
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
@classmethod
def from_dict(cls, data: Mapping) -> "DocumentationMetadataModel":
    doc = data.get("doc", None)
    desc = data.get("description", None)
    if desc is None:
        desc = data.get("desc", None)

    if not doc and not desc:
        return cls.from_string(DEFAULT_NO_DESC_VALUE)
    elif doc and not desc:
        return cls.from_string(doc)
    elif desc and not doc:
        return cls.from_string(desc)
    else:
        return cls(description=desc, doc=doc)
create(item: Union[Any, None] = None) -> DocumentationMetadataModel classmethod
Source code in src/kiara/models/documentation.py
224
225
226
227
228
229
230
231
232
233
234
235
236
237
@classmethod
def create(cls, item: Union[Any, None] = None) -> "DocumentationMetadataModel":
    if not item:
        return cls.from_string(DEFAULT_NO_DESC_VALUE)
    elif isinstance(item, DocumentationMetadataModel):
        return item
    elif isinstance(item, Mapping):
        return cls.from_dict(item)
    if isinstance(item, type):
        return cls.from_class_doc(item)
    elif isinstance(item, str):
        return cls.from_string(item)
    else:
        raise TypeError(f"Can't create documentation from type '{type(item)}'.")
create_renderable(**config: Any) -> RenderableType
Source code in src/kiara/models/documentation.py
263
264
def create_renderable(self, **config: Any) -> RenderableType:
    return Markdown(self.full_doc)

Functions