|
13 | 13 | # SPDX-License-Identifier: Apache-2.0
|
14 | 14 | # Copyright (c) OWASP Foundation. All Rights Reserved.
|
15 | 15 |
|
16 |
| -from typing import Optional |
| 16 | +from typing import TYPE_CHECKING, Optional |
17 | 17 |
|
18 | 18 | from ..exception.factory import InvalidLicenseExpressionException, InvalidSpdxLicenseException
|
19 |
| -from ..model import AttachedText, License, LicenseChoice, XsUri |
| 19 | +from ..model.license import DisjunctiveLicense, LicenseExpression |
20 | 20 | from ..spdx import fixup_id as spdx_fixup, is_compound_expression as is_spdx_compound_expression
|
21 | 21 |
|
| 22 | +if TYPE_CHECKING: # pragma: no cover |
| 23 | + from ..model import AttachedText, XsUri |
| 24 | + from ..model.license import License |
22 | 25 |
|
23 |
| -class LicenseFactory: |
24 |
| - """Factory for :class:`cyclonedx.model.License`.""" |
25 |
| - |
26 |
| - def make_from_string(self, name_or_spdx: str, *, |
27 |
| - license_text: Optional[AttachedText] = None, |
28 |
| - license_url: Optional[XsUri] = None) -> License: |
29 |
| - """Make a :class:`cyclonedx.model.License` from a string.""" |
30 |
| - try: |
31 |
| - return self.make_with_id(name_or_spdx, text=license_text, url=license_url) |
32 |
| - except InvalidSpdxLicenseException: |
33 |
| - return self.make_with_name(name_or_spdx, text=license_text, url=license_url) |
34 |
| - |
35 |
| - def make_with_id(self, spdx_id: str, *, text: Optional[AttachedText] = None, |
36 |
| - url: Optional[XsUri] = None) -> License: |
37 |
| - """Make a :class:`cyclonedx.model.License` from an SPDX-ID. |
38 |
| -
|
39 |
| - :raises InvalidSpdxLicenseException: if `spdx_id` was not known/supported SPDX-ID |
40 |
| - """ |
41 |
| - spdx_license_id = spdx_fixup(spdx_id) |
42 |
| - if spdx_license_id is None: |
43 |
| - raise InvalidSpdxLicenseException(spdx_id) |
44 |
| - return License(id=spdx_license_id, text=text, url=url) |
45 |
| - |
46 |
| - def make_with_name(self, name: str, *, text: Optional[AttachedText] = None, url: Optional[XsUri] = None) -> License: |
47 |
| - """Make a :class:`cyclonedx.model.License` with a name.""" |
48 |
| - return License(name=name, text=text, url=url) |
49 | 26 |
|
| 27 | +class LicenseFactory: |
| 28 | + """Factory for :class:`cyclonedx.model.license.License`.""" |
50 | 29 |
|
51 |
| -class LicenseChoiceFactory: |
52 |
| - """Factory for :class:`cyclonedx.model.LicenseChoice`.""" |
53 |
| - |
54 |
| - def __init__(self, *, license_factory: LicenseFactory) -> None: |
55 |
| - self.license_factory = license_factory |
56 |
| - |
57 |
| - def make_from_string(self, expression_or_name_or_spdx: str) -> LicenseChoice: |
58 |
| - """Make a :class:`cyclonedx.model.LicenseChoice` from a string. |
59 |
| -
|
60 |
| - Priority: SPDX license ID, SPDX license expression, named license |
61 |
| - """ |
| 30 | + def make_from_string(self, value: str, *, |
| 31 | + license_text: Optional['AttachedText'] = None, |
| 32 | + license_url: Optional['XsUri'] = None) -> 'License': |
| 33 | + """Make a :class:`cyclonedx.model.license.License` from a string.""" |
62 | 34 | try:
|
63 |
| - return LicenseChoice(license=self.license_factory.make_with_id(expression_or_name_or_spdx)) |
| 35 | + return self.make_with_id(value, text=license_text, url=license_url) |
64 | 36 | except InvalidSpdxLicenseException:
|
65 | 37 | pass
|
66 | 38 | try:
|
67 |
| - return self.make_with_compound_expression(expression_or_name_or_spdx) |
| 39 | + return self.make_with_expression(value) |
68 | 40 | except InvalidLicenseExpressionException:
|
69 | 41 | pass
|
70 |
| - return LicenseChoice(license=self.license_factory.make_with_name(expression_or_name_or_spdx)) |
| 42 | + return self.make_with_name(value, text=license_text, url=license_url) |
71 | 43 |
|
72 |
| - def make_with_compound_expression(self, compound_expression: str) -> LicenseChoice: |
73 |
| - """Make a :class:`cyclonedx.model.LicenseChoice` with a compound expression. |
| 44 | + def make_with_expression(self, expression: str) -> LicenseExpression: |
| 45 | + """Make a :class:`cyclonedx.model.license.LicenseExpression` with a compound expression. |
74 | 46 |
|
75 | 47 | Utilizes :func:`cyclonedx.spdx.is_compound_expression`.
|
76 | 48 |
|
77 |
| - :raises InvalidLicenseExpressionException: if `expression` is not known/supported license expression |
| 49 | + :raises InvalidLicenseExpressionException: if param `value` is not known/supported license expression |
78 | 50 | """
|
79 |
| - if is_spdx_compound_expression(compound_expression): |
80 |
| - return LicenseChoice(expression=compound_expression) |
81 |
| - raise InvalidLicenseExpressionException(compound_expression) |
| 51 | + if is_spdx_compound_expression(expression): |
| 52 | + return LicenseExpression(expression) |
| 53 | + raise InvalidLicenseExpressionException(expression) |
| 54 | + |
| 55 | + def make_with_id(self, spdx_id: str, *, |
| 56 | + text: Optional['AttachedText'] = None, |
| 57 | + url: Optional['XsUri'] = None) -> DisjunctiveLicense: |
| 58 | + """Make a :class:`cyclonedx.model.license.DisjunctiveLicense` from an SPDX-ID. |
| 59 | +
|
| 60 | + :raises InvalidSpdxLicenseException: if param `spdx_id` was not known/supported SPDX-ID |
| 61 | + """ |
| 62 | + spdx_license_id = spdx_fixup(spdx_id) |
| 63 | + if spdx_license_id is None: |
| 64 | + raise InvalidSpdxLicenseException(spdx_id) |
| 65 | + return DisjunctiveLicense(id=spdx_license_id, text=text, url=url) |
82 | 66 |
|
83 |
| - def make_with_license(self, name_or_spdx: str, *, |
84 |
| - license_text: Optional[AttachedText] = None, |
85 |
| - license_url: Optional[XsUri] = None) -> LicenseChoice: |
86 |
| - """Make a :class:`cyclonedx.model.LicenseChoice` with a license (name or SPDX-ID).""" |
87 |
| - return LicenseChoice(license=self.license_factory.make_from_string( |
88 |
| - name_or_spdx, license_text=license_text, license_url=license_url)) |
| 67 | + def make_with_name(self, name: str, *, |
| 68 | + text: Optional['AttachedText'] = None, |
| 69 | + url: Optional['XsUri'] = None) -> DisjunctiveLicense: |
| 70 | + """Make a :class:`cyclonedx.model.license.DisjunctiveLicense` with a name.""" |
| 71 | + return DisjunctiveLicense(name=name, text=text, url=url) |
0 commit comments