Skip to content

documentation

Attributes

Classes

AuthorModel

Bases: BaseModel

Details about an author of a resource.

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

    class Config:
        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

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

Classes

Config
Source code in kiara/models/documentation.py
28
29
class Config:
    title = "Author"
Attributes
title = 'Author' class-attribute

LinkModel

Bases: BaseModel

A description and url for a reference of any kind.

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

    class Config:
        title = "Link"

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

Attributes

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

Classes

Config
Source code in kiara/models/documentation.py
40
41
class Config:
    title = "Link"
Attributes
title = 'Link' class-attribute

AuthorsMetadataModel

Bases: KiaraModel

Information about all authors of a resource.

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

    _kiara_model_id = "metadata.authors"

    class Config:
        extra = Extra.ignore
        title = "Authors"

    _metadata_key = "origin"

    @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.parse_obj(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

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

Classes

Config
Source code in kiara/models/documentation.py
55
56
57
class Config:
    extra = Extra.ignore
    title = "Authors"
Attributes
extra = Extra.ignore class-attribute
title = 'Authors' class-attribute

Functions

from_class(item_cls: Type) classmethod
Source code in kiara/models/documentation.py
61
62
63
64
65
66
@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.parse_obj(merged)
create_renderable(**config: Any) -> RenderableType
Source code in kiara/models/documentation.py
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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 kiara/models/documentation.py
 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
148
149
150
151
152
153
154
155
156
157
158
class ContextMetadataModel(KiaraModel):
    """Information about the context of a resource."""

    _kiara_model_id = "metadata.context"

    class Config:
        extra = 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.parse_obj(merged)

    _metadata_key = "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

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

Classes

Config
Source code in kiara/models/documentation.py
93
94
95
class Config:
    extra = Extra.ignore
    title = "Context"
Attributes
extra = Extra.ignore class-attribute
title = 'Context' class-attribute

Functions

from_class(item_cls: Type) classmethod
Source code in kiara/models/documentation.py
 97
 98
 99
100
101
102
@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.parse_obj(merged)
create_renderable(**config: Any) -> RenderableType
Source code in kiara/models/documentation.py
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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 kiara/models/documentation.py
139
140
141
142
143
144
145
146
147
148
149
150
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 kiara/models/documentation.py
152
153
154
155
156
157
158
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 kiara/models/documentation.py
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
265
266
267
268
269
class DocumentationMetadataModel(KiaraModel):
    """Documentation about a resource."""

    class Config:
        title = "Documentation"

    _kiara_model_id = "metadata.documentation"

    _metadata_key = "documentation"

    @classmethod
    def from_class_doc(cls, item_cls: Type):
        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):

        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]):

        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):

        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):

        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

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

Classes

Config
Source code in kiara/models/documentation.py
164
165
class Config:
    title = "Documentation"
Attributes
title = 'Documentation' class-attribute

Functions

from_class_doc(item_cls: Type) classmethod
Source code in kiara/models/documentation.py
171
172
173
174
175
176
177
178
179
@classmethod
def from_class_doc(cls, item_cls: Type):
    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) classmethod
Source code in kiara/models/documentation.py
181
182
183
184
185
186
187
188
189
190
@classmethod
def from_function(cls, func: Callable):

    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]) classmethod
Source code in kiara/models/documentation.py
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
@classmethod
def from_string(cls, doc: Union[str, None]):

    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) classmethod
Source code in kiara/models/documentation.py
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
@classmethod
def from_dict(cls, data: Mapping):

    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) classmethod
Source code in kiara/models/documentation.py
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
@classmethod
def create(cls, item: Union[Any, None] = None):

    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 kiara/models/documentation.py
267
268
269
def create_renderable(self, **config: Any) -> RenderableType:

    return Markdown(self.full_doc)

Functions