Skip to content

Commit d0b5997

Browse files
authored
refactor: streamline constants (#820)
Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
1 parent f64ccaf commit d0b5997

File tree

6 files changed

+30
-22
lines changed

6 files changed

+30
-22
lines changed

cyclonedx_py/_internal/utils/cdx.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from cyclonedx.model.component import Component, ComponentType
3030
from cyclonedx.model.license import DisjunctiveLicense, License, LicenseAcknowledgement, LicenseExpression
3131

32-
from ... import __version__ as __THIS_VERSION # noqa:N812
32+
from ... import __version__ as _THIS_VERSION # noqa:N812
3333

3434

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

9797

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

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

121121

122122
def url_label_to_ert(value: str) -> ExternalReferenceType:
123-
return __known_ulr_labels.get(
124-
__re_nochar.sub('', str(value).lower()),
125-
ExternalReferenceType.OTHER)
123+
return _MAP_KNOWN_URL_LABELS.get(
124+
_NOCHAR_MATCHER.sub('', str(value).lower()),
125+
ExternalReferenceType.OTHER
126+
)

cyclonedx_py/_internal/utils/license_trove_classifier.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@
2121
All in here may have breaking change without notice.
2222
"""
2323

24+
2425
from typing import Optional
2526

26-
__LICENSE_TROVE_PREFIX = 'License :: '
27+
_LICENSE_TROVE_PREFIX = 'License :: '
2728

2829

2930
def is_license_trove(classifier: str) -> bool:
30-
return classifier.startswith(__LICENSE_TROVE_PREFIX)
31+
return classifier.startswith(_LICENSE_TROVE_PREFIX)
3132

3233

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

4950
# region not OSI Approved
5051

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

169170
def license_trove2spdx(classifier: str) -> Optional[str]:
170171
"""return the SPDX id or expression for a given license trove classifier"""
171-
return __TO_SPDX_MAP.get(classifier)
172+
return _MAP_TO_SPDX.get(classifier)

cyclonedx_py/_internal/utils/mimetypes.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
from os.path import splitext
2020
from typing import Optional
2121

22-
_ext_mime_map = {
22+
_MAP_EXT_MIME = {
2323
# https://www.iana.org/assignments/media-types/media-types.xhtml
24-
'md': 'text/markdown',
25-
'txt': 'text/plain',
26-
'rst': 'text/prs.fallenstein.rst',
24+
'.md': 'text/markdown',
25+
'.txt': 'text/plain',
26+
'.rst': 'text/prs.fallenstein.rst',
2727
# add more mime types. pull-requests welcome!
2828
}
2929

@@ -33,6 +33,8 @@ def guess_type(file_name: str) -> Optional[str]:
3333
The stdlib `mimetypes.guess_type()` is inconsistent, as it depends heavily on type registry in the env/os.
3434
Therefore, this polyfill exists.
3535
"""
36-
ext = splitext(file_name)[1][1:].lower()
37-
return _ext_mime_map.get(ext) \
38-
or _stdlib_guess_type(file_name)[0]
36+
ext = splitext(file_name)[1].lower()
37+
return _MAP_EXT_MIME.get(
38+
ext,
39+
_stdlib_guess_type(file_name)[0]
40+
)

cyclonedx_py/_internal/utils/packaging.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,7 @@ def normalize_packagename(name: str) -> str:
9797
9898
see https://packaging.python.org/en/latest/specifications/name-normalization/#name-normalization
9999
"""
100-
return _NORMALIZE_PN_MATCHER.sub(_NORMALIZE_PN_REPLACE, name).lower()
100+
return _NORMALIZE_PN_MATCHER.sub(
101+
_NORMALIZE_PN_REPLACE,
102+
name.lower()
103+
)

cyclonedx_py/_internal/utils/secret.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@
2424
def redact_auth_from_url(s: str) -> str:
2525
# is intended to work on any string that contains an url.
2626
return _URL_AUTH_MATCHER.sub(_URL_AUTH_REPLACE, s) \
27-
if '@' in s else s
27+
if '@' in s \
28+
else s

tests/functional/test_license_trove_classifier.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@
2121
from cyclonedx.spdx import is_supported_id
2222
from ddt import ddt, named_data
2323

24-
from cyclonedx_py._internal.utils.license_trove_classifier import __TO_SPDX_MAP as TO_SPDX_MAP
24+
from cyclonedx_py._internal.utils.license_trove_classifier import _MAP_TO_SPDX
2525

2626

2727
@ddt
2828
class TestLicenseTroveClassifier(TestCase):
2929

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

0 commit comments

Comments
 (0)