ptr-editor 0.3.0__tar.gz → 0.4.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (155) hide show
  1. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/PKG-INFO +1 -2
  2. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/pyproject.toml +1 -3
  3. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/attrs_xml/core/fields.py +7 -0
  4. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/attrs_xml/xml/converter.py +192 -1
  5. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/attrs_xml/xml/custom_disambiguator.py +25 -0
  6. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/attrs_xml/xml/io.py +101 -102
  7. ptr_editor-0.4.0/src/attrs_xml/xml/lxml_adapter.py +204 -0
  8. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/attrs_xml/xml/utils.py +1 -1
  9. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/juice_jsoc/apl/elements.py +28 -13
  10. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/juice_jsoc/itl/elements.py +1 -5
  11. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/juice_jsoc/opl/elements.py +4 -3
  12. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/juice_jsoc/opl/jsoc-opl-schema.json +4 -1
  13. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/agm_config/agm_config.py +13 -0
  14. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/core/ptr_element.py +1 -1
  15. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/elements/metadata.py +32 -13
  16. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/time_segments/segment_mixin.py +57 -0
  17. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/time_segments/segments_collection_mixin.py +28 -1
  18. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/.gitignore +0 -0
  19. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/LICENSE +0 -0
  20. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/README.md +0 -0
  21. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/attrs_xml/README.md +0 -0
  22. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/attrs_xml/__init__.py +0 -0
  23. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/attrs_xml/core/__init__.py +0 -0
  24. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/attrs_xml/core/base_element.py +0 -0
  25. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/attrs_xml/core/converter_factory.py +0 -0
  26. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/attrs_xml/core/decorators.py +0 -0
  27. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/attrs_xml/core/decorators_utils.py +0 -0
  28. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/attrs_xml/core/field_transformer.py +0 -0
  29. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/attrs_xml/core/inspect.py +0 -0
  30. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/attrs_xml/core/protocols.py +0 -0
  31. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/attrs_xml/core/sentinels.py +0 -0
  32. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/attrs_xml/core/type_utils.py +0 -0
  33. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/attrs_xml/core/validator_factory.py +0 -0
  34. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/attrs_xml/elements_registry.py +0 -0
  35. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/attrs_xml/formatting.py +0 -0
  36. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/attrs_xml/globals.py +0 -0
  37. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/attrs_xml/pandas.py +0 -0
  38. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/attrs_xml/resolution/__init__.py +0 -0
  39. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/attrs_xml/resolution/registry.py +0 -0
  40. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/attrs_xml/resolution/strategies.py +0 -0
  41. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/attrs_xml/xml/__init__.py +0 -0
  42. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/attrs_xml/xml/converters.py +0 -0
  43. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/juice_jsoc/__init__.py +0 -0
  44. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/juice_jsoc/apl/__init__.py +0 -0
  45. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/juice_jsoc/apl/jsoc-apl-schema.json +0 -0
  46. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/juice_jsoc/base.py +0 -0
  47. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/juice_jsoc/diffing.py +0 -0
  48. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/juice_jsoc/itl/__init__.py +0 -0
  49. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/juice_jsoc/itl/jsoc-itl-schema.json +0 -0
  50. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/juice_jsoc/opl/__init__.py +0 -0
  51. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/juice_jsoc/opl/csv.py +0 -0
  52. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/juice_jsoc/schema.py +0 -0
  53. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/juice_jsoc/utils.py +0 -0
  54. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/juice_opl/__init__.py +0 -0
  55. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_cli/README.md +0 -0
  56. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_cli/__init__.py +0 -0
  57. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_cli/commands/__init__.py +0 -0
  58. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_cli/commands/validate.py +0 -0
  59. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_cli/commands/version.py +0 -0
  60. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_cli/main.py +0 -0
  61. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_compare/__init__.py +0 -0
  62. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/__init__.py +0 -0
  63. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/accessors/__init__.py +0 -0
  64. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/accessors/accessor.py +0 -0
  65. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/accessors/attrs_dict_accessor.py +0 -0
  66. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/agm_config/__init__.py +0 -0
  67. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/agm_config/agm_utils.py +0 -0
  68. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/agm_config/html.py +0 -0
  69. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/agm_validator/__init__.py +0 -0
  70. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/agm_validator/agm_config_validator.py +0 -0
  71. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/context/README.md +0 -0
  72. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/context/__init__.py +0 -0
  73. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/context/defaults.py +0 -0
  74. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/context/presets.py +0 -0
  75. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/context/service_config.py +0 -0
  76. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/context/services.py +0 -0
  77. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/core/__init__.py +0 -0
  78. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/core/codenames_gen.py +0 -0
  79. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/core/logging.py +0 -0
  80. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/core/tree_utils.py +0 -0
  81. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/data/__init__.py +0 -0
  82. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/data/snippets.txt +0 -0
  83. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/diffing/__init__.py +0 -0
  84. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/diffing/diff_report.py +0 -0
  85. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/diffing/matcher.py +0 -0
  86. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/diffing/text_diff_result.py +0 -0
  87. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/diffing/timeline_differ_simple.py +0 -0
  88. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/diffing/visualize.py +0 -0
  89. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/elements/__init__.py +0 -0
  90. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/elements/additional_elements.py +0 -0
  91. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/elements/agm_config.py +0 -0
  92. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/elements/array.py +0 -0
  93. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/elements/attitude.py +0 -0
  94. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/elements/blocks.py +0 -0
  95. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/elements/directions.py +0 -0
  96. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/elements/doc.py +0 -0
  97. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/elements/offset.py +0 -0
  98. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/elements/phase_angle.py +0 -0
  99. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/elements/ref_typed_mixin.py +0 -0
  100. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/elements/surface.py +0 -0
  101. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/elements/timeline.py +0 -0
  102. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/elements/units.py +0 -0
  103. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/elements/values.py +0 -0
  104. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/elements_extractor/__init__.py +0 -0
  105. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/extra/__init__.py +0 -0
  106. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/extra/add_block_comments.py +0 -0
  107. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/factory/__init__.py +0 -0
  108. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/factory/attitudes.py +0 -0
  109. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/factory/blocks.py +0 -0
  110. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/generators/__init__.py +0 -0
  111. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/generators/base.py +0 -0
  112. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/generators/generators.py +0 -0
  113. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/generators/processor.py +0 -0
  114. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/html.py +0 -0
  115. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/io/__init__.py +0 -0
  116. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/io/ptr_converter_setup.py +0 -0
  117. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/io/simplified_converter.py +0 -0
  118. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/io/simplified_converter2.py +0 -0
  119. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/io/xml_formatting.py +0 -0
  120. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/py.typed +0 -0
  121. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/services/__init__.py +0 -0
  122. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/services/quick_access.py +0 -0
  123. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/services/resolution_strategies.py +0 -0
  124. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/spice_accessors/__init__.py +0 -0
  125. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/spice_accessors/directions.py +0 -0
  126. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/spice_accessors/eval_directions.py +0 -0
  127. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/templates/__init__.py +0 -0
  128. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/templates/html.py +0 -0
  129. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/templates/register.py +0 -0
  130. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/templates/template.py +0 -0
  131. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/validation/README.md +0 -0
  132. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/validation/__init__.py +0 -0
  133. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/validation/combinators.py +0 -0
  134. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/validation/html.py +0 -0
  135. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/validation/ptr_validators.py +0 -0
  136. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/validation/result.py +0 -0
  137. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/validation/ruleset_registry.py +0 -0
  138. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/validation/runner.py +0 -0
  139. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/validation/validator.py +0 -0
  140. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/versioned_file_manager/__init__.py +0 -0
  141. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/versioned_file_manager/file_info.py +0 -0
  142. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/versioned_file_manager/html.py +0 -0
  143. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_editor/versioned_file_manager/manager.py +0 -0
  144. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_solver/__init__.py +0 -0
  145. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_solver/cached_solver.py +0 -0
  146. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_solver/ptr_solver.py +0 -0
  147. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/ptr_solver/solvable_mixin.py +0 -0
  148. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/time_segments/__init__.py +0 -0
  149. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/time_segments/config.py +0 -0
  150. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/time_segments/interface.py +0 -0
  151. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/time_segments/merging.py +0 -0
  152. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/time_segments/opl.py +0 -0
  153. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/time_segments/ops.py +0 -0
  154. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/time_segments/plot.py +0 -0
  155. {ptr_editor-0.3.0 → ptr_editor-0.4.0}/src/time_segments/utils.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ptr-editor
