Skip to content

Commit a8a4190

Browse files
committed
feat: prepare "contrib" area
Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
1 parent f6430ec commit a8a4190

5 files changed

Lines changed: 209 additions & 124 deletions

File tree

cyclonedx/builder/this.py

Lines changed: 23 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -17,67 +17,32 @@
1717

1818
"""Representation of this very python library."""
1919

20-
__all__ = ['this_component', 'this_tool', ]
20+
__all__ = []
2121

22-
from .. import __version__ as __ThisVersion # noqa: N812
23-
from ..model import ExternalReference, ExternalReferenceType, XsUri
24-
from ..model.component import Component, ComponentType
25-
from ..model.license import DisjunctiveLicense, LicenseAcknowledgement
26-
from ..model.tool import Tool
22+
from ..contrib.this import this_tool as _this_tool, this_component as _this_component
2723

28-
# !!! keep this file in sync with `pyproject.toml`
24+
# endregion deprecated re-export
2925

26+
this_component = _this_component
27+
"""
28+
Alias of :class:`_this_component`.
3029
31-
def this_component() -> Component:
32-
"""Representation of this very python library as a :class:`Component`."""
33-
return Component(
34-
type=ComponentType.LIBRARY,
35-
group='CycloneDX',
36-
name='cyclonedx-python-lib',
37-
version=__ThisVersion or 'UNKNOWN',
38-
description='Python library for CycloneDX',
39-
licenses=(DisjunctiveLicense(id='Apache-2.0',
40-
acknowledgement=LicenseAcknowledgement.DECLARED),),
41-
external_references=(
42-
# let's assume this is not a fork
43-
ExternalReference(
44-
type=ExternalReferenceType.WEBSITE,
45-
url=XsUri('https://github.com/CycloneDX/cyclonedx-python-lib/#readme')
46-
),
47-
ExternalReference(
48-
type=ExternalReferenceType.DOCUMENTATION,
49-
url=XsUri('https://cyclonedx-python-library.readthedocs.io/')
50-
),
51-
ExternalReference(
52-
type=ExternalReferenceType.VCS,
53-
url=XsUri('https://github.com/CycloneDX/cyclonedx-python-lib')
54-
),
55-
ExternalReference(
56-
type=ExternalReferenceType.BUILD_SYSTEM,
57-
url=XsUri('https://github.com/CycloneDX/cyclonedx-python-lib/actions')
58-
),
59-
ExternalReference(
60-
type=ExternalReferenceType.ISSUE_TRACKER,
61-
url=XsUri('https://github.com/CycloneDX/cyclonedx-python-lib/issues')
62-
),
63-
ExternalReference(
64-
type=ExternalReferenceType.LICENSE,
65-
url=XsUri('https://github.com/CycloneDX/cyclonedx-python-lib/blob/main/LICENSE')
66-
),
67-
ExternalReference(
68-
type=ExternalReferenceType.RELEASE_NOTES,
69-
url=XsUri('https://github.com/CycloneDX/cyclonedx-python-lib/blob/main/CHANGELOG.md')
70-
),
71-
# we cannot assert where the lib was fetched from, but we can give a hint
72-
ExternalReference(
73-
type=ExternalReferenceType.DISTRIBUTION,
74-
url=XsUri('https://pypi.org/project/cyclonedx-python-lib/')
75-
),
76-
),
77-
# to be extended...
78-
)
30+
This re-export location is deprecated.
31+
Use ``from ...contrib.this import this_component`` instead.
32+
The exported symbol itself is NOT deprecated - only this import path.
7933
34+
.. deprecated:: next
35+
"""
8036

81-
def this_tool() -> Tool:
82-
"""Representation of this very python library as a :class:`Tool`."""
83-
return Tool.from_component(this_component())
37+
this_tool = _this_tool
38+
"""
39+
Alias of :class:`_this_tool`.
40+
41+
This re-export location is deprecated.
42+
Use ``from ...contrib.this import this_tool`` instead.
43+
The exported symbol itself is NOT deprecated - only this import path.
44+
45+
.. deprecated:: next
46+
"""
47+
48+
# endregion deprecated re-export

cyclonedx/contrib/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,4 @@
2323

2424
__all__ = [
2525
# there is no intention to export anything in here.
26-
#
2726
]

