Skip to content

Commit

Permalink
refactor: streamline constants
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
  • Loading branch information
jkowalleck committed Oct 23, 2024
1 parent f64ccaf commit a2f3b0a
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 22 deletions.
15 changes: 8 additions & 7 deletions cyclonedx_py/_internal/utils/cdx.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from cyclonedx.model.component import Component, ComponentType
from cyclonedx.model.license import DisjunctiveLicense, License, LicenseAcknowledgement, LicenseExpression

from ... import __version__ as __THIS_VERSION # noqa:N812
from ... import __version__ as _THIS_VERSION # noqa:N812


def make_bom(**kwargs: Any) -> Bom:
Expand All @@ -41,7 +41,7 @@ def make_bom(**kwargs: Any) -> Bom:
group='CycloneDX',
# package is called 'cyclonedx-bom', but the tool is called 'cyclonedx-py'
name='cyclonedx-py',
version=__THIS_VERSION,
version=_THIS_VERSION,
description='CycloneDX Software Bill of Materials (SBOM) generator for Python projects and environments',
licenses=(DisjunctiveLicense(id='Apache-2.0',
acknowledgement=LicenseAcknowledgement.DECLARED),),
Expand Down Expand Up @@ -95,7 +95,7 @@ def licenses_fixup(licenses: Iterable['License']) -> Iterable['License']:
return licenses


__known_ulr_labels: Dict[str, ExternalReferenceType] = {
_MAP_KNOWN_URL_LABELS: Dict[str, ExternalReferenceType] = {
# see https://peps.python.org/pep-0345/#project-url-multiple-use
# see https://github.com/pypi/warehouse/issues/5947#issuecomment-699660629
'bugtracker': ExternalReferenceType.ISSUE_TRACKER,
Expand All @@ -116,10 +116,11 @@ def licenses_fixup(licenses: Iterable['License']) -> Iterable['License']:
'chat': ExternalReferenceType.CHAT,
}

__re_nochar = re_compile('[^a-z]')
_NOCHAR_MATCHER = re_compile('[^a-z]')


def url_label_to_ert(value: str) -> ExternalReferenceType:
return __known_ulr_labels.get(
__re_nochar.sub('', str(value).lower()),
ExternalReferenceType.OTHER)
return _MAP_KNOWN_URL_LABELS.get(
_NOCHAR_MATCHER.sub('', str(value).lower()),
ExternalReferenceType.OTHER
)
9 changes: 5 additions & 4 deletions cyclonedx_py/_internal/utils/license_trove_classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@
All in here may have breaking change without notice.
"""


from typing import Optional

__LICENSE_TROVE_PREFIX = 'License :: '
_LICENSE_TROVE_PREFIX = 'License :: '


def is_license_trove(classifier: str) -> bool:
return classifier.startswith(__LICENSE_TROVE_PREFIX)
return classifier.startswith(_LICENSE_TROVE_PREFIX)


"""
Expand All @@ -44,7 +45,7 @@ def is_license_trove(classifier: str) -> bool:
See also: https://peps.python.org/pep-0639/#mapping-license-classifiers-to-spdx-identifiers
"""
__TO_SPDX_MAP = {
_MAP_TO_SPDX = {

# region not OSI Approved

Expand Down Expand Up @@ -168,4 +169,4 @@ def is_license_trove(classifier: str) -> bool:

def license_trove2spdx(classifier: str) -> Optional[str]:
"""return the SPDX id or expression for a given license trove classifier"""
return __TO_SPDX_MAP.get(classifier)
return _MAP_TO_SPDX.get(classifier)
16 changes: 9 additions & 7 deletions cyclonedx_py/_internal/utils/mimetypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
from os.path import splitext
from typing import Optional

_ext_mime_map = {
_MAP_EXT_MIME = {
# https://www.iana.org/assignments/media-types/media-types.xhtml
'md': 'text/markdown',
'txt': 'text/plain',
'rst': 'text/prs.fallenstein.rst',
'.md': 'text/markdown',
'.txt': 'text/plain',
'.rst': 'text/prs.fallenstein.rst',
# add more mime types. pull-requests welcome!
}

Expand All @@ -33,6 +33,8 @@ def guess_type(file_name: str) -> Optional[str]:
The stdlib `mimetypes.guess_type()` is inconsistent, as it depends heavily on type registry in the env/os.
Therefore, this polyfill exists.
"""
ext = splitext(file_name)[1][1:].lower()
return _ext_mime_map.get(ext) \
or _stdlib_guess_type(file_name)[0]
ext = splitext(file_name)[1].lower()
return _MAP_EXT_MIME.get(
ext,
_stdlib_guess_type(file_name)[0]
)
5 changes: 4 additions & 1 deletion cyclonedx_py/_internal/utils/packaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,7 @@ def normalize_packagename(name: str) -> str:
see https://packaging.python.org/en/latest/specifications/name-normalization/#name-normalization
"""
return _NORMALIZE_PN_MATCHER.sub(_NORMALIZE_PN_REPLACE, name).lower()
return _NORMALIZE_PN_MATCHER.sub(
_NORMALIZE_PN_REPLACE,
name.lower()
)
3 changes: 2 additions & 1 deletion cyclonedx_py/_internal/utils/secret.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@
def redact_auth_from_url(s: str) -> str:
# is intended to work on any string that contains an url.
return _URL_AUTH_MATCHER.sub(_URL_AUTH_REPLACE, s) \
if '@' in s else s
if '@' in s \
else s
4 changes: 2 additions & 2 deletions tests/functional/test_license_trove_classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
from cyclonedx.spdx import is_supported_id
from ddt import ddt, named_data

from cyclonedx_py._internal.utils.license_trove_classifier import __TO_SPDX_MAP as TO_SPDX_MAP
from cyclonedx_py._internal.utils.license_trove_classifier import _MAP_TO_SPDX


@ddt
class TestLicenseTroveClassifier(TestCase):

@named_data(*TO_SPDX_MAP.items())
@named_data(*_MAP_TO_SPDX.items())
def test_map_is_known_id(self, mapped: str) -> None:
self.assertTrue(is_supported_id(mapped))

0 comments on commit a2f3b0a

Please sign in to comment.