|
| 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) |
0 commit comments