Skip to content

Commit 7ca2455

Browse files
authored
feat: license factory set acknowledgement (#593)
add a parameter to `LicenseFactory.make_*()` methods, to set the `LicenseAcknowledgement`. Signed-off-by: Jan Kowalleck <[email protected]>
1 parent beb2c21 commit 7ca2455

File tree

3 files changed

+53
-24
lines changed

3 files changed

+53
-24
lines changed

cyclonedx/factory/license.py

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,51 +21,66 @@
2121

2222
if TYPE_CHECKING: # pragma: no cover
2323
from ..model import AttachedText, XsUri
24-
from ..model.license import License
24+
from ..model.license import License, LicenseAcknowledgement
2525

2626

2727
class LicenseFactory:
2828
"""Factory for :class:`cyclonedx.model.license.License`."""
2929

3030
def make_from_string(self, value: str, *,
3131
license_text: Optional['AttachedText'] = None,
32-
license_url: Optional['XsUri'] = None) -> 'License':
32+
license_url: Optional['XsUri'] = None,
33+
license_acknowledgement: Optional['LicenseAcknowledgement'] = None
34+
) -> 'License':
3335
"""Make a :class:`cyclonedx.model.license.License` from a string."""
3436
try:
35-
return self.make_with_id(value, text=license_text, url=license_url)
37+
return self.make_with_id(value,
38+
text=license_text,
39+
url=license_url,
40+
acknowledgement=license_acknowledgement)
3641
except InvalidSpdxLicenseException:
3742
pass
3843
try:
39-
return self.make_with_expression(value)
44+
return self.make_with_expression(value,
45+
acknowledgement=license_acknowledgement)
4046
except InvalidLicenseExpressionException:
4147
pass
42-
return self.make_with_name(value, text=license_text, url=license_url)
48+
return self.make_with_name(value,
49+
text=license_text,
50+
url=license_url,
51+
acknowledgement=license_acknowledgement)
4352

44-
def make_with_expression(self, expression: str) -> LicenseExpression:
53+
def make_with_expression(self, expression: str, *,
54+
acknowledgement: Optional['LicenseAcknowledgement'] = None
55+
) -> LicenseExpression:
4556
"""Make a :class:`cyclonedx.model.license.LicenseExpression` with a compound expression.
4657
4758
Utilizes :func:`cyclonedx.spdx.is_compound_expression`.
4859
4960
:raises InvalidLicenseExpressionException: if param `value` is not known/supported license expression
5061
"""
5162
if is_spdx_compound_expression(expression):
52-
return LicenseExpression(expression)
63+
return LicenseExpression(expression, acknowledgement=acknowledgement)
5364
raise InvalidLicenseExpressionException(expression)
5465

5566
def make_with_id(self, spdx_id: str, *,
5667
text: Optional['AttachedText'] = None,
57-
url: Optional['XsUri'] = None) -> DisjunctiveLicense:
68+
url: Optional['XsUri'] = None,
69+
acknowledgement: Optional['LicenseAcknowledgement'] = None
70+
) -> DisjunctiveLicense:
5871
"""Make a :class:`cyclonedx.model.license.DisjunctiveLicense` from an SPDX-ID.
5972
6073
:raises InvalidSpdxLicenseException: if param `spdx_id` was not known/supported SPDX-ID
6174
"""
6275
spdx_license_id = spdx_fixup(spdx_id)
6376
if spdx_license_id is None:
6477
raise InvalidSpdxLicenseException(spdx_id)
65-
return DisjunctiveLicense(id=spdx_license_id, text=text, url=url)
78+
return DisjunctiveLicense(id=spdx_license_id, text=text, url=url, acknowledgement=acknowledgement)
6679

6780
def make_with_name(self, name: str, *,
6881
text: Optional['AttachedText'] = None,
69-
url: Optional['XsUri'] = None) -> DisjunctiveLicense:
82+
url: Optional['XsUri'] = None,
83+
acknowledgement: Optional['LicenseAcknowledgement'] = None
84+
) -> DisjunctiveLicense:
7085
"""Make a :class:`cyclonedx.model.license.DisjunctiveLicense` with a name."""
71-
return DisjunctiveLicense(name=name, text=text, url=url)
86+
return DisjunctiveLicense(name=name, text=text, url=url, acknowledgement=acknowledgement)

cyclonedx/model/license.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ class LicenseExpression:
250250

251251
def __init__(
252252
self, value: str,
253+
# *, # all optional args are intended to be keyword-args
253254
acknowledgement: Optional[LicenseAcknowledgement] = None
254255
) -> None:
255256
self._value = value

tests/test_factory_license.py

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,49 +21,60 @@
2121
from cyclonedx.exception.factory import InvalidLicenseExpressionException, InvalidSpdxLicenseException
2222
from cyclonedx.factory.license import LicenseFactory
2323
from cyclonedx.model import AttachedText, XsUri
24-
from cyclonedx.model.license import DisjunctiveLicense, LicenseExpression
24+
from cyclonedx.model.license import DisjunctiveLicense, LicenseAcknowledgement, LicenseExpression
2525

2626

2727
class TestFactoryLicense(unittest.TestCase):
2828

