Skip to content

Commit 03235f7

Browse files
committed
* Add multi_sort filter
* Update the key_value parser as I think the value should be string, not list (I don't see a use case a list is needed/used for any purpose) * Update test cases
1 parent 076f9d7 commit 03235f7

File tree

3 files changed

+14
-20
lines changed

3 files changed

+14
-20
lines changed

src/attributecode/attrib.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# -*- coding: utf8 -*-
33

44
# ============================================================================
5-
# Copyright (c) 2013-2019 nexB Inc. http://www.nexb.com/ - All rights reserved.
5+
# Copyright (c) 2013-2020 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
@@ -32,13 +32,14 @@
3232
from attributecode.licenses import COMMON_LICENSES
3333
from attributecode.model import parse_license_expression
3434
from attributecode.util import add_unc
35+
from attributecode.attrib_util import multi_sort
3536

3637

3738
DEFAULT_TEMPLATE_FILE = os.path.join(
3839
os.path.dirname(os.path.realpath(__file__)), '../../templates', 'default_html.template')
3940

4041

41-
def generate(abouts, template=None, vartext_dict=None):
42+
def generate(abouts, template=None, variables=None):
4243
"""
4344
Generate an attribution text from an `abouts` list of About objects, a
4445
`template` template text and a `variables` optional dict of extra
@@ -125,7 +126,7 @@ def generate(abouts, template=None, vartext_dict=None):
125126
license_name_to_license_key=license_name_to_license_key,
126127
utcnow=utcnow,
127128
tkversion=__version__,
128-
vartext_dict=vartext_dict
129+
variables=variables
129130
)
130131
except Exception as e:
131132
lineno = getattr(e, 'lineno', '') or ''
@@ -145,12 +146,13 @@ def check_template(template_string):
145146
message) if the template is invalid or None if it is valid.
146147
"""
147148
try:
149+
jinja2.filters.FILTERS['multi_sort'] = multi_sort
148150
jinja2.Template(template_string)
149151
except (jinja2.TemplateSyntaxError, jinja2.TemplateAssertionError) as e:
150152
return e.lineno, e.message
151153

152154

153-
def generate_from_file(abouts, template_loc=DEFAULT_TEMPLATE_FILE, vartext_dict=None):
155+
def generate_from_file(abouts, template_loc=DEFAULT_TEMPLATE_FILE, variables=None):
154156
"""
155157
Generate an attribution text from an `abouts` list of About objects, a
156158
`template_loc` template file location and a `variables` optional
@@ -163,7 +165,7 @@ def generate_from_file(abouts, template_loc=DEFAULT_TEMPLATE_FILE, vartext_dict=
163165
template_loc = add_unc(template_loc)
164166
with io.open(template_loc, encoding='utf-8') as tplf:
165167
tpls = tplf.read()
166-
return generate(abouts, template=tpls, vartext_dict=vartext_dict)
168+
return generate(abouts, template=tpls, variables=variables)
167169

168170

169171
def generate_and_save(abouts, output_location, template_loc=None, variables=None):
@@ -186,16 +188,10 @@ def generate_and_save(abouts, output_location, template_loc=None, variables=None
186188
str(special_char_in_expression))
187189
errors.append(Error(ERROR, msg))
188190

189-
vartext_dict = {}
190-
if variables:
191-
keys = variables.keys()
192-
for k in keys:
193-
vartext_dict[k] = (variables[k])[0]
194-
195191
rendering_error, rendered = generate_from_file(
196192
abouts,
197193
template_loc=template_loc,
198-
vartext_dict=vartext_dict
194+
variables=variables
199195
)
200196

201197
if rendering_error:

src/attributecode/cmd.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def about():
107107

108108
def validate_key_values(ctx, param, value):
109109
"""
110-
Return the a dict of {key: [values,...] if valid or raise a UsageError
110+
Return the a dict of {key: value} if valid or raise a UsageError
111111
otherwise.
112112
"""
113113
if not value:
@@ -523,7 +523,7 @@ def get_error_messages(errors, quiet=False, verbose=False):
523523
def parse_key_values(key_values):
524524
"""
525525
Given a list of "key=value" strings, return:
526-
- a dict {key: [value, value, ...]}
526+
- a dict {key: value}
527527
- a sorted list of unique error messages for invalid entries where there is
528528
a missing a key or value.
529529
"""
@@ -545,9 +545,7 @@ def parse_key_values(key_values):
545545
errors.add('missing <value> in "{key_value}".'.format(**locals()))
546546
continue
547547

548-
values = parsed_key_values[key]
549-
if value not in values:
550-
parsed_key_values[key].append(value)
548+
parsed_key_values[key] = value
551549

552550
return dict(parsed_key_values), sorted(errors)
553551

tests/test_cmd.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,8 @@ def test_parse_key_values_simple(self):
307307
'keY=bar',
308308
]
309309
expected = {
310-
'key': ['value', 'bar'],
311-
'this': ['THat']
310+
'key': 'bar',
311+
'this': 'THat'
312312
}
313313
keyvals, errors = cmd.parse_key_values(test)
314314
assert expected == keyvals
@@ -323,7 +323,7 @@ def test_parse_key_values_with_errors(self):
323323
'FOO=bar'
324324
]
325325
expected = {
326-
'foo': ['bar'],
326+
'foo': 'bar',
327327
}
328328
keyvals, errors = cmd.parse_key_values(test)
329329
assert expected == keyvals

0 commit comments

Comments
 (0)