Skip to content

Commit

Permalink
Fixed #103
Browse files Browse the repository at this point in the history
correct the '--mapping' function

AND
Created test code for genattrib.py
Updated the README and USAGE text
  • Loading branch information
chinyeungli committed Jun 25, 2014
1 parent e4ab125 commit 9208bce
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.rst
Expand Up @@ -165,7 +165,7 @@ the .ABOUT files in the directory /tmp/thirdparty_about/.
Input can be a file or directory.
Output of rendered template must be a file (e.g. .html).
Component List must be a .csv file which has at least an "about_resource" column.
Component List must be a .csv file which has at least an "about_file" column.
Options:
Expand Down
2 changes: 1 addition & 1 deletion USAGE
Expand Up @@ -195,7 +195,7 @@ Syntax

Input can be a file or directory.
Output of rendered template must be a file (e.g. .html).
Component List must be a .csv file which has at least an "about_resource" column.
Component List must be a .csv file which has at least an "about_file" column.


Options:
Expand Down
48 changes: 37 additions & 11 deletions about_code_tool/genattrib.py
Expand Up @@ -71,15 +71,38 @@ def component_subset_to_sublist(input_list):
def update_path_to_about(input_list):
output_list = []
for row in input_list:
if row.endswith('/'):
row = row.rpartition('/')[0]
output_list.append(row + '.ABOUT')
if not row.endswith('.ABOUT'):
if row.endswith('/'):
row = row.rpartition('/')[0]
output_list.append(row + '.ABOUT')
else:
output_list.append(row)
return output_list

def convert_dict_key_to_lower_case(input_list):
output_list = []
for line in input_list:
dict = {}
for key in line:
dict[key.lower()] = line[key]
output_list.append(dict)
return output_list

def check_about_file_existance_and_format(input_list):
try:
for row in input_list:
# Force the path to start with the '/' to do the mapping
# with the project structure
if not row['about_file'].startswith('/'):
row['about_file'] = '/' + row['about_file']
return input_list
except Exception as e:
return []

USAGE_SYNTAX = """\
Input can be a file or directory.
Output of rendered template must be a file (e.g. .html).
Component List must be a .csv file which has at least an "about_resource" column.
Component List must be a .csv file which has at least an "about_file" column.
"""

VERBOSITY_HELP = """\
Expand Down Expand Up @@ -156,22 +179,25 @@ def main(parser, options, args):

if not exists(output_path) or (exists(output_path) and overwrite):
collector = AboutCollector(input_path)
input_list = []
if not component_subset_path:
sublist = None
else:
input_list = []
with open(component_subset_path, "rU") as f:
input_dict = csv.DictReader(f)
for row in input_dict:
# Force the path to start with the '/' to do the mapping
# with the project structure
if not row['about_file'].startswith('/'):
row['about_file'] = '/' + row['about_file']
input_list.append(row)
updated_list = convert_dict_key_to_lower_case(input_list)
if mapping_config:
mapping_list = genabout.GenAbout().get_mapping_list()
input_list = genabout.GenAbout().convert_input_list(input_list, mapping_list)
sublist = component_subset_to_sublist(input_list)
updated_list = genabout.GenAbout().convert_input_list(updated_list, mapping_list)
if not check_about_file_existance_and_format(updated_list):
print("The required key, 'about_file, not found.")
print("Please use the '--mapping' option to map the input keys and verify the mapping information are correct.")
print("OR, correct the header keys from the component list.")
parser.print_help()
sys.exit(errno.EISDIR)
sublist = component_subset_to_sublist(updated_list)
outlist = update_path_to_about(sublist)

attrib_str = collector.generate_attribution(template_path=template_location, limit_to=outlist)
Expand Down
2 changes: 1 addition & 1 deletion about_code_tool/tests/test_about.py
Expand Up @@ -2,7 +2,7 @@
# -*- coding: utf8 -*-

# =============================================================================
# Copyright (c) 2013 by nexB, Inc. http://www.nexb.com/ - All rights reserved.
# Copyright (c) 2014 by nexB, Inc. http://www.nexb.com/ - All rights reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
Expand Down
2 changes: 1 addition & 1 deletion about_code_tool/tests/test_genabout.py
Expand Up @@ -2,7 +2,7 @@
# -*- coding: utf8 -*-

# =============================================================================
# Copyright (c) 2013 by nexB, Inc. http://www.nexb.com/ - All rights reserved.
# Copyright (c) 2014 by nexB, Inc. http://www.nexb.com/ - All rights reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
Expand Down
54 changes: 54 additions & 0 deletions about_code_tool/tests/test_genattrib.py
@@ -0,0 +1,54 @@
#!/usr/bin/env python
# -*- coding: utf8 -*-

# =============================================================================
# Copyright (c) 2014 by nexB, Inc. http://www.nexb.com/ - All rights reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# =============================================================================

from __future__ import print_function, with_statement # We require Python 2.6 or later

import os
import shutil
import tempfile
import unittest

from os.path import abspath, dirname, join

from about_code_tool import genattrib


TESTDATA_PATH = join(abspath(dirname(__file__)), 'testdata')
GEN_LOCATION = join(TESTDATA_PATH, 'test_files_for_genabout')

class GenAttribTest(unittest.TestCase):
def test_convert_dict_key_to_lower_case(self):
input_list = [{'Directory': '/test/', 'file_name': 'test.c'}]
expected_list = [{'directory': '/test/', 'file_name': 'test.c'}]
output = genattrib.convert_dict_key_to_lower_case(input_list)
self.assertTrue(output == expected_list)

def test_check_no_about_file_existance(self):
input_list = [{'Directory': '/test/', 'file_name': '/test.c'}]
self.assertFalse(genattrib.check_about_file_existance_and_format(input_list))

def test_check_have_about_file_existance(self):
input_list = [{'Directory': '/test/', 'about_file': '/test.ABOUT'}]
self.assertTrue(genattrib.check_about_file_existance_and_format(input_list) == input_list)

def test_check_no_about_file_not_start_with_slash(self):
input_list = [{'Directory': '/test/', 'file_name': 'test.c'}]
self.assertFalse(genattrib.check_about_file_existance_and_format(input_list))

def test_check_have_about_file_not_start_with_slash(self):
input_list = [{'Directory': '/test/', 'about_file': 'test.ABOUT'}]
expected_list = [{'Directory': '/test/', 'about_file': '/test.ABOUT'}]
self.assertTrue(genattrib.check_about_file_existance_and_format(input_list) == expected_list)

1 comment on commit 9208bce

@chinyeungli
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is meant to fixed #104

Please sign in to comment.