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 604
|
import abc
import copy
import importlib
import random
import unittest
from dataclasses import is_dataclass
from typing import Any, Callable, Dict, List, Optional, Sequence, Type, TypeVar
from xsdata.codegen.models import (
Attr,
AttrType,
Class,
Extension,
Import,
Restrictions,
Status,
)
from xsdata.formats.dataclass.models.elements import XmlMeta, XmlType, XmlVar
from xsdata.models.dtd import (
Dtd,
DtdAttribute,
DtdAttributeDefault,
DtdAttributeType,
DtdContent,
DtdContentOccur,
DtdContentType,
DtdElement,
DtdElementType,
)
from xsdata.models.enums import DataType, Namespace, Tag
from xsdata.utils.collections import first
from xsdata.utils.namespaces import build_qname
T = TypeVar("T")
DEFAULT_NS_MAP = {
Namespace.XS.prefix: Namespace.XS.uri,
Namespace.XSI.prefix: Namespace.XSI.uri,
Namespace.XML.prefix: Namespace.XML.uri,
Namespace.XLINK.prefix: Namespace.XLINK.uri,
}
def load_class(output: str, clazz_name: str) -> Any:
search = "Generating package: "
start = len(search)
packages = [line[start:] for line in output.splitlines() if line.startswith(search)]
for package in reversed(packages):
try:
module = importlib.import_module(package)
return getattr(module, clazz_name)
except (ModuleNotFoundError, AttributeError):
pass
raise ModuleNotFoundError(f"Class `{clazz_name}` not found.")
class FactoryTestCase(unittest.TestCase):
def setUp(self):
super().setUp()
ClassFactory.reset()
AttrFactory.reset()
AttrTypeFactory.reset()
ExtensionFactory.reset()
PackageFactory.reset()
XmlVarFactory.reset()
XmlMetaFactory.reset()
DtdElementFactory.reset()
DtdAttributeFactory.reset()
DtdContentFactory.reset()
DtdFactory.reset()
class Factory(abc.ABC):
counter = 0
model: Type
@classmethod
@abc.abstractmethod
def create(cls, **kwargs: Any) -> Any:
"""Abstract method create."""
@classmethod
def reset(cls):
cls.counter = 65
@classmethod
def next_letter(cls) -> str:
cls.counter += 1
return chr(cls.counter)
@classmethod
def list(cls, number: int, **kwargs: Any) -> List:
return [cls.create(**kwargs) for _ in range(number)]
class ClassFactory(Factory):
tags = [Tag.ELEMENT, Tag.ATTRIBUTE, Tag.COMPLEX_TYPE, Tag.SIMPLE_TYPE]
counter = 65
@classmethod
def create(
cls,
qname: Optional[str] = None,
meta_name: Optional[str] = None,
namespace: Optional[str] = None,
target_namespace: Optional[str] = None,
tag: Optional[str] = None,
abstract: bool = False,
mixed: bool = False,
nillable: bool = False,
extensions: Optional[List[Extension]] = None,
substitutions: Optional[List[str]] = None,
attrs: Optional[List[Attr]] = None,
inner: Optional[List[Class]] = None,
ns_map: Optional[Dict] = None,
location: str = "tests.xsd",
package: Optional[str] = None,
module: Optional[str] = "tests",
status: Status = Status.RAW,
container: Optional[str] = None,
default: Optional[Any] = None,
fixed: bool = False,
prefix: str = "class_",
**kwargs: Any,
) -> Class:
if not qname:
qname = build_qname("xsdata", f"{prefix}{cls.next_letter()}")
if ns_map is None:
ns_map = copy.deepcopy(DEFAULT_NS_MAP)
return Class(
qname=qname,
meta_name=meta_name,
namespace=namespace,
abstract=abstract,
mixed=mixed,
nillable=nillable,
tag=tag or random.choice(cls.tags),
extensions=extensions or [],
substitutions=substitutions or [],
attrs=attrs or [],
inner=inner or [],
package=package,
location=location,
module=module,
ns_map=ns_map,
status=status,
container=container,
default=default,
fixed=fixed,
**kwargs,
)
@classmethod
def simple_type(cls, **kwargs: Any) -> Class:
return cls.create(
tag=Tag.SIMPLE_TYPE,
attrs=AttrFactory.list(1, tag=Tag.EXTENSION),
**kwargs,
)
@classmethod
def enumeration(cls, attributes: int, **kwargs: Any) -> Class:
return cls.create(
tag=Tag.SIMPLE_TYPE,
attrs=AttrFactory.list(attributes, tag=Tag.ENUMERATION),
**kwargs,
)
@classmethod
def elements(cls, attributes: int, **kwargs: Any) -> Class:
return cls.create(
tag=Tag.COMPLEX_TYPE,
attrs=AttrFactory.list(attributes, tag=Tag.ELEMENT),
**kwargs,
)
@classmethod
def service(cls, attributes: int, **kwargs: Any) -> Class:
return cls.create(
tag=Tag.BINDING_OPERATION,
attrs=AttrFactory.list(attributes, tag=Tag.ELEMENT),
**kwargs,
)
class ExtensionFactory(Factory):
counter = 65
tags = [Tag.ELEMENT, Tag.EXTENSION, Tag.RESTRICTION]
@classmethod
def create(
cls,
attr_type: Optional[AttrType] = None,
restrictions: Optional[Restrictions] = None,
tag: Optional[str] = None,
**kwargs: Any,
) -> Extension:
return Extension(
tag=tag or random.choice(cls.tags),
type=attr_type or AttrTypeFactory.create(**kwargs),
restrictions=restrictions or Restrictions(),
)
@classmethod
def reference(cls, qname: str, **kwargs: Any) -> Extension:
tag = kwargs.pop("tag", None)
restrictions = kwargs.pop("restrictions", None)
return cls.create(
AttrTypeFactory.create(qname=qname, **kwargs),
tag=tag,
restrictions=restrictions,
)
@classmethod
def native(cls, datatype: DataType, **kwargs: Any) -> Extension:
return cls.create(AttrTypeFactory.native(datatype), **kwargs)
class AttrTypeFactory(Factory):
counter = 65
@classmethod
def create(
cls,
qname: Optional[str] = None,
alias: Optional[str] = None,
native: bool = False,
forward: bool = False,
circular: bool = False,
reference: int = 0,
prefix: str = "attr_",
**kwargs: Any,
) -> AttrType:
if not qname:
qname = build_qname("xsdata", f"{prefix}{cls.next_letter()}")
return AttrType(
qname=str(qname),
alias=alias,
native=native,
circular=circular,
forward=forward,
reference=reference,
)
@classmethod
def native(cls, datatype: DataType, **kwargs: Any) -> AttrType:
return cls.create(qname=str(datatype), native=True, **kwargs)
class AttrFactory(Factory):
tags = [Tag.ATTRIBUTE, Tag.ELEMENT, Tag.RESTRICTION]
counter = 65
@classmethod
def create(
cls,
name: Optional[str] = None,
index: Optional[int] = None,
types: Optional[List[AttrType]] = None,
choices: Optional[List[Attr]] = None,
tag: Optional[str] = None,
namespace: Optional[str] = None,
default: Optional[Any] = None,
fixed: bool = False,
mixed: bool = False,
restrictions: Optional[Restrictions] = None,
prefix: str = "attr_",
**kwargs: Any,
) -> Attr:
name = name or f"{prefix}{cls.next_letter()}"
return Attr(
name=name,
index=cls.counter if index is None else index,
types=types or [AttrTypeFactory.native(DataType.STRING)],
choices=choices or [],
tag=tag or random.choice(cls.tags),
namespace=namespace,
default=default,
fixed=fixed,
mixed=mixed,
restrictions=restrictions or Restrictions(),
**kwargs,
)
@classmethod
def reference(cls, qname: str, tag: str = Tag.ELEMENT, **kwargs: Any) -> Attr:
return cls.create(
tag=tag, types=[AttrTypeFactory.create(qname=qname, **kwargs)]
)
@classmethod
def native(cls, datatype: DataType, tag: str = Tag.ELEMENT, **kwargs: Any) -> Attr:
return cls.create(tag=tag, types=[AttrTypeFactory.native(datatype)], **kwargs)
@classmethod
def enumeration(cls, **kwargs: Any) -> Attr:
return cls.create(tag=Tag.ENUMERATION, **kwargs)
@classmethod
def element(cls, **kwargs: Any) -> Attr:
return cls.create(tag=Tag.ELEMENT, **kwargs)
@classmethod
def extension(cls, **kwargs: Any) -> Attr:
return cls.create(tag=Tag.EXTENSION, **kwargs)
@classmethod
def any(cls, **kwargs: Any) -> Attr:
return cls.create(
tag=Tag.ANY, types=[AttrTypeFactory.native(DataType.ANY_TYPE)], **kwargs
)
@classmethod
def any_attribute(cls, **kwargs: Any) -> Attr:
return cls.create(
tag=Tag.ANY_ATTRIBUTE,
types=[AttrTypeFactory.native(DataType.ANY_TYPE)],
**kwargs,
)
@classmethod
def attribute(cls, **kwargs: Any) -> Attr:
return cls.create(tag=Tag.ATTRIBUTE, **kwargs)
@classmethod
def attribute_group(cls, **kwargs: Any) -> Attr:
return cls.create(
tag=Tag.ATTRIBUTE_GROUP,
types=[AttrTypeFactory.create(qname=kwargs.get("name"))],
**kwargs,
)
@classmethod
def group(cls, **kwargs: Any) -> Attr:
return cls.create(tag=Tag.GROUP, **kwargs)
class PackageFactory(Factory):
counter = 65
@classmethod
def create(
cls,
qname: str = "{xsdata}Root",
source: str = "generated.models",
alias: Optional[str] = None,
**kwargs: Any,
) -> Import:
return Import(qname=qname, source=source, alias=alias)
class XmlVarFactory(Factory):
counter = 65
@classmethod
def create(
cls,
name: Optional[str] = None,
qname: Optional[str] = None,
index: int = 0,
types: Optional[Sequence[Type]] = None,
clazz: Optional[Type] = None,
init: bool = True,
mixed: bool = False,
factory: Optional[Callable] = None,
tokens_factory: Optional[Callable] = None,
format: Optional[str] = None,
derived: bool = False,
any_type: bool = False,
required: bool = False,
nillable: bool = False,
sequence: Optional[int] = None,
list_element: bool = False,
default: Optional[Any] = None,
xml_type: str = XmlType.ELEMENT,
namespaces: Optional[Sequence[str]] = None,
elements: Optional[Dict[str, XmlVar]] = None,
wildcards: Optional[Sequence[XmlVar]] = None,
prefix: str = "field_",
**kwargs: Any,
) -> XmlVar:
name = name or f"{prefix}{cls.next_letter()}"
if qname is None:
qname = name
if types is None:
types = ()
if namespaces is None:
namespaces = ()
if elements is None:
elements = {}
if wildcards is None:
wildcards = []
return XmlVar(
index=index,
name=name,
qname=qname,
types=types,
clazz=clazz or first(tp for tp in types if is_dataclass(tp)),
init=init,
mixed=mixed,
factory=factory,
tokens_factory=tokens_factory,
format=format,
derived=derived,
any_type=any_type,
required=required,
nillable=nillable,
sequence=sequence,
process_contents="strict",
list_element=list_element,
default=default,
xml_type=xml_type,
namespaces=namespaces,
elements=elements,
wildcards=wildcards,
wrapper=None,
)
class XmlMetaFactory(Factory):
counter = 65
@classmethod
def create( # type: ignore
cls,
clazz: Type,
qname: Optional[str] = None,
target_qname: Optional[str] = None,
nillable: bool = False,
text: Optional[XmlVar] = None,
choices: Optional[Sequence[XmlVar]] = None,
elements: Optional[Dict[str, Sequence[XmlVar]]] = None,
wildcards: Optional[Sequence[XmlVar]] = None,
attributes: Optional[Dict[str, XmlVar]] = None,
any_attributes: Optional[Sequence[XmlVar]] = None,
**kwargs: Any,
) -> XmlMeta:
if qname is None:
qname = clazz.__name__
if target_qname is None:
target_qname = qname
if choices is None:
choices = []
if elements is None:
elements = {}
if wildcards is None:
wildcards = []
if any_attributes is None:
any_attributes = []
if attributes is None:
attributes = {}
return XmlMeta(
clazz=clazz,
qname=qname,
target_qname=target_qname,
nillable=nillable,
text=text,
choices=choices,
elements=elements,
wildcards=wildcards,
attributes=attributes,
any_attributes=any_attributes,
wrappers={},
)
class DtdAttributeFactory(Factory):
counter = 65
@classmethod
def create(
cls,
name: Optional[str] = None,
prefix: Optional[str] = None,
type: Optional[DtdAttributeType] = None,
default: Optional[DtdAttributeDefault] = None,
default_value: Optional[str] = None,
values: Optional[List[str]] = None,
**kwargs: Any,
) -> DtdAttribute:
if name is None:
name = f"attribute_{cls.next_letter()}"
if type is None:
type = DtdAttributeType.CDATA
if default is None:
default = DtdAttributeDefault.NONE
if values is None:
values = []
return DtdAttribute(
name=name,
prefix=prefix,
type=type,
default=default,
default_value=default_value,
values=values,
)
class DtdContentFactory(Factory):
counter = 65
@classmethod
def create(
cls,
name: Optional[str] = None,
type: Optional[DtdContentType] = None,
occur: Optional[DtdContentOccur] = None,
left: Optional[DtdContent] = None,
right: Optional[DtdContent] = None,
**kwargs: Any,
) -> DtdContent:
if name is None:
name = f"content_{cls.next_letter()}"
if type is None:
type = DtdContentType.ELEMENT
if occur is None:
occur = DtdContentOccur.ONCE
return DtdContent(
name=name,
type=type,
occur=occur,
left=left,
right=right,
)
class DtdElementFactory(Factory):
counter = 65
@classmethod
def create(
cls,
name: Optional[str] = None,
prefix: Optional[str] = None,
type: Optional[DtdElementType] = None,
content: Optional[DtdContent] = None,
attributes: Optional[List[DtdAttribute]] = None,
ns_map: Optional[Dict] = None,
**kwargs: Any,
) -> DtdElement:
if name is None:
name = f"element_{cls.next_letter()}"
if type is None:
type = DtdElementType.ELEMENT
if attributes is None:
attributes = []
if ns_map is None:
ns_map = {}
return DtdElement(
name=name,
prefix=prefix,
type=type,
content=content,
attributes=attributes,
ns_map=ns_map,
)
class DtdFactory(Factory):
@classmethod
def create(
cls,
elements: Optional[List[DtdElement]] = None,
location: Optional[str] = None,
**kwargs: Any,
) -> Dtd:
if elements is None:
elements = []
if location is None:
location = "test.dtd"
return Dtd(elements=elements, location=location)
@classmethod
def root(cls, number: int, **kwargs: Any) -> Dtd:
elements = DtdElementFactory.list(number)
return cls.create(elements=elements, **kwargs)
|