ycpp 0.1.0__cp314-cp314-win_amd64.whl

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 (142) hide show
  1. ycpp/__init__.py +110 -0
  2. ycpp/_array.py +318 -0
  3. ycpp/_awareness.py +268 -0
  4. ycpp/_doc.py +718 -0
  5. ycpp/_event.py +188 -0
  6. ycpp/_id_map.py +620 -0
  7. ycpp/_map.py +267 -0
  8. ycpp/_provider.py +180 -0
  9. ycpp/_snapshot.py +28 -0
  10. ycpp/_sticky_index.py +59 -0
  11. ycpp/_stream.py +91 -0
  12. ycpp/_sync.py +192 -0
  13. ycpp/_text.py +239 -0
  14. ycpp/_transaction.py +287 -0
  15. ycpp/_typed.py +122 -0
  16. ycpp/_undo.py +201 -0
  17. ycpp/_update.py +18 -0
  18. ycpp/_xml.py +452 -0
  19. ycpp/_ycpp.cp314-win_amd64.lib +0 -0
  20. ycpp/_ycpp.cp314-win_amd64.pyd +0 -0
  21. ycpp-0.1.0.data/headers/fast_float/ascii_number.h +766 -0
  22. ycpp-0.1.0.data/headers/fast_float/bigint.h +638 -0
  23. ycpp-0.1.0.data/headers/fast_float/constexpr_feature_detect.h +53 -0
  24. ycpp-0.1.0.data/headers/fast_float/decimal_to_binary.h +212 -0
  25. ycpp-0.1.0.data/headers/fast_float/digit_comparison.h +455 -0
  26. ycpp-0.1.0.data/headers/fast_float/fast_float.h +91 -0
  27. ycpp-0.1.0.data/headers/fast_float/fast_table.h +708 -0
  28. ycpp-0.1.0.data/headers/fast_float/float_common.h +1432 -0
  29. ycpp-0.1.0.data/headers/fast_float/parse_number.h +488 -0
  30. ycpp-0.1.0.data/headers/xsimd/arch/common/xsimd_common_arithmetic.hpp +267 -0
  31. ycpp-0.1.0.data/headers/xsimd/arch/common/xsimd_common_bit.hpp +230 -0
  32. ycpp-0.1.0.data/headers/xsimd/arch/common/xsimd_common_cast.hpp +43 -0
  33. ycpp-0.1.0.data/headers/xsimd/arch/common/xsimd_common_complex.hpp +112 -0
  34. ycpp-0.1.0.data/headers/xsimd/arch/common/xsimd_common_details.hpp +386 -0
  35. ycpp-0.1.0.data/headers/xsimd/arch/common/xsimd_common_logical.hpp +240 -0
  36. ycpp-0.1.0.data/headers/xsimd/arch/common/xsimd_common_math.hpp +2601 -0
  37. ycpp-0.1.0.data/headers/xsimd/arch/common/xsimd_common_memory.hpp +1006 -0
  38. ycpp-0.1.0.data/headers/xsimd/arch/common/xsimd_common_rounding.hpp +72 -0
  39. ycpp-0.1.0.data/headers/xsimd/arch/common/xsimd_common_swizzle.hpp +260 -0
  40. ycpp-0.1.0.data/headers/xsimd/arch/common/xsimd_common_trigo.hpp +996 -0
  41. ycpp-0.1.0.data/headers/xsimd/arch/utils/shifts.hpp +82 -0
  42. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_avx.hpp +2111 -0
  43. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_avx2.hpp +1591 -0
  44. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_avx512bw.hpp +724 -0
  45. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_avx512cd.hpp +28 -0
  46. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_avx512dq.hpp +270 -0
  47. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_avx512er.hpp +17 -0
  48. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_avx512f.hpp +2919 -0
  49. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_avx512ifma.hpp +17 -0
  50. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_avx512pf.hpp +17 -0
  51. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_avx512vbmi.hpp +76 -0
  52. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_avx512vbmi2.hpp +130 -0
  53. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_avx512vnni_avx512bw.hpp +17 -0
  54. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_avx512vnni_avx512vbmi2.hpp +17 -0
  55. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_avxvnni.hpp +17 -0
  56. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_common.hpp +26 -0
  57. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_common_fwd.hpp +127 -0
  58. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_constants.hpp +393 -0
  59. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_emulated.hpp +787 -0
  60. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_fma3_avx.hpp +93 -0
  61. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_fma3_avx2.hpp +46 -0
  62. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_fma3_sse.hpp +97 -0
  63. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_fma4.hpp +92 -0
  64. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_i8mm_neon64.hpp +17 -0
  65. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_isa.hpp +146 -0
  66. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_neon.hpp +3580 -0
  67. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_neon64.hpp +1614 -0
  68. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_rvv.hpp +1515 -0
  69. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_scalar.hpp +1210 -0
  70. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_sse2.hpp +2333 -0
  71. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_sse3.hpp +66 -0
  72. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_sse4_1.hpp +510 -0
  73. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_sse4_2.hpp +44 -0
  74. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_ssse3.hpp +186 -0
  75. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_sve.hpp +1186 -0
  76. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_vsx.hpp +916 -0
  77. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_vxe.hpp +787 -0
  78. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_wasm.hpp +1819 -0
  79. ycpp-0.1.0.data/headers/xsimd/config/xsimd_arch.hpp +242 -0
  80. ycpp-0.1.0.data/headers/xsimd/config/xsimd_config.hpp +620 -0
  81. ycpp-0.1.0.data/headers/xsimd/config/xsimd_cpu_features.hpp +50 -0
  82. ycpp-0.1.0.data/headers/xsimd/config/xsimd_cpu_features_arm.hpp +132 -0
  83. ycpp-0.1.0.data/headers/xsimd/config/xsimd_cpu_features_ppc.hpp +54 -0
  84. ycpp-0.1.0.data/headers/xsimd/config/xsimd_cpu_features_riscv.hpp +95 -0
  85. ycpp-0.1.0.data/headers/xsimd/config/xsimd_cpu_features_s390x.hpp +56 -0
  86. ycpp-0.1.0.data/headers/xsimd/config/xsimd_cpu_features_x86.hpp +1044 -0
  87. ycpp-0.1.0.data/headers/xsimd/config/xsimd_cpuid.hpp +144 -0
  88. ycpp-0.1.0.data/headers/xsimd/config/xsimd_getauxval.hpp +159 -0
  89. ycpp-0.1.0.data/headers/xsimd/config/xsimd_macros.hpp +76 -0
  90. ycpp-0.1.0.data/headers/xsimd/math/xsimd_rem_pio2.hpp +707 -0
  91. ycpp-0.1.0.data/headers/xsimd/memory/xsimd_aligned_allocator.hpp +349 -0
  92. ycpp-0.1.0.data/headers/xsimd/memory/xsimd_alignment.hpp +102 -0
  93. ycpp-0.1.0.data/headers/xsimd/types/xsimd_all_registers.hpp +57 -0
  94. ycpp-0.1.0.data/headers/xsimd/types/xsimd_api.hpp +3106 -0
  95. ycpp-0.1.0.data/headers/xsimd/types/xsimd_avx2_register.hpp +44 -0
  96. ycpp-0.1.0.data/headers/xsimd/types/xsimd_avx512bw_register.hpp +51 -0
  97. ycpp-0.1.0.data/headers/xsimd/types/xsimd_avx512cd_register.hpp +51 -0
  98. ycpp-0.1.0.data/headers/xsimd/types/xsimd_avx512dq_register.hpp +51 -0
  99. ycpp-0.1.0.data/headers/xsimd/types/xsimd_avx512er_register.hpp +51 -0
  100. ycpp-0.1.0.data/headers/xsimd/types/xsimd_avx512f_register.hpp +77 -0
  101. ycpp-0.1.0.data/headers/xsimd/types/xsimd_avx512ifma_register.hpp +51 -0
  102. ycpp-0.1.0.data/headers/xsimd/types/xsimd_avx512pf_register.hpp +51 -0
  103. ycpp-0.1.0.data/headers/xsimd/types/xsimd_avx512vbmi2_register.hpp +51 -0
  104. ycpp-0.1.0.data/headers/xsimd/types/xsimd_avx512vbmi_register.hpp +51 -0
  105. ycpp-0.1.0.data/headers/xsimd/types/xsimd_avx512vnni_avx512bw_register.hpp +54 -0
  106. ycpp-0.1.0.data/headers/xsimd/types/xsimd_avx512vnni_avx512vbmi2_register.hpp +53 -0
  107. ycpp-0.1.0.data/headers/xsimd/types/xsimd_avx_register.hpp +64 -0
  108. ycpp-0.1.0.data/headers/xsimd/types/xsimd_avxvnni_register.hpp +44 -0
  109. ycpp-0.1.0.data/headers/xsimd/types/xsimd_batch.hpp +1665 -0
  110. ycpp-0.1.0.data/headers/xsimd/types/xsimd_batch_constant.hpp +571 -0
  111. ycpp-0.1.0.data/headers/xsimd/types/xsimd_batch_fwd.hpp +41 -0
  112. ycpp-0.1.0.data/headers/xsimd/types/xsimd_common_arch.hpp +49 -0
  113. ycpp-0.1.0.data/headers/xsimd/types/xsimd_emulated_register.hpp +80 -0
  114. ycpp-0.1.0.data/headers/xsimd/types/xsimd_fma3_avx2_register.hpp +50 -0
  115. ycpp-0.1.0.data/headers/xsimd/types/xsimd_fma3_avx_register.hpp +50 -0
  116. ycpp-0.1.0.data/headers/xsimd/types/xsimd_fma3_sse_register.hpp +50 -0
  117. ycpp-0.1.0.data/headers/xsimd/types/xsimd_fma4_register.hpp +50 -0
  118. ycpp-0.1.0.data/headers/xsimd/types/xsimd_i8mm_neon64_register.hpp +55 -0
  119. ycpp-0.1.0.data/headers/xsimd/types/xsimd_neon64_register.hpp +55 -0
  120. ycpp-0.1.0.data/headers/xsimd/types/xsimd_neon_register.hpp +130 -0
  121. ycpp-0.1.0.data/headers/xsimd/types/xsimd_register.hpp +96 -0
  122. ycpp-0.1.0.data/headers/xsimd/types/xsimd_rvv_register.hpp +491 -0
  123. ycpp-0.1.0.data/headers/xsimd/types/xsimd_sse2_register.hpp +59 -0
  124. ycpp-0.1.0.data/headers/xsimd/types/xsimd_sse3_register.hpp +49 -0
  125. ycpp-0.1.0.data/headers/xsimd/types/xsimd_sse4_1_register.hpp +48 -0
  126. ycpp-0.1.0.data/headers/xsimd/types/xsimd_sse4_2_register.hpp +48 -0
  127. ycpp-0.1.0.data/headers/xsimd/types/xsimd_ssse3_register.hpp +48 -0
  128. ycpp-0.1.0.data/headers/xsimd/types/xsimd_sve_register.hpp +216 -0
  129. ycpp-0.1.0.data/headers/xsimd/types/xsimd_traits.hpp +408 -0
  130. ycpp-0.1.0.data/headers/xsimd/types/xsimd_utils.hpp +462 -0
  131. ycpp-0.1.0.data/headers/xsimd/types/xsimd_vsx_register.hpp +77 -0
  132. ycpp-0.1.0.data/headers/xsimd/types/xsimd_vxe_register.hpp +86 -0
  133. ycpp-0.1.0.data/headers/xsimd/types/xsimd_wasm_register.hpp +59 -0
  134. ycpp-0.1.0.data/headers/xsimd/utils/bits.hpp +122 -0
  135. ycpp-0.1.0.data/headers/xsimd/utils/xsimd_type_traits.hpp +145 -0
  136. ycpp-0.1.0.data/headers/xsimd/xsimd.hpp +43 -0
  137. ycpp-0.1.0.dist-info/DELVEWHEEL +2 -0
  138. ycpp-0.1.0.dist-info/METADATA +15 -0
  139. ycpp-0.1.0.dist-info/RECORD +142 -0
  140. ycpp-0.1.0.dist-info/WHEEL +4 -0
  141. ycpp.libs/msvcp140-a4c2229bdc2a2a630acdc095b4d86008.dll +0 -0
  142. ycpp.libs/msvcp140_1-226498d4bfcb4f07974757b596ebec45.dll +0 -0
