|
19 | 19 | from collections.abc import Iterable |
20 | 20 | from decimal import Decimal |
21 | 21 | from enum import Enum |
22 | | -from json import loads as json_loads |
23 | 22 | from typing import Any, Optional, Union |
24 | | -from warnings import warn |
25 | 23 | from xml.etree.ElementTree import Element as XmlElement |
26 | 24 |
|
27 | 25 | # See https://github.com/package-url/packageurl-python/issues/65 |
@@ -170,7 +168,7 @@ def xml_normalize(cls, o: Iterable[BomRef], *, |
170 | 168 | tool_name = f'{{{xmlns}}}tool' if xmlns else 'tool' |
171 | 169 | ref_name = f'{{{xmlns}}}ref' if xmlns else 'ref' |
172 | 170 | elem_s.extend( |
173 | | - XmlElement(tool_name, {ref_name: t.value}) \ |
| 171 | + XmlElement(tool_name, {ref_name: t.value}) |
174 | 172 | for t in o if t.value) |
175 | 173 | return elem_s |
176 | 174 |
|
@@ -284,60 +282,8 @@ def __hash__(self) -> int: |
284 | 282 |
|
285 | 283 | def __repr__(self) -> str: |
286 | 284 | return f'<Identity field={self.field}, confidence={self.confidence},' \ |
287 | | - f' concludedValue={self.concluded_value},' \ |
288 | | - f' methods={self.methods}, tools={self.tools}>' |
289 | | - |
290 | | - |
291 | | -class _IdentityRepositorySerializationHelper(serializable.helpers.BaseHelper): |
292 | | - """ THIS CLASS IS NON-PUBLIC API """ |
293 | | - |
294 | | - @classmethod |
295 | | - def json_normalize(cls, o: Iterable[Identity], *, |
296 | | - view: Optional[type['serializable.ViewType']], |
297 | | - **__: Any) -> Optional[Any]: |
298 | | - o = tuple(o) |
299 | | - if l := len(o) == 0: |
300 | | - return None |
301 | | - if view is SchemaVersion1Dot5: |
302 | | - if l >= 1: |
303 | | - warn(f'serialization omitted some identity evidences due to unsupported amount: {o!r}', |
304 | | - category=UserWarning, stacklevel=0) |
305 | | - return json_loads(o[0].as_json(view)) # type:ignore[attr-defined] |
306 | | - return tuple(json_loads(i.as_json(view)) for i in o) # type:ignore[attr-defined] |
307 | | - |
308 | | - @classmethod |
309 | | - def json_deserialize(cls, o: Any) -> tuple[Identity]: |
310 | | - if isinstance(o, list): |
311 | | - return tuple(Identity.from_json(i) for i in o) # type:ignore[attr-defined] |
312 | | - return (Identity.from_json(o),) # type:ignore[attr-defined] |
313 | | - |
314 | | - @classmethod |
315 | | - def xml_normalize(cls, o: Iterable[Identity], *, |
316 | | - element_name: str, |
317 | | - view: Optional[type['serializable.ViewType']], |
318 | | - xmlns: Optional[str], |
319 | | - **__: Any) -> Optional[XmlElement]: |
320 | | - o = tuple(o) |
321 | | - if l := len(o) == 0: |
322 | | - return None |
323 | | - if view is SchemaVersion1Dot5: |
324 | | - if l >= 1: |
325 | | - warn(f'serialization omitted some identity evidences due to unsupported amount: {o!r}', |
326 | | - category=UserWarning, stacklevel=0) |
327 | | - o = (o[0],) |
328 | | - elem_s = XmlElement(f'{{{xmlns}}}' if xmlns else '') |
329 | | - elem_s.extend(i.as_xml( # type:ignore[attr-defined] |
330 | | - view, |
331 | | - as_string=False, |
332 | | - element_name=element_name, xmlns=xmlns |
333 | | - ) for i in o) |
334 | | - return elem_s |
335 | | - |
336 | | - @classmethod |
337 | | - def xml_denormalize(cls, o: 'XmlElement', *, |
338 | | - default_ns: Optional[str], |
339 | | - **__: Any) -> Identity: |
340 | | - return Identity.from_xml(o, default_ns) # type:ignore[attr-defined,no-any-return] |
| 285 | + f' concludedValue={self.concluded_value},' \ |
| 286 | + f' methods={self.methods}, tools={self.tools}>' |
341 | 287 |
|
342 | 288 |
|
343 | 289 | @serializable.serializable_class |
@@ -703,8 +649,10 @@ def __init__( |
703 | 649 | @property |
704 | 650 | @serializable.view(SchemaVersion1Dot5) |
705 | 651 | @serializable.view(SchemaVersion1Dot6) |
706 | | - @serializable.type_mapping(_IdentityRepositorySerializationHelper) |
| 652 | + @serializable.xml_array(serializable.XmlArraySerializationType.FLAT, 'identity') |
707 | 653 | @serializable.xml_sequence(1) |
| 654 | + # TODO: CDX 1.5 knows only one identity, all versions later known multiple ... |
| 655 | + # TODO: need to fix the serializatoin/normlaization |
708 | 656 | def identity(self) -> 'SortedSet[Identity]': |
709 | 657 | """ |
710 | 658 | Provides a way to identify components via various methods. |
|
0 commit comments