Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add the '--mapping' support for attrib.
  • Loading branch information
chinyeungli committed May 18, 2016
1 parent 7bc7f9e commit 88719a2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
19 changes: 17 additions & 2 deletions about_code_tool/attrib.py
Expand Up @@ -25,6 +25,7 @@
from about_code_tool import ERROR
from about_code_tool import Error
from licenses import COMMON_LICENSES
from util import get_mappings


def generate(abouts, template_string=None):
Expand Down Expand Up @@ -91,7 +92,7 @@ def generate_from_file(abouts, template_loc=None):
return generate(abouts, template_string=tpls)


def generate_and_save(abouts, output_location, template_loc=None,
def generate_and_save(abouts, output_location, mapping, template_loc=None,
inventory_location=None):
"""
Generate attribution using template and save at output_location.
Expand All @@ -107,8 +108,22 @@ def generate_and_save(abouts, output_location, template_loc=None,
with open(inventory_location, 'rU') as inp:
reader = csv.DictReader(inp)
about_data = [data for data in reader]
map_key = 'about_file_path'

if mapping:
mapping = get_mappings()
if 'about_file_path' in mapping:
map_key = mapping['about_file_path']

for data in about_data:
afp = data['about_file_path']
try:
afp = data[map_key]
except Exception, e:
# 'about_file_path' key/column doesn't exist
msg = (u'The required key: \'about_file_path\' does not exist. Generation halted.')
errors.append(Error(ERROR, msg))
return errors

if afp.startswith('/'):
afp = afp.partition('/')[2]
filter_afp.append(afp)
Expand Down
7 changes: 4 additions & 3 deletions about_code_tool/cmd.py
Expand Up @@ -237,7 +237,8 @@ def fetch(location):
dir_okay=False, resolve_path=True))
@click.option('--template', type=click.Path(exists=True), nargs=1,
help='Use the custom template for the Attribution Generation')
def attrib(location, output, template, inventory_location=None,):
@click.option('--mapping', is_flag=True, help='Configure the mapping key from the MAPPING.CONFIG')
def attrib(location, output, template, mapping, inventory_location=None,):
"""
Generate attribution document at output using the directory of
ABOUT files at location, the template file (or a default) and an
Expand All @@ -252,8 +253,8 @@ def attrib(location, output, template, inventory_location=None,):
location = extract_zip(location)

errors, abouts = about_code_tool.model.collect_inventory(location)
no_match_errors = about_code_tool.attrib.generate_and_save(abouts, output,
template_loc=template,
no_match_errors = about_code_tool.attrib.generate_and_save(abouts, output, mapping,
template_loc=template,
inventory_location=inventory_location)

log_errors(no_match_errors, os.path.dirname(output))
Expand Down

0 comments on commit 88719a2

Please sign in to comment.