|
15 | 15 | # SPDX-License-Identifier: Apache-2.0 |
16 | 16 | # Copyright (c) OWASP Foundation. All Rights Reserved. |
17 | 17 |
|
18 | | -from abc import ABC, abstractmethod |
19 | | -from typing import Literal |
| 18 | +from abc import ABC |
| 19 | +from typing import ClassVar, Optional |
20 | 20 |
|
21 | 21 | from . import SchemaVersion |
22 | 22 |
|
| 23 | +__all__ = [ |
| 24 | + 'SchemaDeprecationWarning', |
| 25 | + 'DeprecationWarning1Dot1', |
| 26 | + 'DeprecationWarning1Dot2', |
| 27 | + 'DeprecationWarning1Dot3', |
| 28 | + 'DeprecationWarning1Dot4', |
| 29 | + 'DeprecationWarning1Dot5', |
| 30 | + 'DeprecationWarning1Dot6', |
| 31 | + 'DeprecationWarning1Dot7', |
| 32 | +] |
| 33 | + |
23 | 34 |
|
24 | 35 | 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: |
29 | 45 |
|
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 |
32 | 52 |
|
33 | 53 |
|
34 | 54 | 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 |
38 | 56 |
|
39 | 57 |
|
40 | 58 | 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 |
44 | 60 |
|
45 | 61 |
|
46 | 62 | 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 |
50 | 64 |
|
51 | 65 |
|
52 | 66 | 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 |
56 | 68 |
|
57 | 69 |
|
58 | 70 | 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 |
62 | 72 |
|
63 | 73 |
|
64 | 74 | 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 |
68 | 76 |
|
69 | 77 |
|
70 | 78 | 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