Skip to content

Commit 1b23580

Browse files
committed
Do not use a global for mapping #280
* instead use a flag that is passed around as needed Signed-off-by: Philippe Ombredanne <[email protected]>
1 parent 6b44f6c commit 1b23580

File tree

9 files changed

+199
-193
lines changed

9 files changed

+199
-193
lines changed

src/attributecode/attrib.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ def generate_from_file(abouts, template_loc=None):
138138
return generate(abouts, template_string=tpls)
139139

140140

141-
def generate_and_save(abouts, output_location, mapping, template_loc=None,
142-
inventory_location=None):
141+
def generate_and_save(abouts, output_location, use_mapping=False,
142+
template_loc=None, inventory_location=None):
143143
"""
144144
Generate attribution using template and save at output_location.
145145
Filter the list of about object based on the inventory CSV at
@@ -163,7 +163,8 @@ def generate_and_save(abouts, output_location, mapping, template_loc=None,
163163
if inventory_location.endswith('.csv') or inventory_location.endswith('.json'):
164164
try:
165165
# Return a list which contains only the about file path
166-
about_list = attributecode.util.get_about_file_path(mapping, inventory_location)
166+
about_list = attributecode.util.get_about_file_path(
167+
inventory_location, use_mapping=use_mapping)
167168
except Exception:
168169
# 'about_file_path' key/column doesn't exist
169170
msg = u"The required key: 'about_file_path' does not exist. Generation halted."

src/attributecode/cmd.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def inventory(location, output, quiet, format):
139139
@click.option('--license-notice-text-location', nargs=1,
140140
type=click.Path(exists=True, dir_okay=True, readable=True, resolve_path=True),
141141
help="Copy the 'license_file' from the directory to the generated location")
142-
@click.option('--mapping', is_flag=True, help='Use for mapping between the input keys and the ABOUT field names - mapping.config')
142+
@click.option('--mapping', is_flag=True, help='Use file mapping.config with mapping between input keys and ABOUT field names')
143143
@click.option('-q', '--quiet', is_flag=True, help='Do not print any error/warning.')
144144
def gen(location, output, mapping, license_notice_text_location, fetch_license, quiet):
145145
"""
@@ -150,13 +150,16 @@ def gen(location, output, mapping, license_notice_text_location, fetch_license,
150150
151151
OUTPUT: Path to a directory where ABOUT files are generated.
152152
"""
153-
click.echo('Running attributecode version ' + __version__)
153+
click.echo('Running aboutcode-toolkit version ' + __version__)
154154
if not location.endswith('.csv') and not location.endswith('.json'):
155155
click.echo('ERROR: Input file. Only .csv and .json files are supported.')
156156
return
157157
click.echo('Generating ABOUT files...')
158158

159-
errors, abouts = attributecode.gen.generate(location, output, mapping, license_notice_text_location, fetch_license)
159+
errors, abouts = attributecode.gen.generate(
160+
location=location, base_dir=output, mapping=mapping,
161+
license_notice_text_location=license_notice_text_location,
162+
fetch_license=fetch_license)
160163

161164
number_of_about_file = len(abouts)
162165
number_of_error = 0
@@ -174,7 +177,7 @@ def gen(location, output, mapping, license_notice_text_location, fetch_license,
174177
@click.argument('output', nargs=1, required=True, type=click.Path(exists=False, writable=True, resolve_path=True))
175178
@click.option('--inventory', required=False, type=click.Path(exists=True, file_okay=True, resolve_path=True),
176179
help='Path to an inventory file')
177-
@click.option('--mapping', is_flag=True, help='Use for mapping between the input keys and the ABOUT field names - mapping.config')
180+
@click.option('--mapping', is_flag=True, help='Use file mapping.config with mapping between input keys and ABOUT field names')
178181
@click.option('--template', type=click.Path(exists=True), nargs=1,
179182
help='Path to a custom attribution template')
180183
@click.option('-q', '--quiet', is_flag=True, help='Do not print any error/warning.')
@@ -196,12 +199,11 @@ def attrib(location, output, template, mapping, inventory, quiet):
196199
# accept zipped ABOUT files as input
197200
location = extract_zip(location)
198201

199-
if mapping:
200-
attributecode.util.have_mapping = True
201-
202202
errors, abouts = model.collect_inventory(location)
203203
no_match_errors = attributecode.attrib.generate_and_save(
204-
abouts, output, mapping, template_loc=template, inventory_location=inventory)
204+
abouts=abouts, output_location=output,
205+
use_mapping=mapping, template_loc=template,
206+
inventory_location=inventory)
205207

206208
for no_match_error in no_match_errors:
207209
errors.append(no_match_error)
@@ -218,7 +220,7 @@ def check(location):
218220
219221
LOCATION: Path to an ABOUT file or a directory containing ABOUT files.
220222
"""
221-
click.echo('Running attributecode version ' + __version__)
223+
click.echo('Running aboutcode-toolkit version ' + __version__)
222224
click.echo('Checking ABOUT files...')
223225

224226
errors, abouts = attributecode.model.collect_inventory(location)
@@ -241,6 +243,7 @@ def check(location):
241243
else:
242244
click.echo('No error is found.')
243245

246+
244247
def log_errors(errors, quiet, base_dir=False):
245248
"""
246249
Iterate of sequence of Error objects and print and log errors with a severity

src/attributecode/gen.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ def check_duplicated_about_file_path(inventory_dict):
9797
return errors
9898

9999

100-
def load_inventory(mapping, location, base_dir, license_notice_text_location=None):
100+
def load_inventory(location, base_dir,
101+
license_notice_text_location=None, use_mapping=False):
101102
"""
102103
Load the inventory file at location. Return a list of errors and a list of About
103104
objects validated against the base_dir.
@@ -110,9 +111,9 @@ def load_inventory(mapping, location, base_dir, license_notice_text_location=Non
110111
if dup_cols_err:
111112
errors.extend(dup_cols_err)
112113
return errors, abouts
113-
inventory = util.load_csv(mapping, location)
114+
inventory = util.load_csv(location, use_mapping)
114115
else:
115-
inventory = util.load_json(mapping, location)
116+
inventory = util.load_json(location, use_mapping)
116117

117118
try:
118119
dup_about_paths_err = check_duplicated_about_file_path(inventory)
@@ -126,9 +127,9 @@ def load_inventory(mapping, location, base_dir, license_notice_text_location=Non
126127

127128
for i, fields in enumerate(inventory):
128129
# check does the input contains the required fields
129-
requied_fileds = model.About.required_fields
130+
required_fields = model.About.required_fields
130131

131-
for f in requied_fileds:
132+
for f in required_fields:
132133
if f not in fields:
133134
msg = (
134135
"Required column: %(f)r not found.\n"
@@ -164,8 +165,8 @@ def load_inventory(mapping, location, base_dir, license_notice_text_location=Non
164165
return errors, abouts
165166

166167

167-
def generate(location, base_dir, mapping, license_notice_text_location, fetch_license, policy=None, conf_location=None,
168-
with_empty=False, with_absent=False):
168+
def generate(location, base_dir, license_notice_text_location=None, fetch_license=False, policy=None, conf_location=None,
169+
with_empty=False, with_absent=False, use_mapping=False):
169170
"""
170171
Load ABOUT data from an inventory at csv_location. Write ABOUT files to
171172
base_dir using policy flags and configuration file at conf_location.
@@ -183,7 +184,11 @@ def generate(location, base_dir, mapping, license_notice_text_location, fetch_li
183184
gen_license = True
184185

185186
bdir = to_posix(base_dir)
186-
errors, abouts = load_inventory(mapping, location, bdir, license_notice_text_location)
187+
errors, abouts = load_inventory(
188+
location=location,
189+
base_dir=bdir,
190+
license_notice_text_location=license_notice_text_location,
191+
use_mapping=use_mapping)
187192

188193
if gen_license:
189194
license_dict, err = model.pre_process_and_fetch_license_dict(abouts, api_url, api_key)
@@ -291,4 +296,4 @@ def deduplicate(sequence):
291296
for item in sequence:
292297
if item not in deduped:
293298
deduped.append(item)
294-
return deduped
299+
return deduped

0 commit comments

Comments
 (0)