3
- Version: 0.3.0
3
+ Version: 0.4.0
4
4
  Summary: Utilities to edit PTR files.
5
5
  Project-URL: documentation, https://luca-penasa.github.io/ptr-editor
6
6
  Project-URL: homepage, https://luca-penasa.github.io/ptr-editor
@@ -38,7 +38,6 @@ Requires-Dist: quick-spice-manager>=0.0.3
38
38
  Requires-Dist: requests-cache>=1.2.1
39
39
  Requires-Dist: requests>=2.31.0
40
40
  Requires-Dist: xmldiff>=2.7.0
41
- Requires-Dist: xmltodict>=0.14.2
42
41
  Description-Content-Type: text/markdown
43
42
 
44
43
  # Ptr Editor
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "ptr-editor"
3
- version = "0.3.0"
3
+ version = "0.4.0"
4
4
  description = "Utilities to edit PTR files."
5
5
  authors = [
6
6
  { name = "Luca Penasa", email = "luca.penasa@inaf.it" },
@@ -27,7 +27,6 @@ dependencies = [
27
27
  "importlib-metadata>=8.5.0,<9",
28
28
  "loguru>=0.7.2,<0.8",
29
29
  "pandas>=2.2.3,<3",
30
- "xmltodict>=0.14.2", # main xml to dict conveter and vice-versa
31
30
  "cattrs>=24",
32
31
  "ptwrapper>=2.7.0",
33
32
  "planetary-coverage>=1.2.0",
@@ -83,7 +82,6 @@ dev = [
83
82
  "bump-my-version>=0.27.0,<0.28",
84
83
  "rust-just>=1.36.0,<2",
85
84
  "xmldiff>=2.7.0,<3",
86
- "xmltodict>=0.14.2,<0.15",
87
85
  "xmlformatter>=0.2.8,<0.3",
88
86
  "pandas-stubs>=2.2.3.241126,<3",
89
87
  "pylint>=3.3.7",
@@ -34,6 +34,7 @@ def _custom_field(
34
34
  tag_aliases: str | list[str] | None = None,
35
35
  xml_type=None,
36
36
  cattrs_omit: bool = False,
37
+ mixed: bool = False,
37
38
  post_converter=None, # function that process the value after the automated one
38
39
  **kwargs,
39
40
  ) -> Any:
@@ -45,6 +46,10 @@ def _custom_field(
45
46
  Can be a single string or a list of strings.
46
47
  xml_type: Type of XML element ("text", "attr", or None for element)
47
48
  cattrs_omit: Whether to omit this field during cattrs structuring
49
+ mixed: When True, the field is serialised as a mixed-type element list:
50
+ each item uses its own ``default_name`` as its XML tag rather than a
51
+ shared tag derived from the field name. Only applies to fields
52
+ typed as ``list[A | B | ...]``.
48
53
  post_converter: Function to process the value after automated conversion
49
54
  **kwargs: Additional arguments passed to attrs.field()
50
55
  """
@@ -52,6 +57,8 @@ def _custom_field(
52
57
  mdata["xml_tag"] = tag
53
58
  mdata["xml_type"] = xml_type
54
59
  mdata["cattrs_omit"] = cattrs_omit
60
+ if mixed:
61
+ mdata["xml_mixed"] = True
55
62
 
56
63
  # Normalize tag_aliases to a list
57
64
  if tag_aliases is not None:
@@ -20,7 +20,7 @@ from attrs_xml.formatting import (
20
20
  )
21
21
  from attrs_xml.globals import global_config
22
22
  from attrs_xml.xml.converters import format_timestamp
23
- from attrs_xml.xml.custom_disambiguator import custom_create_default_dis_func
23
+ from attrs_xml.xml.custom_disambiguator import XML_TAG_MARKER, custom_create_default_dis_func
24
24
 
25
25
 
26
26
  class CustomConverter(cattrs.Converter):
@@ -177,6 +177,189 @@ def _expects_list_type(field_type: Any) -> bool:
177
177
  # Cache for field mappings per class to avoid rebuilding on every call
178
178
  _FIELD_MAPPING_CACHE: dict[type, dict] = {}
179
179
 
180
+ # ---------------------------------------------------------------------------
181
+ # Mixed-type element list support
182
+ # ---------------------------------------------------------------------------
183
+
184
+ #: Marker key injected into dicts to carry the originating XML tag name so
185
+ #: that ambiguous union members (sharing the same dict structure) can be
186
+ #: correctly disambiguated during structuring. See ``_collect_mixed_list_fields``
187
+ #: and ``custom_disambiguator.py``.
188
+ XML_TAG_MARKER = "__xml_tag__"
189
+
190
+ #: Cache: cls → {field_name: tuple[union_member_types]}
191
+ _MIXED_LIST_FIELD_CACHE: dict[type, dict[str, tuple[type, ...]]] = {}
192
+
193
+
194
+ def _get_mixed_element_list_types(field_type: Any) -> tuple[type, ...] | None:
195
+ """Return the union member types when *field_type* is ``list[A | B | …]``
196
+ with ≥ 2 distinct ``BaseElement`` subclasses; otherwise return *None*.
197
+
198
+ This is used both in the serialisation path (to decide whether to use each
199
+ item's ``default_name`` as the XML tag) and in the structuring
200
+ (parsing) path (to locate mixed children in the parsed dict).
201
+ """
202
+ from types import UnionType
203
+
204
+ from typing import Union # noqa: F811 (shadowed by top-level import is fine here)
205
+
206
+ from attrs_xml.core.base_element import BaseElement
207
+
208
+ origin = get_origin(field_type)
209
+ if origin not in (list, tuple, set):
210
+ return None
211
+
212
+ args = get_args(field_type)
213
+ if not args:
214
+ return None
215
+
216
+ inner_type = args[0]
217
+ inner_origin = get_origin(inner_type)
218
+
219
+ # Accept both ``typing.Union[A, B]`` and the Python 3.10+ ``A | B`` syntax.
220
+ if inner_origin is not Union and not isinstance(inner_type, UnionType):
221
+ return None
222
+
223
+ element_types = tuple(
224
+ arg
225
+ for arg in get_args(inner_type)
226
+ if arg is not type(None)
227
+ and isinstance(arg, type)
228
+ and issubclass(arg, BaseElement)
229
+ )
230
+
231
+ return element_types if len(element_types) >= 2 else None
232
+
233
+
234
+ def _get_class_mixed_list_fields(cls: type) -> dict[str, tuple[type, ...]]:
235
+ """Return a cached mapping of *field_name* → *union_types* for every field
236
+ on *cls* that has been explicitly opted in to mixed-type element list
237
+ serialisation via ``element(mixed=True)``.
238
+ """
239
+ if cls not in _MIXED_LIST_FIELD_CACHE:
240
+ result: dict[str, tuple[type, ...]] = {}
241
+ for f in fields(cls):
242
+ if not f.metadata.get("xml_mixed"):
243
+ continue
244
+ union_types = _get_mixed_element_list_types(f.type)
245
+ if union_types:
246
+ result[f.name] = union_types
247
+ _MIXED_LIST_FIELD_CACHE[cls] = result
248
+ return _MIXED_LIST_FIELD_CACHE[cls]
249
+
250
+
251
+ def _collect_mixed_list_fields(val: dict, cls: type) -> dict:
252
+ """Pre-process *val* to collect mixed-type list fields from separate tag keys.
253
+
254
+ When XML has mixed children such as::
255
+
256
+ <root>
257
+ <sequence name="Seq1"/>
258
+ <command name="Cmd1"/>
259
+ <sequence name="Seq2"/>
260
+ </root>
261
+
262
+ The lxml parser preserves document order via a ``__xml_children__`` key
263
+ (a flat list of all interleaved children tagged with ``XML_TAG_MARKER``).
264
+ When that key is present this function uses it directly, giving exact
265
+ document order.
266
+
267
+ When ``__xml_children__`` is absent (non-interleaved or pre-existing dict),
268
+ it falls back to collecting per-tag keys in dict-insertion order.
269
+
270
+ In both cases, consumed per-tag keys are removed and the result is stored
271
+ under the field's expected dict key. ``__xml_children__`` is always
272
+ removed after processing to prevent it leaking into the structure hook.
273
+ """
274
+ if not isinstance(val, dict) or not has(cls):
275
+ return val
276
+
277
+ mixed_fields = _get_class_mixed_list_fields(cls)
278
+
279
+ # Always strip __xml_children__ to prevent ForbiddenExtraKeysError when
280
+ # the element has no mixed fields, or after it has been consumed.
281
+ if not mixed_fields:
282
+ if "__xml_children__" in val:
283
+ val = {k: v for k, v in val.items() if k != "__xml_children__"}
284
+ return val
285
+
286
+ overrides = generate_converter_overrides(cls)
287
+ modified_val: dict | None = None
288
+
289
+ for field_name, union_types in mixed_fields.items():
290
+ # Determine the expected dict key for this field after renaming.
291
+ primary_key = field_name
292
+ if field_name in overrides:
293
+ override_obj = overrides[field_name]
294
+ renamed_to = (
295
+ override_obj.rename if hasattr(override_obj, "rename") else None
296
+ )
297
+ if renamed_to:
298
+ primary_key = renamed_to
299
+
300
+ # If the primary key is already present, nothing to do for this field.
301
+ if primary_key in (modified_val or val):
302
+ continue
303
+
304
+ # Build tag → set mapping from each union member's default_name.
305
+ tag_to_type = {
306
+ t.default_name: t
307
+ for t in union_types
308
+ if getattr(t, "default_name", None)
309
+ }
310
+
311
+ src = modified_val if modified_val is not None else val
312
+
313
+ # ── Fast path: use document-order children list if the lxml parser
314
+ # detected interleaved tags for this element. ────────────────────
315
+ if "__xml_children__" in src:
316
+ relevant = [
317
+ c
318
+ for c in src["__xml_children__"]
319
+ if c.get(XML_TAG_MARKER) in tag_to_type
320
+ ]
321
+ if relevant:
322
+ if modified_val is None:
323
+ modified_val = dict(val)
324
+ # Remove the per-tag keys consumed by this field.
325
+ for t in tag_to_type:
326
+ modified_val.pop(t, None)
327
+ modified_val.pop("__xml_children__", None)
328
+ modified_val[primary_key] = relevant
329
+ continue
330
+
331
+ # ── Fallback: collect per-tag keys in dict-insertion order ──────────
332
+ member_tags_in_val = [k for k in src if k in tag_to_type]
333
+ if not member_tags_in_val:
334
+ continue
335
+
336
+ if modified_val is None:
337
+ modified_val = dict(val)
338
+
339
+ collected: list = []
340
+ for key in list(modified_val.keys()):
341
+ if key not in tag_to_type:
342
+ continue
343
+ items = modified_val.pop(key)
344
+ if not isinstance(items, list):
345
+ items = [items]
346
+ for item in items:
347
+ if isinstance(item, dict):
348
+ collected.append({XML_TAG_MARKER: key, **item})
349
+ else:
350
+ collected.append(item)
351
+
352
+ if collected:
353
+ modified_val[primary_key] = collected
354
+
355
+ # Ensure __xml_children__ is stripped even if no field consumed it.
356
+ if modified_val is not None:
357
+ modified_val.pop("__xml_children__", None)
358
+ elif "__xml_children__" in val:
359
+ modified_val = {k: v for k, v in val.items() if k != "__xml_children__"}
360
+
361
+ return modified_val if modified_val is not None else val
362
+
180
363
 
181
364
  def _resolve_tag_aliases(val: dict, cls: type) -> dict:
182
365
  """Resolve tag aliases by renaming aliased keys to their primary tag names.
@@ -345,6 +528,14 @@ def make_structure_hook_factory(converter: Converter):
345
528
  # Pre-process: resolve tag aliases first
346
529
  val = _resolve_tag_aliases(val, cls)
347
530
 
531
+ # Pre-process: collect mixed-type list fields from per-tag keys.
532
+ # For fields typed as list[A | B | C] without an explicit xml_tag,
533
+ # the parsed dict has separate keys "a_tag", "b_tag", … rather than
534
+ # one "fieldName" key. This step merges them into the expected key
535
+ # and attaches XML_TAG_MARKER so the disambiguator can determine
536
+ # the concrete type of each item.
537
+ val = _collect_mixed_list_fields(val, cls)
538
+
348
539
  # Pre-process: fix list values before attempting to structure
349
540
  val = _fix_list_fields_in_dict(val, cls)
350
541
 
@@ -32,6 +32,11 @@ from cattrs._compat import (
32
32
  Mapping,
33
33
  )
34
34
 
35
+ #: Marker key used by ``_collect_mixed_list_fields`` (converter.py) to embed
36
+ #: the originating XML tag inside a dict item so that the disambiguator can
37
+ #: reliably select the correct union member even when the dict structures of
38
+ #: the union members are otherwise identical.
39
+ XML_TAG_MARKER = "__xml_tag__"
35
40
 
36
41
  def custom_create_default_dis_func(
37
42
  converter: BaseConverter,
@@ -228,6 +233,13 @@ def custom_create_default_dis_func(
228
233
  msg = "Could not create a disambiguation function for the given classes"
229
234
  raise ValueError(msg)
230
235
 
236
+ # Build a default_name → type lookup for the XML_TAG_MARKER fast-path.
237
+ _tag_to_cls: dict[str | None, type[AttrsInstance]] = {
238
+ getattr(cls, "default_name", None): cls
239
+ for cls in classes
240
+ if getattr(cls, "default_name", None) is not None
241
+ }
242
+
231
243
  if fallback is None:
232
244
 
233
245
  def dis_func(data: Mapping[Any, Any]) -> type[AttrsInstance] | None:
@@ -235,6 +247,13 @@ def custom_create_default_dis_func(
235
247
  msg = "Only input mappings are supported"
236
248
  raise TypeError(msg)
237
249
 
250
+ # Fast-path: honour the XML tag marker injected by
251
+ # _collect_mixed_list_fields during mixed-type list parsing.
252
+ if XML_TAG_MARKER in data:
253
+ result = _tag_to_cls.get(data[XML_TAG_MARKER])
254
+ if result is not None:
255
+ return result
256
+
238
257
  # First try literal discriminators
239
258
  for discriminator_name, value_map in literal_discriminators.items():
240
259
  if discriminator_name in data:
@@ -257,6 +276,12 @@ def custom_create_default_dis_func(
257
276
  msg = "Only input mappings are supported"
258
277
  raise TypeError(msg)
259
278
 
279
+ # Fast-path: honour the XML tag marker.
280
+ if XML_TAG_MARKER in data:
281
+ result = _tag_to_cls.get(data[XML_TAG_MARKER])
282
+ if result is not None:
283
+ return result
284
+
260
285
  # First try literal discriminators
261
286
  for discriminator_name, value_map in literal_discriminators.items():
262
287
  if discriminator_name in data:
@@ -10,99 +10,17 @@ from loguru import logger as log
10
10
 
11
11
  # from attrs_xml.xml_formatting import style_xml_with_padding
12
12
  from attrs_xml.core.inspect import ptr_fields
13
- from attrs_xml.xml.utils import is_element_a_list, remove_null_values
13
+ from attrs_xml.xml.lxml_adapter import (
14
+ dict_to_element,
15
+ parse_xml,
16
+ )
17
+ from attrs_xml.xml.utils import remove_null_values
14
18
 
15
19
  if TYPE_CHECKING:
16
20
  from attrs_xml.core.base_element import BaseElement
17
21
  from attrs_xml.xml.converter import CustomConverter
18
22
 
19
23
 
20
- def _parse_xml_to_dict(xml: str, preprocess: bool = True) -> dict[str, Any]:
21
- """
22
- Parse XML string or file path to dictionary.
23
-
24
- Args:
25
- xml: Either an XML string or path to an XML file
26
- preprocess: Whether to preprocess XML with lxml parser (recover mode) before
27
- parsing with xmltodict. Default is True.
28
-
29
- Returns:
30
- Dictionary representation of the XML
31
-
32
- Raises:
33
- Various exceptions from xmltodict.parse or file operations
34
- """
35
- from xmltodict import parse
36
-
37
- # Check if it's a file path (but don't use Path for long strings)
38
- if os.path.isfile(xml):
39
- with open(xml) as f:
40
- xml_content = f.read()
41
- else:
42
- xml_content = xml
43
-
44
- # Preprocess with lxml if requested
45
- if preprocess:
46
- xml_content = _preprocess_xml_with_lxml(xml_content)
47
-
48
- return parse(
49
- xml_content.encode(),
50
- force_list=is_element_a_list,
51
- )
52
-
53
-
54
- def _preprocess_xml_with_lxml(xml_content: str) -> str:
55
- """
56
- Preprocess XML content using lxml parser with recover mode.
57
-
58
- This ensures the XML can be parsed and automatically fixes common issues.
59
-
60
- Args:
61
- xml_content: The XML content as a string
62
-
63
- Returns:
64
- The preprocessed XML content as a string
65
-
66
- Raises:
67
- ValueError: If the XML cannot be parsed even with recovery mode
68
- """
69
- from lxml import etree
70
-
71
- try:
72
- # Create a parser with recover mode enabled
73
- parser = etree.XMLParser(recover=True, encoding="utf-8")
74
-
75
- # Parse the XML
76
- tree = etree.fromstring(xml_content.encode("utf-8"), parser=parser)
77
-
78
- # Check for parsing errors
79
- if parser.error_log:
80
- log.warning(
81
- f"XML parsing encountered {len(parser.error_log)} recoverable errors",
82
- )
83
- for error in parser.error_log:
84
- log.debug(
85
- f" Line {error.line}, Column {error.column}: {error.message}",
86
- )
87
-
88
- # Convert back to string
89
- preprocessed = etree.tostring(tree, encoding="unicode", pretty_print=False)
90
-
91
- log.debug("XML preprocessing completed successfully")
92
- return preprocessed
93
-
94
- except etree.XMLSyntaxError as e:
95
- log.error(f"Failed to preprocess XML: {e}")
96
- log.error(f"Error on line {e.lineno}, column {e.offset}: {e.msg}")
97
- msg = f"XML syntax error: {e.msg}"
98
- raise ValueError(msg) from e
99
- except Exception as e:
100
- log.error(f"Unexpected error during XML preprocessing: {e}")
101
- log.error(f"Error type: {type(e).__name__}")
102
- msg = f"Failed to preprocess XML: {e}"
103
- raise ValueError(msg) from e
104
-
105
-
106
24
  def _read_xml_to_object(cls, xml: str, converter: CustomConverter) -> BaseElement:
107
25
  """
108
26
  Read XML and convert it to an object instance.
@@ -117,7 +35,7 @@ def _read_xml_to_object(cls, xml: str, converter: CustomConverter) -> BaseElemen
117
35
  Raises:
118
36
  ValueError: If XML parsing or object creation fails
119
37
  """
120
- asdict = _parse_xml_to_dict(xml)
38
+ asdict = parse_xml(xml)
121
39
  log.debug(f"as dict {asdict}")
122
40
 
123
41
  result = dict_to_object(cls, asdict, converter=converter)
@@ -191,6 +109,92 @@ def handle_dict(dictionary: dict, parent) -> None:
191
109
  set_parent_references(item, parent)
192
110
 
193
111
 
112
+ def _build_lxml_element(
113
+ obj: "BaseElement",
114
+ converter: "CustomConverter",
115
+ tag: str | None = None,
116
+ ) -> Any:
117
+ """Build an ``lxml.etree.Element`` from a *BaseElement*.
118
+
119
+ Serialises via ``converter.unstructure`` + ``remove_null_values``, then
120
+ populates an lxml element. Empty ``BaseElement`` objects (all-None fields)
121
+ are suppressed automatically.
122
+
123
+ Fields declared with ``element(mixed=True)`` use each item's own
124
+ ``default_name`` as its XML tag, preserving order for mixed-type union
125
+ lists.
126
+ """
127
+ from lxml import etree
128
+
129
+ from attrs import fields as attrs_fields
130
+
131
+ from attrs_xml.core.base_element import BaseElement
132
+ from attrs_xml.formatting import to_camel_case
133
+
134
+ root_tag = tag if tag is not None else obj.default_name
135
+ el = etree.Element(root_tag)
136
+
137
+ for f in attrs_fields(type(obj)):
138
+ if f.metadata.get("cattrs_omit"):
139
+ continue
140
+
141
+ value = getattr(obj, f.name)
142
+ if value is None:
143
+ continue
144
+
145
+ xml_type = f.metadata.get("xml_type")
146
+ field_xml_tag = f.metadata.get("xml_tag") or to_camel_case(f.name)
147
+
148
+ if xml_type == "attr":
149
+ unst = converter.unstructure(value)
150
+ if unst is not None:
151
+ el.set(field_xml_tag, str(unst))
152
+
153
+ elif xml_type == "text":
154
+ unst = converter.unstructure(value)
155
+ if unst is not None:
156
+ el.text = str(unst)
157
+
158
+ elif isinstance(value, list) and f.metadata.get("xml_mixed", False):
159
+ for item in value:
160
+ if isinstance(item, BaseElement):
161
+ item_unst = converter.unstructure(item)
162
+ item_cleaned = remove_null_values(item_unst)
163
+ child_el = etree.SubElement(el, item.default_name)
164
+ if item_cleaned:
165
+ dict_to_element(child_el, item_cleaned)
166
+ elif item is not None:
167
+ unst = converter.unstructure(item)
168
+ if unst is not None:
169
+ child_el = etree.SubElement(el, field_xml_tag)
170
+ child_el.text = str(unst)
171
+
172
+ else:
173
+ unst = converter.unstructure(value)
174
+ if unst is None:
175
+ continue
176
+ cleaned = remove_null_values(unst)
177
+ if cleaned is None:
178
+ continue
179
+
180
+ if isinstance(cleaned, list):
181
+ for item_data in cleaned:
182
+ if isinstance(item_data, dict):
183
+ child_el = etree.SubElement(el, field_xml_tag)
184
+ dict_to_element(child_el, item_data)
185
+ elif item_data is not None:
186
+ child_el = etree.SubElement(el, field_xml_tag)
187
+ child_el.text = str(item_data)
188
+ elif isinstance(cleaned, dict):
189
+ child_el = etree.SubElement(el, field_xml_tag)
190
+ dict_to_element(child_el, cleaned)
191
+ else:
192
+ child_el = etree.SubElement(el, field_xml_tag)
193
+ child_el.text = str(cleaned)
194
+
195
+ return el
196
+
197
+
194
198
  def object_to_dict(
195
199
  obj,
196
200
  converter: CustomConverter,
@@ -267,19 +271,14 @@ def object_to_xml(
267
271
  Returns:
268
272
  XML string representation of the object
269
273
  """
270
- from xmltodict import unparse
271
-
272
- as_dict = object_to_dict(obj, converter=converter, root_key=root_key)
273
- # if not as_dict:
274
- # return f"<{root_key}/>"
275
-
276
- return unparse(
277
- as_dict,
278
- pretty=pretty,
279
- full_document=False,
280
- short_empty_elements=True,
281
- indent=3,
282
- )
274
+ from lxml import etree
275
+
276
+ el = _build_lxml_element(obj, converter, tag=root_key or None)
277
+
278
+ if pretty:
279
+ etree.indent(el, space=" ")
280
+
281
+ return etree.tostring(el, encoding="unicode")
283
282
 
284
283
 
285
284
  from cattrs.v import format_exception
@@ -326,7 +325,7 @@ def loads(
326
325
  """
327
326
  from attrs_xml.globals import disable_defaults as disable_defaults_ctx
328
327
 
329
- xml_dict = _parse_xml_to_dict(xml_text, preprocess=preprocess)
328
+ xml_dict = parse_xml(xml_text, preprocess=preprocess)
330
329
  if not xml_dict:
331
330
  log.error("Failed to parse XML text.")
332
331
  return None