2929
def test_make_from_string_with_id(self) -> None:
3030
text = unittest.mock.NonCallableMock(spec=AttachedText)
3131
url = unittest.mock.NonCallableMock(spec=XsUri)
32-
expected = DisjunctiveLicense(id='bar', text=text, url=url)
32+
acknowledgement = unittest.mock.NonCallableMock(spec=LicenseAcknowledgement)
33+
expected = DisjunctiveLicense(id='bar', text=text, url=url, acknowledgement=acknowledgement)
3334

3435
with unittest.mock.patch('cyclonedx.factory.license.spdx_fixup', return_value='bar'), \
3536
unittest.mock.patch('cyclonedx.factory.license.is_spdx_compound_expression', return_value=True):
36-
actual = LicenseFactory().make_from_string('foo', license_text=text, license_url=url)
37+
actual = LicenseFactory().make_from_string('foo',
38+
license_text=text,
39+
license_url=url,
40+
license_acknowledgement=acknowledgement)
3741

3842
self.assertEqual(expected, actual)
3943

4044
def test_make_from_string_with_name(self) -> None:
4145
text = unittest.mock.NonCallableMock(spec=AttachedText)
4246
url = unittest.mock.NonCallableMock(spec=XsUri)
43-
expected = DisjunctiveLicense(name='foo', text=text, url=url)
47+
acknowledgement = unittest.mock.NonCallableMock(spec=LicenseAcknowledgement)
48+
expected = DisjunctiveLicense(name='foo', text=text, url=url, acknowledgement=acknowledgement)
4449

4550
with unittest.mock.patch('cyclonedx.factory.license.spdx_fixup', return_value=None), \
4651
unittest.mock.patch('cyclonedx.factory.license.is_spdx_compound_expression', return_value=False):
47-
actual = LicenseFactory().make_from_string('foo', license_text=text, license_url=url)
52+
actual = LicenseFactory().make_from_string('foo',
53+
license_text=text,
54+
license_url=url,
55+
license_acknowledgement=acknowledgement)
4856

4957
self.assertEqual(expected, actual)
5058

5159
def test_make_from_string_with_expression(self) -> None:
52-
expected = LicenseExpression('foo')
60+
acknowledgement = unittest.mock.NonCallableMock(spec=LicenseAcknowledgement)
61+
expected = LicenseExpression('foo', acknowledgement=acknowledgement)
5362

5463
with unittest.mock.patch('cyclonedx.factory.license.spdx_fixup', return_value=None), \
5564
unittest.mock.patch('cyclonedx.factory.license.is_spdx_compound_expression', return_value=True):
56-
actual = LicenseFactory().make_from_string('foo')
65+
actual = LicenseFactory().make_from_string('foo',
66+
license_acknowledgement=acknowledgement)
5767

5868
self.assertEqual(expected, actual)
5969

6070
def test_make_with_id(self) -> None:
6171
text = unittest.mock.NonCallableMock(spec=AttachedText)
6272
url = unittest.mock.NonCallableMock(spec=XsUri)
63-
expected = DisjunctiveLicense(id='bar', text=text, url=url)
73+
acknowledgement = unittest.mock.NonCallableMock(spec=LicenseAcknowledgement)
74+
expected = DisjunctiveLicense(id='bar', text=text, url=url, acknowledgement=acknowledgement)
6475

6576
with unittest.mock.patch('cyclonedx.factory.license.spdx_fixup', return_value='bar'):
66-
actual = LicenseFactory().make_with_id(spdx_id='foo', text=text, url=url)
77+
actual = LicenseFactory().make_with_id(spdx_id='foo', text=text, url=url, acknowledgement=acknowledgement)
6778

6879
self.assertEqual(expected, actual)
6980

@@ -75,14 +86,16 @@ def test_make_with_id_raises(self) -> None:
7586
def test_make_with_name(self) -> None:
7687
text = unittest.mock.NonCallableMock(spec=AttachedText)
7788
url = unittest.mock.NonCallableMock(spec=XsUri)
78-
expected = DisjunctiveLicense(name='foo', text=text, url=url)
79-
actual = LicenseFactory().make_with_name(name='foo', text=text, url=url)
89+
acknowledgement = unittest.mock.NonCallableMock(spec=LicenseAcknowledgement)
90+
expected = DisjunctiveLicense(name='foo', text=text, url=url, acknowledgement=acknowledgement)
91+
actual = LicenseFactory().make_with_name(name='foo', text=text, url=url, acknowledgement=acknowledgement)
8092
self.assertEqual(expected, actual)
8193

8294
def test_make_with_expression(self) -> None:
83-
expected = LicenseExpression('foo')
95+
acknowledgement = unittest.mock.NonCallableMock(spec=LicenseAcknowledgement)
96+
expected = LicenseExpression('foo', acknowledgement=acknowledgement)
8497
with unittest.mock.patch('cyclonedx.factory.license.is_spdx_compound_expression', return_value=True):
85-
actual = LicenseFactory().make_with_expression(expression='foo')
98+
actual = LicenseFactory().make_with_expression(expression='foo', acknowledgement=acknowledgement)
8699
self.assertEqual(expected, actual)
87100

88101
def test_make_with_expression_raises(self) -> None:

0 commit comments

Comments
 (0)