cyclonedx/contrib/license.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
__all__ = ['LicenseFactory']
19+
20+
from typing import TYPE_CHECKING, Optional
21+
22+
from ..exception.factory import InvalidLicenseExpressionException, InvalidSpdxLicenseException
23+
from ..model.license import DisjunctiveLicense, LicenseExpression
24+
from ..spdx import fixup_id as spdx_fixup, is_expression as is_spdx_expression
25+
26+
if TYPE_CHECKING: # pragma: no cover
27+
from ..model import AttachedText, XsUri
28+
from ..model.license import License, LicenseAcknowledgement
29+
30+
31+
class LicenseFactory:
32+
"""Factory for :class:`cyclonedx.model.license.License`."""
33+
34+
def make_from_string(self, value: str, *,
35+
license_text: Optional['AttachedText'] = None,
36+
license_url: Optional['XsUri'] = None,
37+
license_acknowledgement: Optional['LicenseAcknowledgement'] = None
38+
) -> 'License':
39+
"""Make a :class:`cyclonedx.model.license.License` from a string."""
40+
try:
41+
return self.make_with_id(value,
42+
text=license_text,
43+
url=license_url,
44+
acknowledgement=license_acknowledgement)
45+
except InvalidSpdxLicenseException:
46+
pass
47+
try:
48+
return self.make_with_expression(value,
49+
acknowledgement=license_acknowledgement)
50+
except InvalidLicenseExpressionException:
51+
pass
52+
return self.make_with_name(value,
53+
text=license_text,
54+
url=license_url,
55+
acknowledgement=license_acknowledgement)
56+
57+
def make_with_expression(self, expression: str, *,
58+
acknowledgement: Optional['LicenseAcknowledgement'] = None
59+
) -> LicenseExpression:
60+
"""Make a :class:`cyclonedx.model.license.LicenseExpression` with a compound expression.
61+
62+
Utilizes :func:`cyclonedx.spdx.is_expression`.
63+
64+
:raises InvalidLicenseExpressionException: if param `value` is not known/supported license expression
65+
"""
66+
if is_spdx_expression(expression):
67+
return LicenseExpression(expression, acknowledgement=acknowledgement)
68+
raise InvalidLicenseExpressionException(expression)
69+
70+
def make_with_id(self, spdx_id: str, *,
71+
text: Optional['AttachedText'] = None,
72+
url: Optional['XsUri'] = None,
73+
acknowledgement: Optional['LicenseAcknowledgement'] = None
74+
) -> DisjunctiveLicense:
75+
"""Make a :class:`cyclonedx.model.license.DisjunctiveLicense` from an SPDX-ID.
76+
77+
:raises InvalidSpdxLicenseException: if param `spdx_id` was not known/supported SPDX-ID
78+
"""
79+
spdx_license_id = spdx_fixup(spdx_id)
80+
if spdx_license_id is None:
81+
raise InvalidSpdxLicenseException(spdx_id)
82+
return DisjunctiveLicense(id=spdx_license_id, text=text, url=url, acknowledgement=acknowledgement)
83+
84+
def make_with_name(self, name: str, *,
85+
text: Optional['AttachedText'] = None,
86+
url: Optional['XsUri'] = None,
87+
acknowledgement: Optional['LicenseAcknowledgement'] = None
88+
) -> DisjunctiveLicense:
89+
"""Make a :class:`cyclonedx.model.license.DisjunctiveLicense` with a name."""
90+
return DisjunctiveLicense(name=name, text=text, url=url, acknowledgement=acknowledgement)

cyclonedx/contrib/this.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
2+
# This file is part of CycloneDX Python Library
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
# SPDX-License-Identifier: Apache-2.0
17+
# Copyright (c) OWASP Foundation. All Rights Reserved.
18+
19+
"""Representation of this very python library."""
20+
21+
__all__ = ['this_component', 'this_tool', ]
22+
23+
from .. import __version__ as __ThisVersion # noqa: N812
24+
from ..model import ExternalReference, ExternalReferenceType, XsUri
25+
from ..model.component import Component, ComponentType
26+
from ..model.license import DisjunctiveLicense, LicenseAcknowledgement
27+
from ..model.tool import Tool
28+
29+
# !!! keep this file in sync with `pyproject.toml`
30+
31+
32+
def this_component() -> Component:
33+
"""Representation of this very python library as a :class:`Component`."""
34+
return Component(
35+
type=ComponentType.LIBRARY,
36+
group='CycloneDX',
37+
name='cyclonedx-python-lib',
38+
version=__ThisVersion or 'UNKNOWN',
39+
description='Python library for CycloneDX',
40+
licenses=(DisjunctiveLicense(id='Apache-2.0',
41+
acknowledgement=LicenseAcknowledgement.DECLARED),),
42+
external_references=(
43+
# let's assume this is not a fork
44+
ExternalReference(
45+
type=ExternalReferenceType.WEBSITE,
46+
url=XsUri('https://github.com/CycloneDX/cyclonedx-python-lib/#readme')
47+
),
48+
ExternalReference(
49+
type=ExternalReferenceType.DOCUMENTATION,
50+
url=XsUri('https://cyclonedx-python-library.readthedocs.io/')
51+
),
52+
ExternalReference(
53+
type=ExternalReferenceType.VCS,
54+
url=XsUri('https://github.com/CycloneDX/cyclonedx-python-lib')
55+
),
56+
ExternalReference(
57+
type=ExternalReferenceType.BUILD_SYSTEM,
58+
url=XsUri('https://github.com/CycloneDX/cyclonedx-python-lib/actions')
59+
),
60+
ExternalReference(
61+
type=ExternalReferenceType.ISSUE_TRACKER,
62+
url=XsUri('https://github.com/CycloneDX/cyclonedx-python-lib/issues')
63+
),
64+
ExternalReference(
65+
type=ExternalReferenceType.LICENSE,
66+
url=XsUri('https://github.com/CycloneDX/cyclonedx-python-lib/blob/main/LICENSE')
67+
),
68+
ExternalReference(
69+
type=ExternalReferenceType.RELEASE_NOTES,
70+
url=XsUri('https://github.com/CycloneDX/cyclonedx-python-lib/blob/main/CHANGELOG.md')
71+
),
72+
# we cannot assert where the lib was fetched from, but we can give a hint
73+
ExternalReference(
74+
type=ExternalReferenceType.DISTRIBUTION,
75+
url=XsUri('https://pypi.org/project/cyclonedx-python-lib/')
76+
),
77+
),
78+
# to be extended...
79+
)
80+
81+
82+
def this_tool() -> Tool:
83+
"""Representation of this very python library as a :class:`Tool`."""
84+
return Tool.from_component(this_component())

