Skip to content

Commit ce1913f

Browse files
committed
Add code so that the jinja2 template can get the license short name
Signed-off-by: Chin Yeung Li <[email protected]>
1 parent b129e79 commit ce1913f

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

src/attributecode/attrib.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ def generate(abouts, template_string=None):
5050
license_key_and_context = {}
5151
sorted_license_key_and_context = {}
5252
license_text_name_and_key = {}
53+
license_key_to_license_name = {}
54+
# FIXME: This need to be simplified
5355
for about in abouts:
5456
# about.license_file.value is a OrderDict with license_text_name as
5557
# the key and the license text as the value
@@ -67,8 +69,37 @@ def generate(abouts, template_string=None):
6769
sorted_license_key_and_context = collections.OrderedDict(sorted(license_key_and_context.items()))
6870
license_text_name_and_key[license_text_name] = license_key
6971

72+
# Convert/map the key in license expression to license name
73+
if about.license_expression.value and about.license_name.value:
74+
# Split the license expression into list with license key and condition keyword
75+
lic_expression_list = about.license_expression.value.split()
76+
77+
78+
lic_name_list = about.license_name.value
79+
lic_name_expression_list = []
80+
81+
# The order of the license_name and key should be the same
82+
# The length for both list should be the same excluding the condition keyword
83+
# such as 'and' and 'or'
84+
assert len(lic_name_list) <= len(lic_expression_list)
85+
86+
# Map the licence key to license name
87+
index_for_license_name_list = 0
88+
for key in lic_expression_list:
89+
if key.lower() == 'and' or key.lower() == 'or':
90+
lic_name_expression_list.append(key)
91+
else:
92+
lic_name_expression_list.append(lic_name_list[index_for_license_name_list])
93+
license_key_to_license_name[key] = lic_name_list[index_for_license_name_list]
94+
index_for_license_name_list = index_for_license_name_list + 1
95+
# Join the license name expression into a single string
96+
lic_name_expression = ' '.join(lic_name_expression_list)
97+
98+
# Add the license name expression string into the about object
99+
about.license_name_expression = lic_name_expression
100+
70101
rendered = template.render(abouts=abouts, common_licenses=COMMON_LICENSES, license_key_and_context=sorted_license_key_and_context,
71-
license_text_name_and_key=license_text_name_and_key)
102+
license_text_name_and_key=license_text_name_and_key, license_key_to_license_name=license_key_to_license_name)
72103
except Exception, e:
73104
line = getattr(e, 'lineno', None)
74105
ln_msg = ' at line: %r' % line if line else ''

src/attributecode/gen.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,10 +239,14 @@ def generate(location, base_dir, mapping, license_notice_text_location, fetch_li
239239
gen_license_name = lic_key + u'.LICENSE'
240240
about.license_file.value[gen_license_name] = lic_context
241241
about.license_file.present = True
242+
if not about.license_name.present:
243+
about.license_name.value.append(lic_name)
242244
if not about.license_url.present:
243245
about.license_url.value.append(lic_url)
244246
if about.license_url.value:
245247
about.license_url.present = True
248+
if about.license_name.value:
249+
about.license_name.present = True
246250

247251
# Write the ABOUT file and check does the referenced file exist
248252
# This function is not purposed to throw error. However, since I've commented

src/attributecode/model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ def create_fields(self):
631631

632632
('license', ListField()),
633633
('license_expression', StringField()),
634-
('license_name', StringField()),
634+
('license_name', ListField()),
635635
('license_file', FileTextField()),
636636
('license_url', UrlField()),
637637
('copyright', StringField()),

0 commit comments

Comments
 (0)