|
14 | 14 | # Copyright (c) OWASP Foundation. All Rights Reserved. |
15 | 15 |
|
16 | 16 |
|
17 | | -from typing import TYPE_CHECKING, Any, Dict, Iterable, List, Optional, Tuple, Type, Union |
| 17 | +from typing import Any, Iterable, Optional, Type |
18 | 18 | from warnings import warn |
19 | | -from xml.etree.ElementTree import Element # nosec B405 |
20 | 19 |
|
21 | 20 | import serializable |
22 | | -from serializable.helpers import BaseHelper |
23 | 21 | from sortedcontainers import SortedSet |
24 | 22 |
|
25 | 23 | from .._internal.compare import ComparableTuple as _ComparableTuple |
26 | | -from ..exception.serialization import CycloneDxDeserializationException |
27 | | -from ..schema import SchemaVersion |
28 | 24 | from ..schema.schema import SchemaVersion1Dot4, SchemaVersion1Dot5, SchemaVersion1Dot6 |
29 | 25 | from . import ExternalReference, HashType, _HashTypeRepositorySerializationHelper |
30 | 26 | from .component import Component |
31 | 27 | from .service import Service |
32 | 28 |
|
33 | | -if TYPE_CHECKING: # pragma: no cover |
34 | | - from serializable import ObjectMetadataLibrary, ViewType |
35 | | - |
36 | 29 |
|
37 | 30 | @serializable.serializable_class |
38 | 31 | class Tool: |
@@ -257,108 +250,3 @@ def __eq__(self, other: object) -> bool: |
257 | 250 |
|
258 | 251 | def __hash__(self) -> int: |
259 | 252 | return hash((tuple(self._tools), tuple(self._components), tuple(self._services))) |
260 | | - |
261 | | - |
262 | | -class ToolsRepositoryHelper(BaseHelper): |
263 | | - |
264 | | - @staticmethod |
265 | | - def __all_as_tools(o: ToolsRepository) -> Tuple[Tool, ...]: |
266 | | - return ( |
267 | | - *o.tools, |
268 | | - *map(Tool.from_component, o.components), |
269 | | - *map(Tool.from_service, o.services), |
270 | | - ) |
271 | | - |
272 | | - @staticmethod |
273 | | - def __supports_components_and_services(view: Any) -> bool: |
274 | | - try: |
275 | | - return view is not None and view().schema_version_enum >= SchemaVersion.V1_5 |
276 | | - except Exception: |
277 | | - return False |
278 | | - |
279 | | - @classmethod |
280 | | - def json_normalize(cls, o: ToolsRepository, *, |
281 | | - view: Optional[Type['ViewType']], |
282 | | - **__: Any) -> Any: |
283 | | - if not o: |
284 | | - return None |
285 | | - if len(o.tools) > 0 or not cls.__supports_components_and_services(view): |
286 | | - return cls.__all_as_tools(o) |
287 | | - return { |
288 | | - 'components': tuple(o.components) if len(o.components) > 0 else None, |
289 | | - 'services': tuple(o.services) if len(o.services) > 0 else None, |
290 | | - } |
291 | | - |
292 | | - @classmethod |
293 | | - def json_denormalize(cls, o: Union[List[Dict[str, Any]], Dict[str, Any]], |
294 | | - **__: Any) -> ToolsRepository: |
295 | | - tools = None |
296 | | - components = None |
297 | | - services = None |
298 | | - if isinstance(o, Dict): |
299 | | - components = map(lambda c: Component.from_json( # type:ignore[attr-defined] |
300 | | - c), o.get('components', ())) |
301 | | - services = map(lambda s: Service.from_json( # type:ignore[attr-defined] |
302 | | - s), o.get('services', ())) |
303 | | - elif isinstance(o, Iterable): |
304 | | - tools = map(lambda t: Tool.from_json(t), o) # type:ignore[attr-defined] |
305 | | - return ToolsRepository(components=components, services=services, tools=tools) |
306 | | - |
307 | | - @classmethod |
308 | | - def xml_normalize(cls, o: ToolsRepository, *, |
309 | | - element_name: str, |
310 | | - view: Optional[Type['ViewType']], |
311 | | - xmlns: Optional[str], |
312 | | - **__: Any) -> Optional[Element]: |
313 | | - if not o: |
314 | | - return None |
315 | | - elem = Element(element_name) |
316 | | - if len(o.tools) > 0 or not cls.__supports_components_and_services(view): |
317 | | - elem.extend( |
318 | | - ti.as_xml( # type:ignore[attr-defined] |
319 | | - view_=view, as_string=False, element_name='tool', xmlns=xmlns) |
320 | | - for ti in cls.__all_as_tools(o) |
321 | | - ) |
322 | | - else: |
323 | | - if o.components: |
324 | | - elem_c = Element(f'{{{xmlns}}}components' if xmlns else 'components') |
325 | | - elem_c.extend( |
326 | | - ci.as_xml( # type:ignore[attr-defined] |
327 | | - view_=view, as_string=False, element_name='component', xmlns=xmlns) |
328 | | - for ci in o.components) |
329 | | - elem.append(elem_c) |
330 | | - if o.services: |
331 | | - elem_s = Element(f'{{{xmlns}}}services' if xmlns else 'services') |
332 | | - elem_s.extend( |
333 | | - si.as_xml( # type:ignore[attr-defined] |
334 | | - view_=view, as_string=False, element_name='service', xmlns=xmlns) |
335 | | - for si in o.services) |
336 | | - elem.append(elem_s) |
337 | | - return elem |
338 | | - |
339 | | - @classmethod |
340 | | - def xml_denormalize(cls, o: Element, *, |
341 | | - default_ns: Optional[str], |
342 | | - prop_info: 'ObjectMetadataLibrary.SerializableProperty', |
343 | | - ctx: Type[Any], |
344 | | - **kwargs: Any) -> ToolsRepository: |
345 | | - tools = [] |
346 | | - components = None |
347 | | - services = None |
348 | | - for e in o: |
349 | | - tag = e.tag if default_ns is None else e.tag.replace(f'{{{default_ns}}}', '') |
350 | | - if tag == 'tool': |
351 | | - tools.append(Tool.from_xml( # type:ignore[attr-defined] |
352 | | - e, default_ns)) |
353 | | - elif tag == 'components': |
354 | | - components = map(lambda s: Component.from_xml( # type:ignore[attr-defined] |
355 | | - s, default_ns), e) |
356 | | - elif tag == 'services': |
357 | | - services = map(lambda s: Service.from_xml( # type:ignore[attr-defined] |
358 | | - s, default_ns), e) |
359 | | - else: |
360 | | - raise CycloneDxDeserializationException(f'unexpected: {e!r}') |
361 | | - return ToolsRepository( |
362 | | - tools=tools, |
363 | | - components=components, |
364 | | - services=services) |
0 commit comments