cyclonedx/factory/license.py

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

18-
from typing import TYPE_CHECKING, Optional
18+
__all__ = []
1919

20-
from ..exception.factory import InvalidLicenseExpressionException, InvalidSpdxLicenseException
21-
from ..model.license import DisjunctiveLicense, LicenseExpression
22-
from ..spdx import fixup_id as spdx_fixup, is_expression as is_spdx_expression
20+
from ..contrib.license import LicenseFactory as _LicenseFactory
2321

24-
if TYPE_CHECKING: # pragma: no cover
25-
from ..model import AttachedText, XsUri
26-
from ..model.license import License, LicenseAcknowledgement
22+
# region deprecated re-export
2723

24+
LicenseFactory = _LicenseFactory
25+
"""
26+
Alias of :class:`_LicenseFactory`.
2827
29-
class LicenseFactory:
30-
"""Factory for :class:`cyclonedx.model.license.License`."""
28+
This re-export location is deprecated.
29+
Use ``from ...contrib.license import LicenseFactory`` instead.
30+
The exported symbol itself is NOT deprecated - only this import path.
3131
32-
def make_from_string(self, value: str, *,
33-
license_text: Optional['AttachedText'] = None,
34-
license_url: Optional['XsUri'] = None,
35-
license_acknowledgement: Optional['LicenseAcknowledgement'] = None
36-
) -> 'License':
37-
"""Make a :class:`cyclonedx.model.license.License` from a string."""
38-
try:
39-
return self.make_with_id(value,
40-
text=license_text,
41-
url=license_url,
42-
acknowledgement=license_acknowledgement)
43-
except InvalidSpdxLicenseException:
44-
pass
45-
try:
46-
return self.make_with_expression(value,
47-
acknowledgement=license_acknowledgement)
48-
except InvalidLicenseExpressionException:
49-
pass
50-
return self.make_with_name(value,
51-
text=license_text,
52-
url=license_url,
53-
acknowledgement=license_acknowledgement)
32+
.. deprecated:: next
33+
"""
5434

55-
def make_with_expression(self, expression: str, *,
56-
acknowledgement: Optional['LicenseAcknowledgement'] = None
57-
) -> LicenseExpression:
58-
"""Make a :class:`cyclonedx.model.license.LicenseExpression` with a compound expression.
59-
60-
Utilizes :func:`cyclonedx.spdx.is_expression`.
61-
62-
:raises InvalidLicenseExpressionException: if param `value` is not known/supported license expression
63-
"""
64-
if is_spdx_expression(expression):
65-
return LicenseExpression(expression, acknowledgement=acknowledgement)
66-
raise InvalidLicenseExpressionException(expression)
67-
68-
def make_with_id(self, spdx_id: str, *,
69-
text: Optional['AttachedText'] = None,
70-
url: Optional['XsUri'] = None,
71-
acknowledgement: Optional['LicenseAcknowledgement'] = None
72-
) -> DisjunctiveLicense:
73-
"""Make a :class:`cyclonedx.model.license.DisjunctiveLicense` from an SPDX-ID.
74-
75-
:raises InvalidSpdxLicenseException: if param `spdx_id` was not known/supported SPDX-ID
76-
"""
77-
spdx_license_id = spdx_fixup(spdx_id)
78-
if spdx_license_id is None:
79-
raise InvalidSpdxLicenseException(spdx_id)
80-
return DisjunctiveLicense(id=spdx_license_id, text=text, url=url, acknowledgement=acknowledgement)
81-
82-
def make_with_name(self, name: str, *,
83-
text: Optional['AttachedText'] = None,
84-
url: Optional['XsUri'] = None,
85-
acknowledgement: Optional['LicenseAcknowledgement'] = None
86-
) -> DisjunctiveLicense:
87-
"""Make a :class:`cyclonedx.model.license.DisjunctiveLicense` with a name."""
88-
return DisjunctiveLicense(name=name, text=text, url=url, acknowledgement=acknowledgement)
35+
# endregion deprecated re-export

0 commit comments

Comments
 (0)