Skip to content

Commit

Permalink
Fixed #136 - It's passing a list of about object ONLY to the template…
Browse files Browse the repository at this point in the history
… instead of couple of lists and dictionary

Remove the following functions:
def get_license_text_file_name(self):
def get_license_text(self):
def get_about_resource_path(self):
  • Loading branch information
chinyeungli committed Aug 14, 2014
1 parent f77596c commit b28a1fe
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 131 deletions.
100 changes: 17 additions & 83 deletions about_code_tool/about.py
Expand Up @@ -1002,33 +1002,12 @@ def notice_text(self):
pass
return ''


def get_license_text_file_name(self):
"""
Return the license_text_file name if the license_text_file field
exists
"""
return self.parsed.get('license_text_file', '')

def get_license_text(self):
"""
Return the license_text if the license_text field exists.
"""
return self.parsed.get('license_text', '')

def get_about_name(self):
"""
Return the about object's name.
"""
return self.parsed.get('name', '')

def get_about_resource_path(self):
"""
Return the about object's name
"""
return self.parsed.get('about_resource_path', '')


class AboutCollector(object):
"""
A collection of AboutFile instances.
Expand Down Expand Up @@ -1174,13 +1153,8 @@ def generate_attribution(self, template_path=None, limit_to=None):
print('Template: %(template_name)s not found' % locals())
return

# We only need the fields names and values to render the template
validated_fields = []
notice_text = []
license_key = []
license_text_list = []
license_dict = {}
common_license_dict = {}
about_object_fields = []
about_content_dict = {}
not_exist_components = list(limit_to)

# FIXME: this loop and conditional is too complex.
Expand All @@ -1198,59 +1172,23 @@ def generate_attribution(self, template_path=None, limit_to=None):
continue

if not limit_to or about_relative_path in limit_to:
validated_fields.append(about_object.validated_fields)
notice_text.append(about_object.notice_text())
license_text_file = about_object.get_license_text_file_name()
about_resource_path = about_object.get_about_resource_path()

if not about_resource_path:
msg = ('About File: %s - The required field '
'about_resource_path was not found. '
'License generation is skipped.'
% about_object.location)
err = Error(GENATTRIB, 'about_resource',
about_object.location, msg)
self.genattrib_errors.append(err)

if license_text_file:
# Use the about_file_path as the key
# Check if the key already existed in the dictionary
# This shouldn't be reached as the 'about_resource_path'
# should be unique.
if about_resource_path in license_dict:
# Raise error if key name are the same but the content
# are different
license_text_check = license_dict[about_resource_path]

if (unicode(license_text_check)
!= unicode(about_object.license_text())):
msg = ('License Name: %s - Same license name '
'with different content. License '
'generation is skipped.'
% license_text_file)
err = Error(GENATTRIB, 'license_text',
license_text_file, msg)
self.genattrib_errors.append(err)
else:
lft = unicode(about_object.license_text(),
about_content_dict = about_object.validated_fields
# Add information in the dictionary where it does not
# present in the ABOUT file
about_content_dict['license_text_content'] = unicode(about_object.license_text(),
errors='replace')
license_dict[about_resource_path] = lft

if license_text_file in COMMON_LICENSES:
ltf = unicode(about_object.license_text(),
errors='replace')
common_license_dict[license_text_file] = ltf
elif about_object.get_license_text():
# We do not need to do anything here as the license_text
# will be captured directly in the about object.
pass
else:
about_content_dict['notice_text_content'] = about_object.notice_text()

# Raise error if no license_text is found
if not about_content_dict['license_text_content']:
msg = ('No license_text is found. '
'License generation is skipped.')
err = Error(GENATTRIB, 'name',
about_object.get_about_name(), msg)
self.genattrib_errors.append(err)

about_object_fields.append(about_content_dict)

if not_exist_components:
for component in not_exist_components:
afp = self.user_provided_path + component.replace('//', '/')
Expand All @@ -1260,17 +1198,13 @@ def generate_attribution(self, template_path=None, limit_to=None):
err = Error(GENATTRIB, 'about_file', component, msg)
self.genattrib_errors.append(err)

# We want to display common_licenses in alphabetical order
# TODO: Handle the grouping and ordering later
"""# We want to display common_licenses in alphabetical order
for key in sorted(common_license_dict.keys()):
license_key.append(key)
license_text_list.append(common_license_dict[key])

