1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 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 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
|
import operator
import sys
from dataclasses import asdict, dataclass, field, replace
from enum import IntEnum
from typing import Any, Dict, Iterator, List, Optional, Tuple, Type
from xsdata.exceptions import CodeGenerationError
from xsdata.formats.converter import converter
from xsdata.formats.dataclass.models.elements import XmlType
from xsdata.models.enums import DataType, Namespace, Tag
from xsdata.models.mixins import ElementBase
from xsdata.utils import namespaces, text
xml_type_map = {
Tag.ANY: XmlType.WILDCARD,
Tag.ANY_ATTRIBUTE: XmlType.ATTRIBUTES,
Tag.ATTRIBUTE: XmlType.ATTRIBUTE,
Tag.CHOICE: XmlType.ELEMENTS,
Tag.ELEMENT: XmlType.ELEMENT,
}
SIMPLE_TYPES = (Tag.EXTENSION, Tag.LIST, Tag.SIMPLE_TYPE, Tag.UNION)
GLOBAL_TYPES = (Tag.ELEMENT, Tag.BINDING_OPERATION, Tag.BINDING_MESSAGE, Tag.MESSAGE)
@dataclass
class Restrictions:
"""Model representation of a dataclass field validation and type
metadata."""
min_occurs: Optional[int] = field(default=None)
max_occurs: Optional[int] = field(default=None)
min_exclusive: Optional[str] = field(default=None)
min_inclusive: Optional[str] = field(default=None)
min_length: Optional[int] = field(default=None)
max_exclusive: Optional[str] = field(default=None)
max_inclusive: Optional[str] = field(default=None)
max_length: Optional[int] = field(default=None)
total_digits: Optional[int] = field(default=None)
fraction_digits: Optional[int] = field(default=None)
length: Optional[int] = field(default=None)
white_space: Optional[str] = field(default=None)
pattern: Optional[str] = field(default=None)
explicit_timezone: Optional[str] = field(default=None)
nillable: Optional[bool] = field(default=None)
sequence: Optional[int] = field(default=None)
tokens: Optional[bool] = field(default=None)
format: Optional[str] = field(default=None)
choice: Optional[int] = field(default=None)
group: Optional[int] = field(default=None)
process_contents: Optional[str] = field(default=None)
path: List[Tuple[str, int, int, int]] = field(default_factory=list)
@property
def is_list(self) -> bool:
"""Return true if max occurs property is larger than one."""
return self.max_occurs is not None and self.max_occurs > 1
@property
def is_optional(self) -> bool:
"""Return true if min occurs property equals zero."""
return self.min_occurs == 0
@property
def is_prohibited(self) -> bool:
return self.max_occurs == 0
def merge(self, source: "Restrictions"):
"""Update properties from another instance."""
self.update(source)
self.path = source.path + self.path
self.sequence = self.sequence or source.sequence
self.choice = self.choice or source.choice
self.tokens = self.tokens or source.tokens
self.format = self.format or source.format
self.group = self.group or source.group
if self.min_occurs is None and source.min_occurs is not None:
self.min_occurs = source.min_occurs
if self.max_occurs is None and source.max_occurs is not None:
self.max_occurs = source.max_occurs
def update(self, source: "Restrictions"):
keys = (
"min_exclusive",
"min_inclusive",
"min_length",
"max_exclusive",
"max_inclusive",
"max_length",
"total_digits",
"fraction_digits",
"length",
"white_space",
"pattern",
"explicit_timezone",
"process_contents",
)
for key in keys:
value = getattr(source, key)
if value is not None:
setattr(self, key, value)
def asdict(self, types: Optional[List[Type]] = None) -> Dict:
"""
Return the initialized only properties as a dictionary.
Skip None or implied values, and optionally use the parent
attribute types to convert relevant options.
"""
result = {}
sorted_types = converter.sort_types(types) if types else []
if self.is_list:
if self.min_occurs is not None and self.min_occurs > 0:
result["min_occurs"] = self.min_occurs
if self.max_occurs is not None and self.max_occurs < sys.maxsize:
result["max_occurs"] = self.max_occurs
elif self.min_occurs == self.max_occurs == 1 and not self.nillable:
result["required"] = True
for key, value in asdict(self).items():
if value is None or key in (
"choice",
"group",
"min_occurs",
"max_occurs",
"path",
):
continue
if key == "process_contents" and value != "skip":
continue
if key.endswith("clusive") and types:
value = converter.deserialize(value, sorted_types)
result[key] = value
return result
def clone(self) -> "Restrictions":
"""Return a deep cloned instance."""
return replace(self)
@classmethod
def from_element(cls, element: ElementBase) -> "Restrictions":
"""Static constructor from a xsd model."""
return cls(**element.get_restrictions())
class AttrCategory(IntEnum):
NATIVE = 0
FORWARD = 1
EXTERNAL = 2
@dataclass(unsafe_hash=True)
class AttrType:
"""Model representation for the typing information for fields and
extensions."""
qname: str
alias: Optional[str] = field(default=None, compare=False)
reference: int = field(default=0, compare=False)
native: bool = field(default=False)
forward: bool = field(default=False)
circular: bool = field(default=False)
substituted: bool = field(default=False, compare=False)
@property
def datatype(self) -> Optional[DataType]:
return DataType.from_qname(self.qname) if self.native else None
@property
def name(self) -> str:
"""Shortcut for qname local name."""
return namespaces.local_name(self.qname)
def is_dependency(self, allow_circular: bool) -> bool:
"""Return true if attribute is not a forward/circular references, and
it's not a native python time."""
return not (
self.forward or self.native or (not allow_circular and self.circular)
)
def clone(self) -> "AttrType":
"""Return a deep cloned instance."""
return replace(self)
@dataclass
class Attr:
"""Model representation for a dataclass field."""
tag: str
name: str = field(compare=False)
local_name: str = field(init=False)
index: int = field(compare=False, default_factory=int)
default: Optional[str] = field(default=None, compare=False)
fixed: bool = field(default=False, compare=False)
mixed: bool = field(default=False, compare=False)
types: List[AttrType] = field(default_factory=list, compare=False)
choices: List["Attr"] = field(default_factory=list, compare=False)
namespace: Optional[str] = field(default=None)
help: Optional[str] = field(default=None, compare=False)
restrictions: Restrictions = field(default_factory=Restrictions, compare=False)
parent: Optional["Class"] = field(default=None, compare=False)
substitution: Optional[str] = field(default=None, compare=False)
def __post_init__(self):
self.local_name = self.name
@property
def key(self) -> str:
return f"{self.tag}.{self.namespace}.{self.local_name}"
@property
def is_attribute(self) -> bool:
"""Return whether this attribute is derived from a xs:attribute or
xs:anyAttribute."""
return self.tag in (Tag.ATTRIBUTE, Tag.ANY_ATTRIBUTE)
@property
def is_enumeration(self) -> bool:
"""Return whether this attribute is derived from a xs:enumeration."""
return self.tag == Tag.ENUMERATION
@property
def is_dict(self) -> bool:
"""Return whether this attribute is a mapping of values."""
return self.tag == Tag.ANY_ATTRIBUTE
@property
def is_factory(self) -> bool:
"""Return whether this attribute is a list of items or a mapping."""
return self.is_list or self.is_dict or self.is_tokens
@property
def is_forward_ref(self) -> bool:
return any(tp.circular or tp.forward for tp in self.types)
@property
def is_group(self) -> bool:
"""Return whether this attribute is derived from a xs:group or
xs:attributeGroup."""
return self.tag in (Tag.ATTRIBUTE_GROUP, Tag.GROUP)
@property
def is_list(self) -> bool:
"""Return whether this attribute is a list of values."""
return self.restrictions.is_list
@property
def is_prohibited(self) -> bool:
"""Return whether this attribute is prohibited."""
return self.restrictions.is_prohibited
@property
def is_nameless(self) -> bool:
"""Return whether this attribute has a local name that will be used
during parsing/serialization."""
return self.tag not in (Tag.ATTRIBUTE, Tag.ELEMENT)
@property
def is_nillable(self) -> bool:
return self.restrictions.nillable is True
@property
def is_optional(self) -> bool:
"""Return whether this attribute is not required."""
return self.restrictions.is_optional
@property
def is_suffix(self) -> bool:
"""Return whether this attribute is not derived from a xs element with
mode suffix."""
return self.index == sys.maxsize
@property
def is_xsi_type(self) -> bool:
"""Return whether this attribute qualified name is equal to
xsi:type."""
return self.namespace == Namespace.XSI.uri and self.name == "type"
@property
def is_tokens(self) -> bool:
"""Return whether this attribute is a list of values."""
return self.restrictions.tokens is True
@property
def is_wildcard(self) -> bool:
"""Return whether this attribute is derived from xs:anyAttribute or
xs:any."""
return self.tag in (Tag.ANY_ATTRIBUTE, Tag.ANY)
@property
def is_any_type(self) -> bool:
return any(tp is object for tp in self.get_native_types())
@property
def native_types(self) -> List[Type]:
"""Return a list of all builtin data types."""
return list(set(self.get_native_types()))
@property
def user_types(self) -> Iterator[AttrType]:
"""Return an iterator of all the user defined types."""
for tp in self.types:
if not tp.native:
yield tp
@property
def slug(self) -> str:
return text.alnum(self.name)
@property
def xml_type(self) -> Optional[str]:
"""Return the xml node type this attribute is mapped to."""
return xml_type_map.get(self.tag)
def clone(self) -> "Attr":
"""Return a deep cloned instance."""
return replace(
self,
types=[x.clone() for x in self.types],
restrictions=self.restrictions.clone(),
)
def get_native_types(self) -> Iterator[Type]:
for tp in self.types:
datatype = tp.datatype
if datatype:
yield datatype.type
def can_be_restricted(self) -> bool:
"""Return whether this attribute can be restricted."""
return self.xml_type not in (Tag.ATTRIBUTE, None)
@dataclass(unsafe_hash=True)
class Extension:
"""Model representation of a dataclass base class."""
tag: str
type: AttrType
restrictions: Restrictions = field(hash=False)
def clone(self) -> "Extension":
"""Return a deep cloned instance."""
return replace(
self,
type=self.type.clone(),
restrictions=self.restrictions.clone(),
)
class Status(IntEnum):
RAW = 0
UNGROUPING = 10
UNGROUPED = 11
FLATTENING = 20
FLATTENED = 21
SANITIZING = 30
SANITIZED = 31
RESOLVING = 40
RESOLVED = 41
FINALIZING = 50
FINALIZED = 51
@dataclass
class Class:
"""Model representation of a dataclass with fields, base/inner classes and
additional metadata settings."""
qname: str
tag: str
location: str
mixed: bool = field(default=False)
abstract: bool = field(default=False)
nillable: bool = field(default=False)
local_type: bool = field(default=False)
status: Status = field(default=Status.RAW)
container: Optional[str] = field(default=None)
package: Optional[str] = field(default=None)
module: Optional[str] = field(default=None)
namespace: Optional[str] = field(default=None)
help: Optional[str] = field(default=None)
meta_name: Optional[str] = field(default=None)
default: Any = field(default=None, compare=False)
fixed: bool = field(default=False, compare=False)
substitutions: List[str] = field(default_factory=list)
extensions: List[Extension] = field(default_factory=list)
attrs: List[Attr] = field(default_factory=list)
inner: List["Class"] = field(default_factory=list)
ns_map: Dict = field(default_factory=dict)
@property
def name(self) -> str:
"""Shortcut for qname local name."""
return namespaces.local_name(self.qname)
@property
def slug(self) -> str:
return text.alnum(self.name)
@property
def ref(self) -> int:
return id(self)
@property
def target_namespace(self) -> Optional[str]:
return namespaces.target_uri(self.qname)
@property
def has_suffix_attr(self) -> bool:
"""Return whether it includes a suffix attribute."""
return any(attr.is_suffix for attr in self.attrs)
@property
def has_help_attr(self) -> bool:
"""Return whether it includes at least one attr with help content."""
return any(attr.help and attr.help.strip() for attr in self.attrs)
@property
def is_complex(self) -> bool:
"""Return whether this instance is derived from a xs:element or
xs:complexType."""
return self.tag in (Tag.ELEMENT, Tag.COMPLEX_TYPE)
@property
def is_element(self) -> bool:
"""Return whether this instance is derived from a non abstract
xs:element."""
return self.tag == Tag.ELEMENT
@property
def is_enumeration(self) -> bool:
"""Return whether all attributes are derived from xs:enumeration."""
return len(self.attrs) > 0 and all(attr.is_enumeration for attr in self.attrs)
@property
def is_global_type(self) -> bool:
"""Return whether this instance is a non-abstract element, wsdl binding
class or a complex type without simple content."""
return (not self.abstract and self.tag in GLOBAL_TYPES) or (
self.tag == Tag.COMPLEX_TYPE and not self.is_simple_type
)
@property
def is_group(self) -> bool:
"""Return whether this attribute is derived from a xs:group or
xs:attributeGroup."""
return self.tag in (Tag.ATTRIBUTE_GROUP, Tag.GROUP)
@property
def is_nillable(self) -> bool:
"""Return whether this class represents a nillable xml element."""
return self.nillable or any(x.restrictions.nillable for x in self.extensions)
@property
def is_mixed(self) -> bool:
"""Return whether this class supports mixed content."""
return self.mixed or any(x.mixed for x in self.attrs)
@property
def is_restricted(self) -> bool:
return any(
True for extension in self.extensions if extension.tag == Tag.RESTRICTION
)
@property
def is_service(self) -> bool:
"""Return whether this instance is derived from wsdl:operation."""
return self.tag == Tag.BINDING_OPERATION
@property
def is_simple_type(self) -> bool:
"""Return whether the class represents a simple text type."""
return (
len(self.attrs) == 1
and self.attrs[0].tag in SIMPLE_TYPES
and not self.extensions
)
@property
def references(self) -> Iterator[int]:
def all_refs():
for ext in self.extensions:
yield ext.type.reference
for attr in self.attrs:
for tp in attr.types:
yield tp.reference
for choice in attr.choices:
for ctp in choice.types:
yield ctp.reference
for inner in self.inner:
yield from inner.references
for ref in all_refs():
if ref:
yield ref
@property
def target_module(self) -> str:
"""Return the target module this class is assigned to."""
if self.package and self.module:
return f"{self.package}.{self.module}"
if self.module:
return self.module
raise CodeGenerationError(
f"Class `{self.name}` has not been assigned to a module yet!"
)
def clone(self) -> "Class":
"""Return a deep cloned instance."""
inners = [inner.clone() for inner in self.inner]
extensions = [extension.clone() for extension in self.extensions]
attrs = [attr.clone() for attr in self.attrs]
return replace(self, inner=inners, extensions=extensions, attrs=attrs)
def dependencies(self, allow_circular: bool = False) -> Iterator[str]:
"""
Return a set of dependencies for the given class.
Collect:
* base classes
* attribute types
* attribute choice types
* recursively go through the inner classes
* Ignore inner class references
* Ignore native types.
"""
types = {ext.type for ext in self.extensions}
for attr in self.attrs:
types.update(attr.types)
types.update(tp for choice in attr.choices for tp in choice.types)
for tp in types:
if tp.is_dependency(allow_circular):
yield tp.qname
for inner in self.inner:
yield from inner.dependencies(allow_circular)
def has_forward_ref(self) -> bool:
"""Return whether this class has any forward references."""
for attr in self.attrs:
if attr.is_forward_ref:
return True
if any(choice for choice in attr.choices if choice.is_forward_ref):
return True
return any(inner.has_forward_ref() for inner in self.inner)
@dataclass
class Import:
"""
Model representation of a python import statement.
:param qname:
:param source:
:param alias:
"""
qname: str
source: str
alias: Optional[str] = field(default=None)
@property
def name(self) -> str:
"""Shortcut for qname local name."""
return namespaces.local_name(self.qname)
@property
def slug(self) -> str:
return text.alnum(self.name)
# Getters used all over the codegen process
get_location = operator.attrgetter("location")
get_name = operator.attrgetter("name")
get_qname = operator.attrgetter("qname")
get_tag = operator.attrgetter("tag")
get_restriction_choice = operator.attrgetter("restrictions.choice")
get_restriction_sequence = operator.attrgetter("restrictions.sequence")
get_slug = operator.attrgetter("slug")
get_target_namespace = operator.attrgetter("target_namespace")
is_enumeration = operator.attrgetter("is_enumeration")
is_group = operator.attrgetter("is_group")
|