MCPcopy Create free account
hub / github.com/aboutcode-org/scancode-toolkit / get_licenses_by_spdx_key

Function get_licenses_by_spdx_key

src/licensedcode/cache.py:313–378  ·  view source on GitHub ↗

Return a mapping of {SPDX license id: License} where license is a License object loaded from a `licenses` list of License or the standard license db if not provided. Optionally include deprecated if ``include_deprecated`` is True. Optionally make the keys lowercase if ``lowerc

(
    licenses=None,
    include_deprecated=False,
    lowercase_keys=True,
    include_other_spdx_license_keys=False,
)

Source from the content-addressed store, hash-verified

311
312
313def get_licenses_by_spdx_key(
314 licenses=None,
315 include_deprecated=False,
316 lowercase_keys=True,
317 include_other_spdx_license_keys=False,
318):
319 """
320 Return a mapping of {SPDX license id: License} where license is a License
321 object loaded from a `licenses` list of License or the standard
322 license db if not provided.
323
324 Optionally include deprecated if ``include_deprecated`` is True.
325
326 Optionally make the keys lowercase if ``lowercase_keys`` is True.
327
328 Optionally include the license "other_spdx_license_keys" if present and
329 ``include_other_spdx_license_keys`` is True.
330 """
331 from licensedcode.models import load_licenses
332
333 if not licenses:
334 licenses = load_licenses().values()
335
336 licenses_by_spdx_key = {}
337
338 for lic in licenses:
339 if not (lic.spdx_license_key or lic.other_spdx_license_keys):
340 continue
341
342 if lic.spdx_license_key:
343 slk = lic.spdx_license_key
344 if lowercase_keys:
345 slk = slk.lower()
346 existing = licenses_by_spdx_key.get(slk)
347 if existing and not lic.is_deprecated:
348 # temp hack for wharty ICU key!!
349 if slk not in ('icu', 'ICU',):
350 raise ValueError(
351 f'Duplicated SPDX license key: {slk!r} defined in '
352 f'{lic.key!r} and {existing!r}'
353 )
354
355 if (
356 not lic.is_deprecated
357 or (lic.is_deprecated and include_deprecated)
358 ):
359 licenses_by_spdx_key[slk] = lic
360
361 if include_other_spdx_license_keys:
362 for other_spdx in lic.other_spdx_license_keys:
363 if not other_spdx or not other_spdx.strip():
364 continue
365 slk = other_spdx
366 if lowercase_keys:
367 slk = slk.lower()
368
369 existing = licenses_by_spdx_key.get(slk)
370 if existing:

Callers 3

__init__Method · 0.90
synchronize_licensesFunction · 0.90
build_spdx_symbolsFunction · 0.85

Calls 3

load_licensesFunction · 0.90
getMethod · 0.65
valuesMethod · 0.45

Tested by

no test coverage detected