return template.render(about_objects=validated_fields,
license_keys=license_key,
license_texts=license_text_list,
notice_texts=notice_text,
license_dicts=license_dict,
common_licenses=COMMON_LICENSES)
license_text_list.append(common_license_dict[key])"""

return template.render(about_objects=about_object_fields)

def get_genattrib_errors(self):
return self.genattrib_errors
Expand Down
36 changes: 5 additions & 31 deletions about_code_tool/templates/default.html
Expand Up @@ -56,50 +56,24 @@ <h1>OPEN SOURCE SOFTWARE INFORMATION</h1>
<h3 class="component-name">{{ about_object.name }}
{% if about_object.version %}{{ about_object.version }}{% endif %}
</h3>
{% if about_object.about_resource_path in license_dicts.keys() %}
{% if about_object.dje_license_name %}
<p>This component is licensed under {{about_object.dje_license_name }}.
{% endif %}
{% if about_object.dje_license_name %}
<p>This component is licensed under {{about_object.dje_license_name }}.
{% endif %}
{% if about_object.copyright %}
<pre>{{about_object.copyright}}</pre>
{% endif %}
{% if about_object.notice %}
<pre>{{ about_object.notice }}</pre>
{% elif about_object.notice_file %}
<pre class="component-notice">{{ notice_texts[loop.index0] }}</pre>
<pre class="component-notice">{{ about_object.notice_text_content }}</pre>
{% endif %}
{% if about_object.license_text_file %}
<!-- 'license_text_file' is not a common license if it
doesn't end with '.LICENSE'
-->
{% if not about_object.license_text_file.endswith('.LICENSE') %}
<pre>{{ license_dicts[about_object.about_resource_path]|e }}</pre>
{% elif about_object.about_resource_path in license_dicts.keys() %}
{% if about_object.license_text_file in common_licenses %}
<p>Full text of
<a class="{{ about_object.license_text_file }}" href="#component-license-{{ about_object.license_text_file }}">
{{ about_object.license_text_file }}
</a>
is available at the end of this document.</p>
{% else %}
<pre>{{ license_dicts[about_object.about_resource_path]|e }}</pre>
{% endif %}
{% endif %}
{% elif about_object.license_text %}
<p>{{about_object.license_text | e}}</p>
{% if about_object.license_text_content %}
<pre>{{about_object.license_text_content | e}}</pre>
{% endif %}
</div>
{% endfor %}
<hr>

<h3>Common Licenses Used in This Product</h3>
{% for index in range(license_keys | count) %}
{% if license_keys[index] in common_licenses %}
<h3 id="component-license-{{ license_keys[index] }}">{{ license_keys[index] }}</h3>
<pre>{{ license_texts[index]|e }}</pre>
{% endif %}
{% endfor %}
<h3><a id="End">End</a></h3>
</body>
</html>
19 changes: 2 additions & 17 deletions about_code_tool/tests/test_about.py
Expand Up @@ -532,7 +532,8 @@ def test_remove_blank_lines_and_no_colon_fields(self):
self.assertEqual(expected_warnings[i][1], w.field_value)

def test_generate_attribution(self):
expected = u'version:2.4.3about_resource:httpd-2.4.3.tar.gzname:Apache HTTP Server'
expected = u'notice_text_content:version:2.4.3about_resource:httpd-2.4.3.tar.gz'\
'name:Apache HTTP Serverlicense_text_content:'
about_collector = about.AboutCollector(join(TESTDATA_PATH, 'attrib/attrib.ABOUT'))
result = about_collector.generate_attribution(join(TESTDATA_PATH, 'attrib/test.template'))
self.assertEqual(result, expected)
Expand Down Expand Up @@ -577,20 +578,4 @@ def test_notice_text_returns_empty_string_when_ref_file_doesnt_exist(self):
notice_text = about_file.notice_text()
self.assertEqual(notice_text, expected)

def test_get_license_text_file_name(self):
expected = 'httpd.LICENSE'
about_file = about.AboutFile(join(TESTDATA_PATH, 'parser_tests/about_resource_field_present.ABOUT'))
output = about_file.get_license_text_file_name()
self.assertEquals(expected, output)

def test_get_license_text_file_name_no_value(self):
expected = ''
about_file = about.AboutFile(join(TESTDATA_PATH, 'parser_tests/about_file_empty_value_for_dje_license_license_text_file.ABOUT'))
output = about_file.get_license_text_file_name()
self.assertEquals(expected, output)

def test_get_license_text_file_name_no_key(self):
expected = ''
about_file = about.AboutFile(join(TESTDATA_PATH, 'parser_tests/about_file_no_dje_license_no_license_text_file_keys.ABOUT'))
output = about_file.get_license_text_file_name()
self.assertEquals(expected, output)

0 comments on commit b28a1fe

Please sign in to comment.