Skip to content

Commit 0119417

Browse files
committed
helpers and pythonic
Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
1 parent 196424e commit 0119417

4 files changed

Lines changed: 39 additions & 40 deletions

File tree

cyclonedx/model/bom.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -292,10 +292,7 @@ def manufacture(self, manufacture: Optional[OrganizationalEntity]) -> None:
292292
we should set this data on `.component.manufacturer`.
293293
"""
294294
if manufacture is not None:
295-
warn(
296-
'`bom.metadata.manufacture` is deprecated from CycloneDX v1.6 onwards. '
297-
'Please use `bom.metadata.component.manufacturer` instead.',
298-
DeprecationWarning1Dot6)
295+
warn(*DeprecationWarning1Dot6._prepw('bom.metadata.manufacture', 'bom.metadata.component.manufacturer'))
299296
self._manufacture = manufacture
300297

301298
@property

cyclonedx/model/component.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,8 +1186,7 @@ def author(self) -> Optional[str]:
11861186
@author.setter
11871187
def author(self, author: Optional[str]) -> None:
11881188
if author is not None:
1189-
warn('`@.author` is deprecated from CycloneDX v1.6 onwards. '
1190-
'Please use `@.authors` or `@.manufacturer` instead.', DeprecationWarning1Dot6)
1189+
warn(*DeprecationWarning1Dot6._prepw('@.author', '@.authors` or `@.manufacturer'))
11911190
self._author = author
11921191

11931192
@property
@@ -1468,8 +1467,7 @@ def modified(self) -> bool:
14681467
@modified.setter
14691468
def modified(self, modified: bool) -> None:
14701469
if modified:
1471-
warn('`@.modified` is deprecated from CycloneDX v1.3 onwards. '
1472-
'Please use `@.pedigree` instead.', DeprecationWarning1Dot3)
1470+
warn(*DeprecationWarning1Dot3._prepw('@.modified', '@.pedigree'))
14731471
self._modified = modified
14741472

14751473
@property

cyclonedx/model/tool.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,7 @@ def tools(self) -> 'SortedSet[Tool]':
241241
@tools.setter
242242
def tools(self, tools: Iterable[Tool]) -> None:
243243
if tools:
244-
warn('`@.tools` is deprecated from CycloneDX v1.5 onwards. '
245-
'Please use `@.components` and `@.services` instead.',
246-
DeprecationWarning1Dot5)
244+
warn(*DeprecationWarning1Dot5._prepw('@.tools', '@.components` and `@.services'))
247245
self._tools = SortedSet(tools)
248246

249247
def __len__(self) -> int:

cyclonedx/schema/deprecation.py

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,59 +15,65 @@
1515
# SPDX-License-Identifier: Apache-2.0
1616
# Copyright (c) OWASP Foundation. All Rights Reserved.
1717

18-
from abc import ABC, abstractmethod
19-
from typing import Literal
18+
from abc import ABC
19+
from typing import ClassVar, Optional
2020

2121
from . import SchemaVersion
2222

23+
__all__ = [
24+
'SchemaDeprecationWarning',
25+
'DeprecationWarning1Dot1',
26+
'DeprecationWarning1Dot2',
27+
'DeprecationWarning1Dot3',
28+
'DeprecationWarning1Dot4',
29+
'DeprecationWarning1Dot5',
30+
'DeprecationWarning1Dot6',
31+
'DeprecationWarning1Dot7',
32+
]
33+
2334

2435
class SchemaDeprecationWarning(DeprecationWarning, ABC):
25-
@property
26-
@abstractmethod
27-
def schema_version_enum(self) -> SchemaVersion:
28-
... # pragma: no cover
36+
SCHEMA_VERSION: ClassVar[SchemaVersion]
37+
38+
@classmethod
39+
def _prepw(cls, deprecated: str, instead: Optional[str] = None) -> tuple[str, type[SchemaDeprecationWarning]]:
40+
"""Prepare the warning message and category for schema deprecations.
41+
42+
Internal API. Not part of the public interface.
43+
44+
Intended to be used as:
2945
30-
def get_schema_version(self) -> str:
31-
return self.schema_version_enum.to_version()
46+
warnings.warn(*SchemaDeprecationWarning._prepw("foo", "bar"))
47+
"""
48+
w = f'`{deprecated}` is deprecated from CycloneDX v{cls.SCHEMA_VERSION.to_version()} onwards.'
49+
if instead is not None:
50+
w += f' Please use `{instead}` instead.'
51+
return w, cls
3252

3353

3454
class DeprecationWarning1Dot7(SchemaDeprecationWarning):
35-
@property
36-
def schema_version_enum(self) -> Literal[SchemaVersion.V1_7]:
37-
return SchemaVersion.V1_7
55+
SCHEMA_VERSION = SchemaVersion.V1_7
3856

3957

4058
class DeprecationWarning1Dot6(SchemaDeprecationWarning):
41-
@property
42-
def schema_version_enum(self) -> Literal[SchemaVersion.V1_6]:
43-
return SchemaVersion.V1_6
59+
SCHEMA_VERSION = SchemaVersion.V1_6
4460

4561

4662
class DeprecationWarning1Dot5(SchemaDeprecationWarning):
47-
@property
48-
def schema_version_enum(self) -> Literal[SchemaVersion.V1_5]:
49-
return SchemaVersion.V1_5
63+
SCHEMA_VERSION = SchemaVersion.V1_5
5064

5165

5266
class DeprecationWarning1Dot4(SchemaDeprecationWarning):
53-
@property
54-
def schema_version_enum(self) -> Literal[SchemaVersion.V1_4]:
55-
return SchemaVersion.V1_4
67+
SCHEMA_VERSION = SchemaVersion.V1_4
5668

5769

5870
class DeprecationWarning1Dot3(SchemaDeprecationWarning):
59-
@property
60-
def schema_version_enum(self) -> Literal[SchemaVersion.V1_3]:
61-
return SchemaVersion.V1_3
71+
SCHEMA_VERSION = SchemaVersion.V1_3
6272

6373

6474
class DeprecationWarning1Dot2(SchemaDeprecationWarning):
65-
@property
66-
def schema_version_enum(self) -> Literal[SchemaVersion.V1_2]:
67-
return SchemaVersion.V1_2
75+
_schema_version_enum = SchemaVersion.V1_2
6876

6977

7078
class DeprecationWarning1Dot1(SchemaDeprecationWarning):
71-
@property
72-
def schema_version_enum(self) -> Literal[SchemaVersion.V1_1]:
73-
return SchemaVersion.V1_1
79+
_schema_version_enum = SchemaVersion.V1_1

0 commit comments

Comments
 (0)