ycpp/_xml.py ADDED
@@ -0,0 +1,452 @@
1
+ """Xml types, shaped like pycrdt's ``_xml.py``.
2
+
3
+ The fragment, element and text handles are wrappers over the integrated nodes; the low-level calls
4
+ they make are what ``python/src/_ycpp.cpp`` has to provide (see ``python/NEEDS.md``).
5
+ """
6
+
7
+ from __future__ import annotations
8
+
9
+ from ._event import _Event, Subscription, arg_count
10
+ from . import _ycpp
11
+
12
+
13
+ def _diff_value(doc, value):
14
+ """The Python wrapper for a shared type a diff reports; other values stay as they are."""
15
+ from ._array import Array
16
+ from ._map import Map
17
+ from ._text import Text
18
+
19
+ if isinstance(value, (_ycpp.XmlFragmentRef, _ycpp.XmlElementRef, _ycpp.XmlTextRef)):
20
+ return _integrated_to_wrapper(doc, value)
21
+ if isinstance(value, _ycpp.MapRef):
22
+ return Map(_doc=doc, _integrated=value)
23
+ if isinstance(value, _ycpp.TextRef):
24
+ return Text(_doc=doc, _integrated=value)
25
+ if isinstance(value, _ycpp.ArrayRef):
26
+ return Array(_doc=doc, _integrated=value)
27
+ return value
28
+
29
+
30
+ def _integrated_to_wrapper(doc, inner):
31
+ """The Python wrapper for an integrated node a binding handed back."""
32
+ kind = inner.kind()
33
+ if kind == "element":
34
+ return XmlElement(_doc=doc, _integrated=inner)
35
+ if kind == "text":
36
+ return XmlText(_doc=doc, _integrated=inner)
37
+ return XmlFragment(_doc=doc, _integrated=inner)
38
+
39
+
40
+ def _check_slice(value, key) -> None:
41
+ if value.step is not None:
42
+ raise RuntimeError("Step not supported")
43
+ if (value.start is not None and value.start < 0) or (
44
+ value.stop is not None and value.stop < 0
45
+ ):
46
+ raise RuntimeError(f"Negative indices not supported: {key}")
47
+
48
+
49
+ def _embedded_kind(value):
50
+ """The shared type an embedded wrapper stands for, or None for a plain value."""
51
+ from ._array import Array
52
+ from ._map import Map
53
+ from ._text import Text
54
+
55
+ if isinstance(value, XmlText):
56
+ return "xml-text"
57
+ if isinstance(value, XmlElement):
58
+ return "xml-element"
59
+ if isinstance(value, XmlFragment):
60
+ return "xml-fragment"
61
+ if isinstance(value, Map):
62
+ return "map"
63
+ if isinstance(value, Array):
64
+ return "array"
65
+ if isinstance(value, Text):
66
+ return "text"
67
+ return None
68
+
69
+
70
+ class XmlEvent(_Event):
71
+ """The event an xml node reports: which attributes or children a transaction changed."""
72
+
73
+ __slots__ = (
74
+ "children_changed",
75
+ "target",
76
+ "path",
77
+ "delta",
78
+ "keys",
79
+ "_doc",
80
+ "_transaction",
81
+ )
82
+
83
+ def __init__(self, target, children_changed, keys, delta, doc, path=None) -> None:
84
+ self.target = target
85
+ self.children_changed = children_changed
86
+ self.keys = keys
87
+ self.delta = delta
88
+ self.path = [] if path is None else path
89
+ self._doc = doc
90
+ self._transaction = None
91
+
92
+ def __repr__(self) -> str:
93
+ return (
94
+ f"XmlEvent(children_changed={self.children_changed!r}, "
95
+ f"keys={self.keys!r}, path={self.path!r})"
96
+ )
97
+
98
+
99
+ class _XmlBase:
100
+ def __init__(self, *, _doc=None, _integrated=None, _prelim=None) -> None:
101
+ self._doc = _doc
102
+ self._ref = _integrated
103
+ self._prelim = None if _integrated is not None else _prelim
104
+ self._subscriptions = []
105
+
106
+ @property
107
+ def doc(self):
108
+ if self._doc is None:
109
+ raise RuntimeError("Not integrated in a document yet")
110
+ return self._doc
111
+
112
+ @property
113
+ def integrated(self):
114
+ if self._ref is None:
115
+ raise RuntimeError("Not integrated in a document yet")
116
+ return self._ref
117
+
118
+ @property
119
+ def is_prelim(self) -> bool:
120
+ return self._ref is None
121
+
122
+ @property
123
+ def is_integrated(self) -> bool:
124
+ return self._ref is not None
125
+
126
+ @property
127
+ def prelim(self):
128
+ return self._prelim
129
+
130
+ def _integrate(self, doc, ref) -> None:
131
+ self._doc = doc
132
+ self._ref = ref
133
+
134
+ @property
135
+ def parent(self):
136
+ inner = self.integrated.parent()
137
+ return None if inner is None else _integrated_to_wrapper(self.doc, inner)
138
+
139
+ def __str__(self) -> str:
140
+ return self.integrated.get_string()
141
+
142
+ def __eq__(self, other) -> bool:
143
+ if isinstance(other, _XmlBase):
144
+ return self.integrated.key() == other.integrated.key()
145
+ return NotImplemented
146
+
147
+ def __hash__(self) -> int:
148
+ return hash(self.integrated.key())
149
+
150
+ def observe(self, callback) -> Subscription:
151
+ observer = self.doc._observe_xml(self.integrated)
152
+ subscription = Subscription(self.doc, observer)
153
+ self.doc._xml_observers[observer] = (self, callback, arg_count(callback))
154
+ self._subscriptions.append(subscription)
155
+ return subscription
156
+
157
+ def observe_deep(self, callback) -> Subscription:
158
+ observer = self.doc._observe_deep_xml(self.integrated)
159
+ subscription = Subscription(self.doc, observer)
160
+ self.doc._deep_observers[observer] = (callback, arg_count(callback))
161
+ self._subscriptions.append(subscription)
162
+ return subscription
163
+
164
+ def unobserve(self, subscription) -> None:
165
+ self._subscriptions.remove(subscription)
166
+ subscription.drop()
167
+
168
+ def to_py(self):
169
+ raise ValueError("XmlFragment has no Python equivalent")
170
+
171
+
172
+ class XmlFragment(_XmlBase):
173
+ """A shared xml fragment: the root the other xml nodes live in."""
174
+
175
+ def __init__(self, init=(), *, _doc=None, _integrated=None) -> None:
176
+ super().__init__(_doc=_doc, _integrated=_integrated, _prelim=list(init))
177
+
178
+ def _get_or_insert(self, doc, name):
179
+ self._doc = doc
180
+ self._ref = doc._doc.get_or_insert_xml_fragment(name)
181
+
182
+ def _init(self) -> None:
183
+ prelim, self._prelim = self._prelim, None
184
+ if not prelim:
185
+ return
186
+ # One transaction for the whole tree: appending an element with children is one change.
187
+ with self.doc.transaction():
188
+ for child in prelim:
189
+ self.children.append(child)
190
+
191
+ @property
192
+ def children(self) -> XmlChildrenView:
193
+ return XmlChildrenView(self)
194
+
195
+ def __len__(self) -> int:
196
+ if self._ref is None:
197
+ return len(self._prelim)
198
+ return len(self._ref)
199
+
200
+ def __repr__(self) -> str:
201
+ return f"XmlFragment({str(self)!r})"
202
+
203
+
204
+ class XmlElement(XmlFragment):
205
+ """A shared xml element: a tag name, attributes and children."""
206
+
207
+ def __init__(
208
+ self, tag=None, attributes=None, contents=None, *, _doc=None, _integrated=None
209
+ ) -> None:
210
+ _XmlBase.__init__(self, _doc=_doc, _integrated=_integrated, _prelim=None)
211
+ if _integrated is not None:
212
+ return
213
+ if tag is None:
214
+ raise ValueError("XmlElement requires a tag name")
215
+ self._tag = tag
216
+ self._attributes = {} if attributes is None else dict(attributes)
217
+ self._prelim = list(contents or ())
218
+
219
+ def _get_or_insert(self, doc, name):
220
+ raise ValueError(
221
+ "Cannot get an XmlElement from a doc, get an XmlFragment instead"
222
+ )
223
+
224
+ def _init(self) -> None:
225
+ # One transaction for attributes and children alike: appending the element is one change.
226
+ with self.doc.transaction():
227
+ for key, value in self._attributes.items():
228
+ self.attributes[key] = value
229
+ for child in self._prelim or ():
230
+ self.children.append(child)
231
+ self._prelim = None
232
+
233
+ @property
234
+ def tag(self) -> str:
235
+ if self._ref is None:
236
+ return self._tag
237
+ return self._ref.tag()
238
+
239
+ @property
240
+ def attributes(self) -> XmlAttributesView:
241
+ return XmlAttributesView(self)
242
+
243
+ def to_py(self):
244
+ raise ValueError("XmlElement has no Python equivalent")
245
+
246
+ def __repr__(self) -> str:
247
+ return f"XmlElement({self.tag!r})"
248
+
249
+
250
+ class XmlText(_XmlBase):
251
+ """A shared xml text: a text node that renders its formatting as nested elements."""
252
+
253
+ def __init__(self, init="", *, _doc=None, _integrated=None) -> None:
254
+ super().__init__(_doc=_doc, _integrated=_integrated, _prelim=init)
255
+
256
+ def _get_or_insert(self, doc, name):
257
+ raise ValueError("Cannot get an XmlText from a doc, get an XmlFragment instead")
258
+
259
+ def _init(self) -> None:
260
+ if self._prelim:
261
+ self.insert(0, self._prelim)
262
+ self._prelim = None
263
+
264
+ def to_py(self):
265
+ if self._ref is None:
266
+ return self._prelim
267
+ return str(self)
268
+
269
+ def __str__(self) -> str:
270
+ if self._ref is None:
271
+ return self._prelim or ""
272
+ return self._ref.get_string()
273
+
274
+ def __len__(self) -> int:
275
+ if self._ref is None:
276
+ return len(self._prelim)
277
+ return self._ref.len()
278
+
279
+ def __iadd__(self, value):
280
+ self.insert(len(self), value)
281
+ return self
282
+
283
+ def insert(self, index: int, value: str, attrs=None) -> None:
284
+ self.integrated.insert(index, value, {} if attrs is None else attrs)
285
+
286
+ def insert_embed(self, index: int, value, attrs=None) -> None:
287
+ kind = _embedded_kind(value)
288
+ if kind is None:
289
+ self.integrated.insert_embed(index, value, {} if attrs is None else attrs)
290
+ return
291
+ name = value.tag if isinstance(value, XmlElement) else None
292
+ value._integrate(self.doc, self.integrated.insert_type(index, kind, name))
293
+ if isinstance(value, _XmlBase):
294
+ value._init()
295
+
296
+ @property
297
+ def attributes(self) -> XmlAttributesView:
298
+ return XmlAttributesView(self)
299
+
300
+ def format(self, start: int, stop: int, attrs) -> None:
301
+ if len(self) > 0:
302
+ self.integrated.format(start, stop, attrs)
303
+
304
+ def diff(self):
305
+ return [
306
+ (_diff_value(self.doc, value), attrs)
307
+ for value, attrs in self.integrated.diff()
308
+ ]
309
+
310
+ def __delitem__(self, index) -> None:
311
+ if isinstance(index, slice):
312
+ _check_slice(index, index)
313
+ start = 0 if index.start is None else index.start
314
+ stop = len(self) if index.stop is None else index.stop
315
+ self.integrated.remove_range(start, stop)
316
+ return
317
+ if not isinstance(index, int):
318
+ raise TypeError(f"Invalid index type: {type(index)}")
319
+ position = index + len(self) if index < 0 else index
320
+ self.integrated.remove_range(position, position + 1)
321
+
322
+ def clear(self) -> None:
323
+ self.integrated.remove_range(0, len(self))
324
+
325
+ def __repr__(self) -> str:
326
+ return f"XmlText({self.to_py()!r})"
327
+
328
+
329
+ def _is_fragment(value) -> bool:
330
+ """A plain fragment: an element is a fragment too, but only fragments may be stored."""
331
+ return isinstance(value, XmlFragment) and not isinstance(value, XmlElement)
332
+
333
+
334
+ class XmlAttributesView:
335
+ """The attributes of an element, as a mutable mapping."""
336
+
337
+ def __init__(self, element: XmlElement) -> None:
338
+ self._element = element
339
+
340
+ @property
341
+ def _ref(self):
342
+ return self._element.integrated
343
+
344
+ def get(self, key, default=None):
345
+ value = self._ref.attribute(key)
346
+ return default if value is None else value
347
+
348
+ def __getitem__(self, key):
349
+ value = self._ref.attribute(key)
350
+ if value is None:
351
+ raise KeyError(key)
352
+ return value
353
+
354
+ def __setitem__(self, key, value) -> None:
355
+ self._ref.insert_attribute(key, value)
356
+
357
+ def __delitem__(self, key) -> None:
358
+ if not self._ref.remove_attribute(key):
359
+ raise KeyError(key)
360
+
361
+ def __contains__(self, key) -> bool:
362
+ return self._ref.attribute(key) is not None
363
+
364
+ def __len__(self) -> int:
365
+ return len(self._ref.attributes())
366
+
367
+ def __iter__(self):
368
+ return iter(self._ref.attributes())
369
+
370
+ def __repr__(self) -> str:
371
+ return f"XmlAttributesView({list(self)!r})"
372
+
373
+
374
+ class XmlChildrenView:
375
+ """The children of a fragment or element, as a mutable sequence of nodes."""
376
+
377
+ def __init__(self, parent: XmlFragment) -> None:
378
+ self._parent = parent
379
+
380
+ @property
381
+ def _ref(self):
382
+ return self._parent.integrated
383
+
384
+ def __len__(self) -> int:
385
+ return len(self._ref)
386
+
387
+ def __getitem__(self, index):
388
+ if isinstance(index, slice):
389
+ _check_slice(index, index)
390
+ start = 0 if index.start is None else index.start
391
+ stop = len(self) if index.stop is None else index.stop
392
+ return [
393
+ _integrated_to_wrapper(self._parent.doc, self._ref.get(position))
394
+ for position in range(start, stop)
395
+ ]
396
+ if not isinstance(index, int):
397
+ raise TypeError(f"Invalid index type: {type(index)}")
398
+ position = index + len(self) if index < 0 else index
399
+ if position < 0 or position >= len(self):
400
+ raise IndexError("Index out of range")
401
+ return _integrated_to_wrapper(self._parent.doc, self._ref.get(position))
402
+
403
+ def __setitem__(self, index, value) -> None:
404
+ self._ref.remove_range(index, index + 1)
405
+ self.insert(index, value)
406
+
407
+ def __delitem__(self, index) -> None:
408
+ if isinstance(index, slice):
409
+ _check_slice(index, index)
410
+ start = 0 if index.start is None else index.start
411
+ stop = len(self) if index.stop is None else index.stop
412
+ self._ref.remove_range(start, stop)
413
+ return
414
+ if not isinstance(index, int):
415
+ raise TypeError(f"Invalid index type: {type(index)}")
416
+ position = index + len(self) if index < 0 else index
417
+ self._ref.remove_range(position, position + 1)
418
+
419
+ def __iter__(self):
420
+ # Membership is snapshotted when iter() is called: an edit during iteration is not seen.
421
+ return iter([self[index] for index in range(len(self))])
422
+
423
+ def insert(self, index: int, element) -> None:
424
+ if index > len(self):
425
+ raise IndexError("Index out of range")
426
+ if isinstance(element, str):
427
+ element = XmlText(element)
428
+ # One transaction for the node and the content it brings: appending an element with text is
429
+ # one change, as it is in Yjs.
430
+ with self._parent.doc.transaction():
431
+ if isinstance(element, XmlText):
432
+ if element.is_integrated:
433
+ raise ValueError("Cannot insert an integrated XmlText")
434
+ element._integrate(
435
+ self._parent.doc, self._ref.insert_str(index, str(element))
436
+ )
437
+ return
438
+ if isinstance(element, XmlElement):
439
+ if element.is_integrated:
440
+ raise ValueError("Cannot insert an integrated XmlElement")
441
+ element._integrate(
442
+ self._parent.doc, self._ref.insert_element(index, element.tag)
443
+ )
444
+ element._init()
445
+ return
446
+ raise TypeError(f"Cannot add value to XML: {element!r}")
447
+
448
+ def append(self, element) -> None:
449
+ self.insert(len(self), element)
450
+
451
+ def __repr__(self) -> str:
452
+ return f"XmlChildrenView({list(self)!r})"
Binary file
Binary file