Skip to content

Commit 692c573

Browse files
committed
Improve the implementation of the AboutCollector, output_path is a params for the write_to_csv rather than on the class, #23
1 parent 8f19385 commit 692c573

File tree

4 files changed

+25
-19
lines changed

4 files changed

+25
-19
lines changed

about_code_tool/about.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -893,12 +893,11 @@ def notice_text(self):
893893

894894

895895
class AboutCollector(object):
896-
def __init__(self, input_path, output_path, verbosity):
896+
def __init__(self, input_path, verbosity):
897897
# Setup the input and output paths
898898
self.original_input_path = input_path
899899
self.input_path = abspath(input_path)
900900
assert exists(self.input_path)
901-
self.output_path = output_path
902901

903902
# Setup the verbosity
904903
self.display_error = self.display_warning = False
@@ -909,10 +908,12 @@ def __init__(self, input_path, output_path, verbosity):
909908

910909
self.about_files = []
911910
self.about_objects = []
911+
self.about_data_list = []
912912

913913
# Running the files collection and objects creation on instantiation
914914
self.collect_about_files()
915915
self.create_about_objects_from_files()
916+
self.extract_about_data_from_objects()
916917

917918
def collect_about_files(self):
918919
"""
@@ -940,10 +941,13 @@ def create_about_objects_from_files(self):
940941

941942
self.about_objects = about_objects
942943

943-
def extract_about_info(self):
944+
def extract_about_data_from_objects(self):
944945
"""
945946
Builds rows for each stored about objects.
946947
"""
948+
if self.about_data_list: # Process only once.
949+
return
950+
947951
about_data_list = []
948952
warnings_count = errors_count = 0
949953

@@ -982,21 +986,24 @@ def extract_about_info(self):
982986
if about_object.warnings:
983987
print("WARNING: %s\n" % about_object.warnings)
984988

985-
self.write_to_csv(about_data_list)
989+
self.about_data_list = about_data_list
990+
986991
if errors_count:
987992
print("%d errors detected." % errors_count)
988993
if warnings_count:
989994
print("%d warnings detected.\n" % warnings_count)
990995

991-
def write_to_csv(self, about_data_list):
996+
def write_to_csv(self, output_path):
992997
"""
993998
Write results in CSV file at output_path.
994999
"""
995-
with open(self.output_path, 'wb') as output_file:
1000+
header_row = ['about_file'] + MANDATORY_FIELDS + OPTIONAL_FIELDS + \
1001+
['warnings', 'errors']
1002+
1003+
with open(output_path, 'wb') as output_file:
9961004
about_spec_writer = csv.writer(output_file)
997-
about_spec_writer.writerow(['about_file'] + MANDATORY_FIELDS +
998-
OPTIONAL_FIELDS + ['warnings', 'errors'])
999-
for row in about_data_list:
1005+
about_spec_writer.writerow(header_row)
1006+
for row in self.about_data_list:
10001007
about_spec_writer.writerow(row)
10011008

10021009
def generate_attribution(self, template_path='templates/default.html',
@@ -1105,8 +1112,8 @@ def main(parser, options, args):
11051112
sys.exit(errno.EEXIST)
11061113

11071114
if not exists(output_path) or (exists(output_path) and overwrite):
1108-
collector = AboutCollector(input_path, output_path, verbosity)
1109-
collector.extract_about_info()
1115+
collector = AboutCollector(input_path, verbosity)
1116+
collector.write_to_csv(output_path)
11101117
else:
11111118
# we should never reach this
11121119
assert False, "Unsupported option(s)."

about_code_tool/genabout.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import os
3232
import shutil
3333
import sys
34-
import urllib2
3534

3635
__version__ = '0.9.0'
3736

about_code_tool/genattrib.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
"""
1818
This is a tool to generate component attribution based on a set of .ABOUT files.
19-
Optionally, one could pass a subset list of specific components for set of
19+
Optionally, one could pass a subset list of specific components for set of
2020
.ABOUT files to generate attribution.
2121
"""
2222

@@ -135,7 +135,7 @@ def main(args, opts):
135135
input_path = args[0]
136136
output_path = args[1]
137137
component_subset_path = None if len(args) < 3 else args[2]
138-
138+
139139
# TODO: need more path normalization (normpath, expanduser)
140140
# input_path = abspath(input_path)
141141
output_path = abspath(output_path)

about_code_tool/tests/test_about.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ def test_return_path_is_not_abspath_and_contains_subdirs_on_file(self):
5151
os.remove(test_output)
5252
except OSError:
5353
pass
54-
collector = about.AboutCollector(test_input, test_output, 0)
55-
collector.extract_about_info()
54+
collector = about.AboutCollector(test_input, 0)
55+
collector.write_to_csv(test_output)
5656
self.assertTrue(open(test_output).read().partition('\n')[2].startswith('about_code_tool/tests/testdata/thirdparty/django_snippets_2413.ABOUT'))
5757
os.remove(test_output)
5858

@@ -64,8 +64,8 @@ def test_return_path_is_not_abspath_and_contains_subdirs_on_dir(self):
6464
os.remove(test_output)
6565
except OSError:
6666
pass
67-
collector = about.AboutCollector(test_input, test_output, 0)
68-
collector.extract_about_info()
67+
collector = about.AboutCollector(test_input, 0)
68+
collector.write_to_csv(test_output)
6969
self.assertTrue(open(test_output).read().partition('\n')[2].startswith('about_code_tool/tests/testdata/basic'))
7070
os.remove(test_output)
7171

@@ -475,7 +475,7 @@ def test_remove_blank_lines_and_no_colon_fields(self):
475475

476476
def test_generate_attribution(self):
477477
expected = u'version:2.4.3about_resource:httpd-2.4.3.tar.gzname:Apache HTTP Server'
478-
about_collector = about.AboutCollector(join(TESTDATA_PATH, 'attrib/attrib.ABOUT'), None, 0)
478+
about_collector = about.AboutCollector(join(TESTDATA_PATH, 'attrib/attrib.ABOUT'), 0)
479479
result = about_collector.generate_attribution(join(TESTDATA_PATH, 'attrib/test.template'))
480480
self.assertEqual(result, expected)
481481

0 commit comments

Comments
 (0)