context
xsdata.formats.dataclass.context
XmlContext
The models context class.
The context is responsible to provide binding metadata for models and their fields.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
element_name_generator | Callable | Default element name generator | return_input |
attribute_name_generator | Callable | Default attribute name generator | return_input |
class_type | str | Default class type | 'dataclasses' |
models_package | Optional[str] | Restrict auto locate to a specific package | None |
Attributes:
Name | Type | Description |
---|---|---|
cache | dict[type, XmlMeta] | Internal cache for binding metadata instances |
xsi_cache | dict[str, list[type]] | Internal cache for xsi types to class locations |
sys_modules | The number of loaded sys modules |
Source code in xsdata/formats/dataclass/context.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 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 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 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 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
|
__init__(element_name_generator=return_input, attribute_name_generator=return_input, class_type='dataclasses', models_package=None)
Initialize the context.
Source code in xsdata/formats/dataclass/context.py
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
|
reset()
Reset all internal caches.
Source code in xsdata/formats/dataclass/context.py
61 62 63 64 65 |
|
get_builder(globalns=None)
Return a new xml meta builder instance.
Source code in xsdata/formats/dataclass/context.py
67 68 69 70 71 72 73 74 75 76 77 |
|
fetch(clazz, parent_ns=None, xsi_type=None)
Build the model metadata for the given class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
clazz | type | The requested dataclass type | required |
parent_ns | Optional[str] | The inherited parent namespace | None |
xsi_type | Optional[str] | if present it means that the given clazz is derived and the lookup procedure needs to check and match a dataclass model to the qualified name instead. | None |
Returns:
Type | Description |
---|---|
XmlMeta | A xml meta instance |
Source code in xsdata/formats/dataclass/context.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
|
build_xsi_cache()
Index all imported data classes by their xsi:type qualified name.
Source code in xsdata/formats/dataclass/context.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
|
is_binding_model(clazz)
Return whether the clazz is a binding model.
If the models package is not empty also validate the class is located within that package.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
clazz | type[T] | The class type to inspect | required |
Returns:
Type | Description |
---|---|
bool | The bool result. |
Source code in xsdata/formats/dataclass/context.py
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
|
find_types(qname)
Find all classes that match the given xsi:type qname.
- Ignores native schema types, xs:string, xs:float, xs:int, ...
- Rebuild cache if new modules were imported since last run
Parameters:
Name | Type | Description | Default |
---|---|---|---|
qname | str | A namespace qualified name | required |
Returns:
Type | Description |
---|---|
list[type[T]] | A list of the matched classes. |
Source code in xsdata/formats/dataclass/context.py
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
|
find_type(qname)
Return the last imported class that matches the given xsi:type qname.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
qname | str | A namespace qualified name | required |
Returns:
Type | Description |
---|---|
Optional[type[T]] | A class type or None if no matches. |
Source code in xsdata/formats/dataclass/context.py
160 161 162 163 164 165 166 167 168 169 170 |
|
find_type_by_fields(field_names)
Find a data class that matches best the given list of field names.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
field_names | set[str] | A set of field names | required |
Returns:
Type | Description |
---|---|
Optional[type[T]] | The best matching class or None if no matches. The class must |
Optional[type[T]] | have all the fields. If more than one classes have all the given |
Optional[type[T]] | fields, return the one with the least extra fields. |
Source code in xsdata/formats/dataclass/context.py
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 |
|
find_subclass(clazz, qname)
Find a subclass for the given clazz and xsi:type qname.
Compare all classes that match the given xsi:type qname and return the first one that is either a subclass or shares the same parent class as the original class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
clazz | type | The input clazz type | required |
qname | str | The xsi:type to lookup from cache | required |
Returns:
Type | Description |
---|---|
Optional[type] | The matching class type or None if no matches. |
Source code in xsdata/formats/dataclass/context.py
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 |
|
build(clazz, parent_ns=None, globalns=None)
Fetch or build the binding metadata for the given class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
clazz | type | A class type | required |
parent_ns | Optional[str] | The inherited parent namespace | None |
globalns | Optional[dict[str, Callable]] | Override the global python namespace | None |
Returns:
Type | Description |
---|---|
XmlMeta | The class binding metadata instance. |
Source code in xsdata/formats/dataclass/context.py
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
|
build_recursive(clazz, parent_ns=None)
Build the binding metadata for the given class and all of its dependencies.
This method is used in benchmarks!
Parameters:
Name | Type | Description | Default |
---|---|---|---|
clazz | type | The class type | required |
parent_ns | Optional[str] | The inherited parent namespace | None |
Source code in xsdata/formats/dataclass/context.py
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
|
local_names_match(names, clazz)
Check if the given field names match the given class type.
Silently ignore, typing errors. These classes are from third party libraries most of them time.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
names | set[str] | A set of field names | required |
clazz | type | The class type to inspect | required |
Returns:
Type | Description |
---|---|
bool | Whether the class contains all the field names. |
Source code in xsdata/formats/dataclass/context.py
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
|
is_derived(obj, clazz)
classmethod
Return whether the obj is a subclass or a parent of the given class type.
Source code in xsdata/formats/dataclass/context.py
293 294 295 296 297 298 299 300 301 302 |
|
get_subclasses(clazz)
classmethod
Return an iterator of the given class subclasses.
Source code in xsdata/formats/dataclass/context.py
304 305 306 307 308 309 310 |
|