Skip to content

Commit

Permalink
Update code to make the test green.
Browse files Browse the repository at this point in the history
The updated code need refactoring as it contains duplicated code.
  • Loading branch information
chinyeungli committed Oct 22, 2014
1 parent 654edce commit 60703aa
Showing 1 changed file with 85 additions and 42 deletions.
127 changes: 85 additions & 42 deletions about_code_tool/about.py
Expand Up @@ -1245,7 +1245,6 @@ def generate_attribution(self, template_path=None, limit_to=None):
'attribution texts. You can install it by running:'
'"configure"')
return

# For some reasons, if I set the template_path = "templates/default.html"
# in the parameter, the tempalte_path will become 'None' and cause error
if not template_path:
Expand All @@ -1257,7 +1256,6 @@ def generate_attribution(self, template_path=None, limit_to=None):
template_file_name = basename(template_path)
loader = j2.FileSystemLoader(template_dir)
jinja_env = j2.Environment(loader=loader)

try:
template = jinja_env.get_template(template_file_name)
except j2.TemplateNotFound:
Expand All @@ -1270,12 +1268,66 @@ def generate_attribution(self, template_path=None, limit_to=None):
about_content_dict = {}

not_process_components = list(limit_to)

component_exist = False

# ToDo: temp fix to have correct order in the attribution generation based on
# the input
for component in not_process_components:
# In addition, if user doesn't provide the component list a.k.a the
# limit_to, this code will never work.
# ToDo: This code need to be refactor. Too many duplicated code.
if not_process_components:
for component in not_process_components:
for about_object in self:
# FIXME: what is the meaning of this partition?
# PO created the var some_path to provide some clarity
# but what does the second element means?
file_name = about_object.location.partition(self.location)[2]
# FIXME: a path starting with / is NOT relative
about_relative_path = '/' + file_name
"""if limit_to:
try:
not_process_components.remove(about_relative_path)
except Exception as e:
continue"""

#if limit_to and about_relative_path in limit_to:
# continue
if component == about_relative_path:
component_exist = True
about_content = about_object.validated_fields
# Add information in the dictionary if not in the ABOUT file
lic_text = unicode(about_object.license_text(),
errors='replace')

about_content['license_text'] = lic_text
notice_text = about_object.notice_text()
about_content['notice_text'] = notice_text

# FIXME: The following is temporary code to handle multiple
# license_text_file paths in the field value, one per lne
for k in about_content:
if ('\n' in about_content[k]
and k == 'license_text_file'):
# FIXME: we should report decoding errors
lic_text = unicode(about_object.tmp_get_license_text(),
errors='replace')
about_content['license_text'] = lic_text

# report error if no license_text is found
if not about_content.get('license_text'):
msg = ('No license_text found. '
'Skipping License generation.')
err = Error(GENATTRIB, 'name',
about_object.get_about_name(), msg)
self.genattrib_errors.append(err)
about_object_fields.append(about_content)
break
if not component_exist:
msg = ('The requested ABOUT file: %r does not exist. '
'No attribution generated for this file.' % component)
err = Error(GENATTRIB, 'about_file', component, msg)
self.genattrib_errors.append(err)
else:
for about_object in self:
# FIXME: what is the meaning of this partition?
# PO created the var some_path to provide some clarity
Expand All @@ -1288,44 +1340,35 @@ def generate_attribution(self, template_path=None, limit_to=None):
not_process_components.remove(about_relative_path)
except Exception as e:
continue"""

#if limit_to and about_relative_path in limit_to:
# continue
if component == about_relative_path:
component_exist = True
about_content = about_object.validated_fields
# Add information in the dictionary if not in the ABOUT file
lic_text = unicode(about_object.license_text(),
errors='replace')

about_content['license_text'] = lic_text
notice_text = about_object.notice_text()
about_content['notice_text'] = notice_text

# FIXME: The following is temporary code to handle multiple
# license_text_file paths in the field value, one per lne
for k in about_content:
if ('\n' in about_content[k]
and k == 'license_text_file'):
# FIXME: we should report decoding errors
lic_text = unicode(about_object.tmp_get_license_text(),
errors='replace')
about_content['license_text'] = lic_text

# report error if no license_text is found
if not about_content.get('license_text'):
msg = ('No license_text found. '
'Skipping License generation.')
err = Error(GENATTRIB, 'name',
about_object.get_about_name(), msg)
self.genattrib_errors.append(err)
about_object_fields.append(about_content)
break
if not component_exist:
msg = ('The requested ABOUT file: %r does not exist. '
'No attribution generated for this file.' % component)
err = Error(GENATTRIB, 'about_file', component, msg)
self.genattrib_errors.append(err)

about_content = about_object.validated_fields
# Add information in the dictionary if not in the ABOUT file
lic_text = unicode(about_object.license_text(),
errors='replace')

about_content['license_text'] = lic_text
notice_text = about_object.notice_text()
about_content['notice_text'] = notice_text

# FIXME: The following is temporary code to handle multiple
# license_text_file paths in the field value, one per lne
for k in about_content:
if ('\n' in about_content[k]
and k == 'license_text_file'):
# FIXME: we should report decoding errors
lic_text = unicode(about_object.tmp_get_license_text(),
errors='replace')
about_content['license_text'] = lic_text

# report error if no license_text is found
if not about_content.get('license_text'):
msg = ('No license_text found. '
'Skipping License generation.')
err = Error(GENATTRIB, 'name',
about_object.get_about_name(), msg)
self.genattrib_errors.append(err)
about_object_fields.append(about_content)
break

"""# find paths requested in the limit_to paths arg that do not point to
# a corresponding ABOUT file
Expand Down

0 comments on commit 60703aa

Please sign in to comment.