|
20 | 20 | Definitions that are used in the documentation via mkdocs-macro-plugin.
|
21 | 21 | """
|
22 | 22 |
|
23 |
| -from types import UnionType |
24 |
| -from pydantic.fields import FieldInfo |
25 | 23 | import yaml
|
26 | 24 | import json
|
27 | 25 | import os.path
|
28 |
| -from typing import Annotated, Any, Union, get_args, get_origin |
29 |
| -from typing import Literal |
| 26 | +from typing import get_args |
| 27 | + |
30 | 28 | from inspect import isclass
|
31 | 29 |
|
32 | 30 | from pydantic.fields import FieldInfo
|
|
54 | 52 | package_markdown_from_package,
|
55 | 53 | )
|
56 | 54 |
|
57 |
| -exported_config_models = set() # type: ignore |
58 |
| - |
59 |
| - |
60 |
| -doc_snippets = { |
61 |
| - 'query': query_documentation, |
62 |
| - 'owner': owner_documentation, |
63 |
| - 'archive-required': archive_required_documentation, |
64 |
| -} |
65 |
| - |
66 |
| - |
67 |
| -def get_field_type_info(field: FieldInfo) -> tuple[str, set[Any]]: |
68 |
| - """Used to recursively walk through a type definition, building up a cleaned |
69 |
| - up type name and returning all of the classes that were used. |
70 |
| -
|
71 |
| - Args: |
72 |
| - type_: The type to inspect. Can be any valid type definition. |
73 |
| -
|
74 |
| - Returns: |
75 |
| - Tuple containing the cleaned up type name and a set of classes |
76 |
| - found inside. |
77 |
| - """ |
78 |
| - classes = set() |
79 |
| - annotation = field.annotation |
80 |
| - |
81 |
| - def get_class_name(ann: Any) -> str: |
82 |
| - if hasattr(ann, '__name__'): |
83 |
| - name = ann.__name__ |
84 |
| - return 'None' if name == 'NoneType' else name |
85 |
| - return str(ann) |
86 |
| - |
87 |
| - def _recursive_extract(ann: Any, type_str: str = '') -> str: |
88 |
| - nonlocal classes |
89 |
| - |
90 |
| - origin = get_origin(ann) |
91 |
| - args = get_args(ann) |
92 |
| - |
93 |
| - if origin is None and issubclass(ann, Enum): |
94 |
| - classes.add(ann) |
95 |
| - # Determine base type for Enums |
96 |
| - if issubclass(ann, str): |
97 |
| - return get_class_name(str) |
98 |
| - elif issubclass(ann, int): |
99 |
| - return get_class_name(int) |
100 |
| - else: |
101 |
| - return get_class_name(ann) |
102 |
| - elif origin is None: |
103 |
| - classes.add(ann) |
104 |
| - return get_class_name(ann) |
105 |
| - if origin is list: |
106 |
| - classes.add(origin) |
107 |
| - if type_str: |
108 |
| - type_str += '[' + _recursive_extract(args[0]) + ']' |
109 |
| - else: |
110 |
| - type_str = 'list[' + _recursive_extract(args[0]) + ']' |
111 |
| - elif origin is dict: |
112 |
| - classes.add(origin) |
113 |
| - if type_str: |
114 |
| - type_str += ( |
115 |
| - '[' |
116 |
| - + _recursive_extract(args[0]) |
117 |
| - + ', ' |
118 |
| - + _recursive_extract(args[1]) |
119 |
| - + ']' |
120 |
| - ) |
121 |
| - else: |
122 |
| - type_str = ( |
123 |
| - 'dict[' |
124 |
| - + _recursive_extract(args[0]) |
125 |
| - + ', ' |
126 |
| - + _recursive_extract(args[1]) |
127 |
| - + ']' |
128 |
| - ) |
129 |
| - |
130 |
| - elif origin is UnionType or origin is Union: |
131 |
| - # Handle Union types (e.g., Optional[str] is equivalent to Union[str, None]) |
132 |
| - union_types = [] |
133 |
| - for arg in args: |
134 |
| - union_types.append(_recursive_extract(arg)) |
135 |
| - type_str = ' | '.join(union_types) |
136 |
| - elif origin is Literal: |
137 |
| - classes.add(origin) |
138 |
| - return get_class_name( |
139 |
| - type(args[0]) |
140 |
| - ) # Add name of the literal value (e.g., str) |
141 |
| - elif origin is Annotated: |
142 |
| - # Extract the underlying type from Annotated |
143 |
| - return _recursive_extract(args[0]) |
144 |
| - else: |
145 |
| - # Handle generic types |
146 |
| - classes.add(origin) |
147 |
| - return get_class_name(ann) |
148 |
| - |
149 |
| - return type_str |
150 |
| - |
151 |
| - type_name = _recursive_extract(annotation) |
152 |
| - return type_name, classes |
153 |
| - |
154 |
| - |
155 |
| -def get_field_description(field: FieldInfo) -> str | None: |
156 |
| - """Retrieves the description for a pydantic field as a markdown string. |
157 |
| -
|
158 |
| - Args: |
159 |
| - field: The pydantic field to inspect. |
160 |
| -
|
161 |
| - Returns: |
162 |
| - Markdown string for the description. |
163 |
| - """ |
164 |
| - value = field.description |
165 |
| - if value: |
166 |
| - value = utils.strip(value) |
167 |
| - value = value.replace('\n\n', '<br/>').replace('\n', ' ') |
168 |
| - |
169 |
| - return value |
170 |
| - |
171 |
| - |
172 |
| -def get_field_default(field: FieldInfo) -> str | None: |
173 |
| - """Retrieves the default value from a pydantic field as a markdown string. |
174 |
| -
|
175 |
| - Args: |
176 |
| - field: The pydantic field to inspect. |
177 |
| -
|
178 |
| - Returns: |
179 |
| - Markdown string for the default value. |
180 |
| - """ |
181 |
| - default_value = field.default |
182 |
| - if default_value is not None: |
183 |
| - if isinstance(default_value, dict | BaseModel): |
184 |
| - default_value = 'Complex object, default value not displayed.' |
185 |
| - elif default_value == '': |
186 |
| - default_value = '""' |
187 |
| - else: |
188 |
| - default_value = f'`{default_value}`' |
189 |
| - return default_value |
190 |
| - |
191 |
| - |
192 |
| -def get_field_options(field: FieldInfo) -> dict[str, str | None]: |
193 |
| - """Retrieves a dictionary of value-description pairs from a pydantic field. |
194 |
| -
|
195 |
| - Args: |
196 |
| - field: The pydantic field to inspect. |
197 |
| -
|
198 |
| - Returns: |
199 |
| - Dictionary containing the possible options and their description for |
200 |
| - this field. The description may be None indicating that it does not exist. |
201 |
| - """ |
202 |
| - options: dict[str, str | None] = {} |
203 |
| - if isclass(field.annotation) and issubclass(field.annotation, Enum): |
204 |
| - for x in field.annotation: |
205 |
| - options[str(x.value)] = None |
206 |
| - return options |
207 |
| - |
208 |
| - |
209 |
| -def get_field_deprecated(field: FieldInfo) -> bool: |
210 |
| - """Returns whether the given pydantic field is deprecated or not. |
211 |
| -
|
212 |
| - Args: |
213 |
| - field: The pydantic field to inspect. |
214 |
| -
|
215 |
| - Returns: |
216 |
| - Whether the field is deprecated. |
217 |
| - """ |
218 |
| - if field.deprecated: |
219 |
| - return True |
220 |
| - return False |
| 55 | +from nomad.config.models.plugins import ParserEntryPoint, EntryPointType |
221 | 56 |
|
222 | 57 |
|
223 | 58 | class MyYamlDumper(yaml.Dumper):
|
|
0 commit comments