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

Function tally_codebase_by_facet

src/summarycode/tallies.py:392–441  ·  view source on GitHub ↗

Summarize codebase by facte.

(codebase, **kwargs)

Source from the content-addressed store, hash-verified

390
391
392def tally_codebase_by_facet(codebase, **kwargs):
393 """
394 Summarize codebase by facte.
395 """
396 from summarycode import facet as facet_module
397
398 talliable = codebase.attributes.tallies.keys()
399 if TRACE:
400 logger_debug('tally_codebase_by_facet for attributes:', talliable)
401
402 # create one group of by-facet values lists for each summarized attribute
403 talliable_values_by_key_by_facet = dict([
404 (facet, dict([(key, []) for key in talliable]))
405 for facet in facet_module.FACETS
406 ])
407
408 for resource in codebase.walk(topdown=True):
409 if not resource.is_file:
410 continue
411
412 for facet in resource.facets:
413 # note: this will fail loudly if the facet is not a known one
414 values_by_attribute = talliable_values_by_key_by_facet[facet]
415 for key, values in values_by_attribute.items():
416 # note we assume things are stored as extra-data, not as direct
417 # Resource attributes
418 res_tallies = get_resource_tallies(resource, key=key, as_attribute=False) or []
419 for tally in res_tallies:
420 # each tally is a mapping with value/count: we transform back to discrete values
421 tally_value = tally.get('value')
422 if tally_value:
423 values.extend([tally_value] * tally['count'])
424
425 final_tallies = []
426 for facet, talliable_values_by_key in talliable_values_by_key_by_facet.items():
427 tally_counters = (
428 (key, tally_values(values, key))
429 for key, values in talliable_values_by_key.items()
430 )
431
432 sorted_tallies = dict(
433 [(key, sorted_counter(counter)) for key, counter in tally_counters])
434
435 facet_tally = dict(facet=facet)
436 facet_tally['tallies'] = sorted_tallies
437 final_tallies.append(facet_tally)
438
439 codebase.attributes.tallies_by_facet.extend(final_tallies)
440
441 if TRACE: logger_debug('codebase tallies_by_facet:', final_tallies)
442
443
444def add_files(packages, resource):

Callers 1

process_codebaseMethod · 0.85

Calls 8

get_resource_talliesFunction · 0.90
sorted_counterFunction · 0.90
tally_valuesFunction · 0.85
logger_debugFunction · 0.70
getMethod · 0.65
keysMethod · 0.45
walkMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected