Skip to content

Commit 9208bce

Browse files
committed
Fixed #103
correct the '--mapping' function AND Created test code for genattrib.py Updated the README and USAGE text
1 parent e4ab125 commit 9208bce

File tree

6 files changed

+95
-15
lines changed

6 files changed

+95
-15
lines changed

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ the .ABOUT files in the directory /tmp/thirdparty_about/.
165165
166166
Input can be a file or directory.
167167
Output of rendered template must be a file (e.g. .html).
168-
Component List must be a .csv file which has at least an "about_resource" column.
168+
Component List must be a .csv file which has at least an "about_file" column.
169169
170170
171171
Options:

USAGE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ Syntax
195195

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

200200

201201
Options:

about_code_tool/genattrib.py

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,38 @@ def component_subset_to_sublist(input_list):
7171
def update_path_to_about(input_list):
7272
output_list = []
7373
for row in input_list:
74-
if row.endswith('/'):
75-
row = row.rpartition('/')[0]
76-
output_list.append(row + '.ABOUT')
74+
if not row.endswith('.ABOUT'):
75+
if row.endswith('/'):
76+
row = row.rpartition('/')[0]
77+
output_list.append(row + '.ABOUT')
78+
else:
79+
output_list.append(row)
7780
return output_list
7881

82+
def convert_dict_key_to_lower_case(input_list):
83+
output_list = []
84+
for line in input_list:
85+
dict = {}
86+
for key in line:
87+
dict[key.lower()] = line[key]
88+
output_list.append(dict)
89+
return output_list
90+
91+
def check_about_file_existance_and_format(input_list):
92+
try:
93+
for row in input_list:
94+
# Force the path to start with the '/' to do the mapping
95+
# with the project structure
96+
if not row['about_file'].startswith('/'):
97+
row['about_file'] = '/' + row['about_file']
98+
return input_list
99+
except Exception as e:
100+
return []
101+
79102
USAGE_SYNTAX = """\
80103
Input can be a file or directory.
81104
Output of rendered template must be a file (e.g. .html).
82-
Component List must be a .csv file which has at least an "about_resource" column.
105+
Component List must be a .csv file which has at least an "about_file" column.
83106
"""
84107

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

157180
if not exists(output_path) or (exists(output_path) and overwrite):
158181
collector = AboutCollector(input_path)
159-
input_list = []
160182
if not component_subset_path:
161183
sublist = None
162184
else:
185+
input_list = []
163186
with open(component_subset_path, "rU") as f:
164187
input_dict = csv.DictReader(f)
165188
for row in input_dict:
166-
# Force the path to start with the '/' to do the mapping
167-
# with the project structure
168-
if not row['about_file'].startswith('/'):
169-
row['about_file'] = '/' + row['about_file']
170189
input_list.append(row)
190+
updated_list = convert_dict_key_to_lower_case(input_list)
171191
if mapping_config:
172192
mapping_list = genabout.GenAbout().get_mapping_list()
173-
input_list = genabout.GenAbout().convert_input_list(input_list, mapping_list)
174-
sublist = component_subset_to_sublist(input_list)
193+
updated_list = genabout.GenAbout().convert_input_list(updated_list, mapping_list)
194+
if not check_about_file_existance_and_format(updated_list):
195+
print("The required key, 'about_file, not found.")
196+
print("Please use the '--mapping' option to map the input keys and verify the mapping information are correct.")
197+
print("OR, correct the header keys from the component list.")
198+
parser.print_help()
199+
sys.exit(errno.EISDIR)
200+
sublist = component_subset_to_sublist(updated_list)
175201
outlist = update_path_to_about(sublist)
176202

177203
attrib_str = collector.generate_attribution(template_path=template_location, limit_to=outlist)

about_code_tool/tests/test_about.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# -*- coding: utf8 -*-
33

44
# =============================================================================
5-
# Copyright (c) 2013 by nexB, Inc. http://www.nexb.com/ - All rights reserved.
5+
# Copyright (c) 2014 by nexB, Inc. http://www.nexb.com/ - All rights reserved.
66
# Licensed under the Apache License, Version 2.0 (the "License");
77
# you may not use this file except in compliance with the License.
88
# You may obtain a copy of the License at

about_code_tool/tests/test_genabout.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# -*- coding: utf8 -*-
33

44
# =============================================================================
5-
# Copyright (c) 2013 by nexB, Inc. http://www.nexb.com/ - All rights reserved.
5+
# Copyright (c) 2014 by nexB, Inc. http://www.nexb.com/ - All rights reserved.
66
# Licensed under the Apache License, Version 2.0 (the "License");
77
# you may not use this file except in compliance with the License.
88
# You may obtain a copy of the License at
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf8 -*-
3+
4+
# =============================================================================
5+
# Copyright (c) 2014 by nexB, Inc. http://www.nexb.com/ - All rights reserved.
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
# =============================================================================
16+
17+
from __future__ import print_function, with_statement # We require Python 2.6 or later
18+
19+
import os
20+
import shutil
21+
import tempfile
22+
import unittest
23+
24+
from os.path import abspath, dirname, join
25+
26+
from about_code_tool import genattrib
27+
28+
29+
TESTDATA_PATH = join(abspath(dirname(__file__)), 'testdata')
30+
GEN_LOCATION = join(TESTDATA_PATH, 'test_files_for_genabout')
31+
32+
class GenAttribTest(unittest.TestCase):
33+
def test_convert_dict_key_to_lower_case(self):
34+
input_list = [{'Directory': '/test/', 'file_name': 'test.c'}]
35+
expected_list = [{'directory': '/test/', 'file_name': 'test.c'}]
36+
output = genattrib.convert_dict_key_to_lower_case(input_list)
37+
self.assertTrue(output == expected_list)
38+
39+
def test_check_no_about_file_existance(self):
40+
input_list = [{'Directory': '/test/', 'file_name': '/test.c'}]
41+
self.assertFalse(genattrib.check_about_file_existance_and_format(input_list))
42+
43+
def test_check_have_about_file_existance(self):
44+
input_list = [{'Directory': '/test/', 'about_file': '/test.ABOUT'}]
45+
self.assertTrue(genattrib.check_about_file_existance_and_format(input_list) == input_list)
46+
47+
def test_check_no_about_file_not_start_with_slash(self):
48+
input_list = [{'Directory': '/test/', 'file_name': 'test.c'}]
49+
self.assertFalse(genattrib.check_about_file_existance_and_format(input_list))
50+
51+
def test_check_have_about_file_not_start_with_slash(self):
52+
input_list = [{'Directory': '/test/', 'about_file': 'test.ABOUT'}]
53+
expected_list = [{'Directory': '/test/', 'about_file': '/test.ABOUT'}]
54+
self.assertTrue(genattrib.check_about_file_existance_and_format(input_list) == expected_list)

0 commit comments

Comments
 (0)