Skip to content

Commit dff4254

Browse files
committed
feat: make schema deprecation warnings handle-able
Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
1 parent 44f1273 commit dff4254

File tree

5 files changed

+91
-8
lines changed

5 files changed

+91
-8
lines changed

cyclonedx/model/bom.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from .._internal.compare import ComparableTuple as _ComparableTuple
3131
from .._internal.time import get_now_utc as _get_now_utc
3232
from ..exception.model import LicenseExpressionAlongWithOthersException, UnknownComponentDependencyException
33+
from ..schema.deprecation import DeprecationWarning1Dot6
3334
from ..schema.schema import (
3435
SchemaVersion1Dot0,
3536
SchemaVersion1Dot1,
@@ -294,7 +295,7 @@ def manufacture(self, manufacture: Optional[OrganizationalEntity]) -> None:
294295
warn(
295296
'`bom.metadata.manufacture` is deprecated from CycloneDX v1.6 onwards. '
296297
'Please use `bom.metadata.component.manufacturer` instead.',
297-
DeprecationWarning)
298+
DeprecationWarning1Dot6)
298299
self._manufacture = manufacture
299300

300301
@property

cyclonedx/model/component.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
SerializationOfUnexpectedValueException,
4141
SerializationOfUnsupportedComponentTypeException,
4242
)
43+
from ..schema.deprecation import DeprecationWarning1Dot3, DeprecationWarning1Dot6
4344
from ..schema.schema import (
4445
SchemaVersion1Dot0,
4546
SchemaVersion1Dot1,
@@ -1186,7 +1187,7 @@ def author(self) -> Optional[str]:
11861187
def author(self, author: Optional[str]) -> None:
11871188
if author is not None:
11881189
warn('`@.author` is deprecated from CycloneDX v1.6 onwards. '
1189-
'Please use `@.authors` or `@.manufacturer` instead.', DeprecationWarning)
1190+
'Please use `@.authors` or `@.manufacturer` instead.', DeprecationWarning1Dot6)
11901191
self._author = author
11911192

11921193
@property
@@ -1468,7 +1469,7 @@ def modified(self) -> bool:
14681469
def modified(self, modified: bool) -> None:
14691470
if modified:
14701471
warn('`@.modified` is deprecated from CycloneDX v1.3 onwards. '
1471-
'Please use `@.pedigree` instead.', DeprecationWarning)
1472+
'Please use `@.pedigree` instead.', DeprecationWarning1Dot3)
14721473
self._modified = modified
14731474

14741475
@property

cyclonedx/model/tool.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
from .._internal.compare import ComparableTuple as _ComparableTuple
3030
from ..schema import SchemaVersion
31+
from ..schema.deprecation import DeprecationWarning1Dot5
3132
from ..schema.schema import SchemaVersion1Dot4, SchemaVersion1Dot5, SchemaVersion1Dot6, SchemaVersion1Dot7
3233
from . import ExternalReference, HashType, _HashTypeRepositorySerializationHelper
3334
from .component import Component
@@ -242,7 +243,7 @@ def tools(self, tools: Iterable[Tool]) -> None:
242243
if tools:
243244
warn('`@.tools` is deprecated from CycloneDX v1.5 onwards. '
244245
'Please use `@.components` and `@.services` instead.',
245-
DeprecationWarning)
246+
DeprecationWarning1Dot5)
246247
self._tools = SortedSet(tools)
247248

248249
def __len__(self) -> int:

cyclonedx/schema/deprecation.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# This file is part of CycloneDX Python Library
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
# SPDX-License-Identifier: Apache-2.0
16+
# Copyright (c) OWASP Foundation. All Rights Reserved.
17+
18+
from abc import ABC, abstractmethod
19+
from typing import Literal
20+
21+
from . import SchemaVersion
22+
23+
24+
class SchemaDeprecationWarning(DeprecationWarning, ABC):
25+
@property
26+
@abstractmethod
27+
def schema_version_enum(self) -> SchemaVersion:
28+
... # pragma: no cover
29+
30+
def get_schema_version(self) -> str:
31+
return self.schema_version_enum.to_version()
32+
33+
34+
class DeprecationWarning1Dot7(SchemaDeprecationWarning):
35+
@property
36+
def schema_version_enum(self) -> Literal[SchemaVersion.V1_7]:
37+
return SchemaVersion.V1_7
38+
39+
40+
class DeprecationWarning1Dot6(SchemaDeprecationWarning):
41+
@property
42+
def schema_version_enum(self) -> Literal[SchemaVersion.V1_6]:
43+
return SchemaVersion.V1_6
44+
45+
46+
class DeprecationWarning1Dot5(SchemaDeprecationWarning):
47+
@property
48+
def schema_version_enum(self) -> Literal[SchemaVersion.V1_5]:
49+
return SchemaVersion.V1_5
50+
51+
52+
class DeprecationWarning1Dot4(SchemaDeprecationWarning):
53+
@property
54+
def schema_version_enum(self) -> Literal[SchemaVersion.V1_4]:
55+
return SchemaVersion.V1_4
56+
57+
58+
class DeprecationWarning1Dot3(SchemaDeprecationWarning):
59+
@property
60+
def schema_version_enum(self) -> Literal[SchemaVersion.V1_3]:
61+
return SchemaVersion.V1_3
62+
63+
64+
class DeprecationWarning1Dot2(SchemaDeprecationWarning):
65+
@property
66+
def schema_version_enum(self) -> Literal[SchemaVersion.V1_2]:
67+
return SchemaVersion.V1_2
68+
69+
70+
class DeprecationWarning1Dot1(SchemaDeprecationWarning):
71+
@property
72+
def schema_version_enum(self) -> Literal[SchemaVersion.V1_1]:
73+
return SchemaVersion.V1_1

examples/complex_deserialize.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# Copyright (c) OWASP Foundation. All Rights Reserved.
1717

1818
import sys
19+
import warnings
1920
from json import loads as json_loads
2021
from typing import TYPE_CHECKING
2122

@@ -24,6 +25,7 @@
2425
from cyclonedx.exception import MissingOptionalDependencyException
2526
from cyclonedx.model.bom import Bom
2627
from cyclonedx.schema import OutputFormat, SchemaVersion
28+
from cyclonedx.schema.deprecation import SchemaDeprecationWarning
2729
from cyclonedx.validation import make_schemabased_validator
2830
from cyclonedx.validation.json import JsonStrictValidator
2931

@@ -100,6 +102,7 @@
100102
"type": "library",
101103
"group": "acme",
102104
"name": "some-component",
105+
"author": "John 'the mighty' Doe",
103106
"version": "1.33.7-beta.1",
104107
"purl": "pkg:generic/acme/some-component@1.33.7-beta.1",
105108
"licenses": [
@@ -154,8 +157,10 @@
154157
print('JSON valid')
155158
except MissingOptionalDependencyException as error:
156159
print('JSON-validation was skipped due to', error)
157-
bom_from_json = Bom.from_json( # type: ignore[attr-defined]
158-
json_loads(json_data))
160+
with warnings.catch_warnings():
161+
warnings.filterwarnings("ignore", category=SchemaDeprecationWarning)
162+
bom_from_json = Bom.from_json( # type: ignore[attr-defined]
163+
json_loads(json_data))
159164
print('bom_from_json', repr(bom_from_json))
160165

161166
# endregion JSON
@@ -255,8 +260,10 @@
255260
print('XML valid')
256261
except MissingOptionalDependencyException as error:
257262
print('XML-validation was skipped due to', error)
258-
bom_from_xml = Bom.from_xml( # type: ignore[attr-defined]
259-
SafeElementTree.fromstring(xml_data))
263+
with warnings.catch_warnings():
264+
warnings.filterwarnings("ignore", category=SchemaDeprecationWarning)
265+
bom_from_xml = Bom.from_xml( # type: ignore[attr-defined]
266+
SafeElementTree.fromstring(xml_data))
260267
print('bom_from_xml', repr(bom_from_xml))
261268

262269
# endregion XML

0 commit comments

Comments
 (0)