Skip to content

Commit f3d17e0

Browse files
committed
Doing surgery for attrib.py #446
1 parent 45d7cb6 commit f3d17e0

File tree

1 file changed

+67
-44
lines changed

1 file changed

+67
-44
lines changed

src/attributecode/attrib.py

Lines changed: 67 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -63,82 +63,97 @@ def generate(abouts, template=None, variables=None):
6363

6464
try:
6565
captured_license = []
66-
license_key_and_context = {}
67-
sorted_license_key_and_context = {}
68-
license_file_name_and_key = {}
69-
license_key_to_license_name = {}
70-
license_name_to_license_key = {}
66+
license_file_key_and_context = {}
67+
sorted_license_file_key_and_context = {}
68+
license_file_name_and_license_file_key = {}
69+
license_key_and_license_name = {}
70+
license_name_and_license_key = {}
71+
license_key_and_license_file_name = {}
72+
license_file_key_and_license_key = {}
7173
# FIXME: This need to be simplified
7274
for about in abouts:
7375
# about.license_file.value is a OrderDict with license_text_name as
7476
# the key and the license text as the value
7577
if about.license_file:
76-
# We want to create a dictionary which have the license short name as
78+
# We want to create a dictionary which have the license name as
7779
# the key and license text as the value
80+
# The reason we want to use license file name as the key instead of the
81+
# license key is because there is a scenario such that the input only provide
82+
# license_file but not license_key
7883
for license_text_name in about.license_file.value:
7984
if not license_text_name in captured_license:
8085
captured_license.append(license_text_name)
81-
if license_text_name.endswith('.LICENSE'):
82-
# See https://github.com/nexB/aboutcode-toolkit/issues/439
83-
# for why using split instead of strip
84-
license_key = license_text_name.rsplit('.', 1)[0]
85-
else:
86-
license_key = license_text_name
87-
license_key_and_context[license_key] = about.license_file.value[license_text_name]
88-
sorted_license_key_and_context = collections.OrderedDict(sorted(license_key_and_context.items()))
89-
license_file_name_and_key[license_text_name] = license_key
90-
86+
license_file_key = get_license_file_key(license_text_name)
87+
license_file_key_and_context[license_file_key] = about.license_file.value[license_text_name]
88+
sorted_license_file_key_and_context = collections.OrderedDict(sorted(license_file_key_and_context.items()))
89+
license_file_name_and_license_file_key[license_text_name] = license_file_key
90+
91+
lic_list = []
92+
lic_name_list = []
93+
lic_name_expression_list = []
9194
# Convert/map the key to name
92-
if (about.license_expression.value or about.license_key.value) and about.license_name.value:
93-
if about.license_expression.value:
94-
special_char, lic_list = parse_license_expression(about.license_expression.value)
95+
if about.license_name.value:
96+
if about.license_expression.value or about.license_key.value:
97+
if about.license_expression.value:
98+
special_char, lic_list = parse_license_expression(about.license_expression.value)
99+
else:
100+
lic_list = about.license_key.value
101+
special_char = []
102+
for lic in lic_list:
103+
special_char_list = detect_special_char(lic)
104+
if special_char_list:
105+
for char in special_char_list:
106+
special_char.append(char)
107+
if special_char:
108+
error = Error(CRITICAL, 'Special character(s) are not allowed in '
109+
'license_expression or license_key: %s' % special_char)
110+
return error, ''
95111
else:
96-
lic_list = about.license_key.value
97-
special_char = []
98-
for lic in lic_list:
99-
special_char_list = detect_special_char(lic)
100-
if special_char_list:
101-
for char in special_char_list:
102-
special_char.append(char)
103-
if special_char:
104-
error = Error(CRITICAL, 'Special character(s) are not allowed in '
105-
'license_expression or license_key: %s' % special_char)
106-
return error, ''
112+
# No license_key or license_expression present. We will use
113+
# the license_file_name as the license_key as needed for the
114+
# linking feature in the jinja2 template
115+
about.license_key.value = about.license_file.value.keys()
116+
lic_list = about.license_file.value.keys()
117+
107118
lic_name_list = about.license_name.value
108-
lic_name_expression_list = []
109119

110120
# The order of the license_name and key should be the same
111121
# The length for both list should be the same
112-
assert len(lic_name_list) == len(lic_list)
122+
assert len(lic_name_list) == len(lic_list)
113123

114124
# Map the license key to license name
115125
index_for_license_name_list = 0
116126
for key in lic_list:
117-
license_key_to_license_name[key] = lic_name_list[index_for_license_name_list]
118-
license_name_to_license_key[lic_name_list[index_for_license_name_list]] = key
127+
license_key_and_license_file_name[key] = about.license_file.value.keys()[index_for_license_name_list]
128+
license_key_and_license_name[key] = lic_name_list[index_for_license_name_list]
129+
license_name_and_license_key[lic_name_list[index_for_license_name_list]] = key
130+
license_file_key = license_file_name_and_license_file_key[license_key_and_license_file_name[key]]
131+
license_file_key_and_license_key[license_file_key] = key
119132
index_for_license_name_list = index_for_license_name_list + 1
120-
133+
121134
# Create a license expression with license name instead of key
122135
for segment in about.license_expression.value.split():
123-
if segment in license_key_to_license_name:
124-
lic_name_expression_list.append(license_key_to_license_name[segment])
136+
if segment in license_key_and_license_name:
137+
lic_name_expression_list.append(license_key_and_license_name[segment])
125138
else:
126139
lic_name_expression_list.append(segment)
127-
140+
128141
# Join the license name expression into a single string
129142
lic_name_expression = ' '.join(lic_name_expression_list)
130-
143+
131144
# Add the license name expression string into the about object
132-
about.license_name_expression = lic_name_expression
145+
about.license_name_expression = lic_name_expression
133146

134147
# Get the current UTC time
135148
utcnow = datetime.datetime.utcnow()
136149
rendered = template.render(
137150
abouts=abouts, common_licenses=COMMON_LICENSES,
138-
license_key_and_context=sorted_license_key_and_context,
139-
license_file_name_and_key=license_file_name_and_key,
140-
license_key_to_license_name=license_key_to_license_name,
141-
license_name_to_license_key=license_name_to_license_key,
151+
license_file_key_and_context=sorted_license_file_key_and_context,
152+
license_file_name_and_license_file_key=license_file_name_and_license_file_key,
153+
license_key_and_license_name=license_key_and_license_name,
154+
license_name_and_license_key=license_name_and_license_key,
155+
license_key_and_license_file_name=license_key_and_license_file_name,
156+
license_file_key_and_license_key=license_file_key_and_license_key,
142157
utcnow=utcnow,
143158
tkversion=__version__,
144159
variables=variables
@@ -155,6 +170,14 @@ def generate(abouts, template=None, variables=None):
155170
return error, rendered
156171

157172

173+
def get_license_file_key(license_text_name):
174+
if license_text_name.endswith('.LICENSE'):
175+
# See https://github.com/nexB/aboutcode-toolkit/issues/439
176+
# for why using split instead of strip
177+
return license_text_name.rsplit('.', 1)[0]
178+
else:
179+
return license_text_name
180+
158181
def check_template(template_string):
159182
"""
160183
Check the syntax of a template. Return an error tuple (line number,

0 commit comments

Comments
 (0)