forked from aboutcode-org/scancode-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
94 lines (77 loc) · 2.98 KB
/
utils.py
File metadata and controls
94 lines (77 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#
# Copyright (c) nexB Inc. and others. All rights reserved.
# ScanCode is a trademark of nexB Inc.
# SPDX-License-Identifier: Apache-2.0
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
# See https://github.com/nexB/scancode-toolkit for support or download.
# See https://aboutcode.org for more information about nexB OSS projects.
#
def get_resource_summary(resource, key, as_attribute=False):
"""
Return the "summary" value as mapping for the `key` summary attribute of a
resource.
This is collected either from a direct Resource.summary attribute if
`as_attribute` is True or as a Resource.extra_data summary item otherwise.
"""
if as_attribute:
summary = resource.summary
else:
summary = resource.extra_data.get('summary', {})
summary = summary or {}
return summary.get(key) or None
def set_resource_summary(resource, key, value, as_attribute=False):
"""
Set `value` as the "summary" value for the `key` summary attribute of a
resource
This is set either in a direct Resource.summary attribute if `as_attribute`
is True or as a Resource.extra_data summary item otherwise.
"""
if as_attribute:
resource.summary[key] = value
else:
summary = resource.extra_data.get('summary')
if not summary:
summary = dict([(key, value)])
resource.extra_data['summary'] = summary
summary[key] = value
def sorted_counter(counter):
"""
Return a list of ordered mapping of {value:val, count:cnt} built from a
`counter` mapping of {value: count} and sortedd by decreasing count then by
value.
"""
def by_count_value(value_count):
value, count = value_count
return -count, value or ''
summarized = [
dict([('value', value), ('count', count)])
for value, count in sorted(counter.items(), key=by_count_value)]
return summarized
def get_resource_tallies(resource, key, as_attribute=False):
"""
Return the "tallies" value as mapping for the `key` tallies attribute of a
resource.
This is collected either from a direct Resource.tallies attribute if
`as_attribute` is True or as a Resource.extra_data tallies item otherwise.
"""
if as_attribute:
tallies = resource.tallies
else:
tallies = resource.extra_data.get('tallies', {})
tallies = tallies or {}
return tallies.get(key) or None
def set_resource_tallies(resource, key, value, as_attribute=False):
"""
Set `value` as the "tallies" value for the `key` tallies attribute of a
resource
This is set either in a direct Resource.tallies attribute if `as_attribute`
is True or as a Resource.extra_data tallies item otherwise.
"""
if as_attribute:
resource.tallies[key] = value
else:
tallies = resource.extra_data.get('tallies')
if not tallies:
tallies = dict([(key, value)])
resource.extra_data['tallies'] = tallies
tallies[key] = value