Skip to content

Commit bc92d17

Browse files
committed
* new `--vartext` option * updated changelog and usage Signed-off-by: Chin Yeung Li <[email protected]>
1 parent 75b512f commit bc92d17

File tree

4 files changed

+42
-16
lines changed

4 files changed

+42
-16
lines changed

USAGE.rst

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ attrib
4545
names - mapping.config
4646
--mapping-file Use a custom mapping file with mapping between input
4747
keys and ABOUT field names.
48-
--verbose Show all the errors and warning.
4948
--template PATH Path to a custom attribution template.
49+
--vartext TEXT Variable texts to the attribution template
50+
--verbose Show all the errors and warning.
5051
-q, --quiet Do not print any error/warning.
5152
-h, --help Show this message and exit.
5253

@@ -90,11 +91,6 @@ Options
9091

9192
$ about attrib --mapping-file CUSTOM_MAPPING_FILE_PATH LOCATION OUTPUT
9293

93-
--verbose
94-
95-
This option tells the tool to show all errors found.
96-
The default behavior will only show 'CRITICAL', 'ERROR', and 'WARNING'
97-
9894
--template
9995

10096
This option allows you to use your own template for attribution generation.
@@ -103,6 +99,21 @@ Options
10399

104100
$ about attrib --template /home/custom_template/template.html LOCATION OUTPUT
105101

102+
--vartext
103+
104+
This option allow you to pass variable texts to the attribution template
105+
106+
$ about attrib --vartext "title=Attribution Notice" --vartext "header="Product 101" LOCATION OUTPUT
107+
108+
Users can use the following in the template to get the vartext:
109+
{{ vartext_dict['title'] }}
110+
{{ vartext_dict['header'] }}
111+
112+
--verbose
113+
114+
This option tells the tool to show all errors found.
115+
The default behavior will only show 'CRITICAL', 'ERROR', and 'WARNING'
116+
106117

107118
The following data are passed to jinja2 and, therefore, can be used for a custom template:
108119
* about object: the about objects
@@ -282,7 +293,7 @@ Options
282293

283294
Same behavior as `--mapping` but with custom mapping file
284295

285-
$ about attrib --mapping-file CUSTOM_MAPPING_FILE_PATH LOCATION OUTPUT
296+
$ about inventory --mapping-file CUSTOM_MAPPING_FILE_PATH LOCATION OUTPUT
286297

287298
--verbose
288299

docs/CHANGELOG.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
2018-x-x
2+
3+
Release 3.1.2
4+
5+
* New `--vartext` option
6+
17
2018-6-25
28

39
Release 3.1.1

src/attributecode/attrib.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
from attributecode.util import add_unc
3838

3939

40-
def generate(abouts, template_string=None, title=None):
40+
def generate(abouts, template_string=None, vartext_dict=None):
4141
"""
4242
Generate and return attribution text from a list of About objects and a
4343
template string.
@@ -110,7 +110,7 @@ def generate(abouts, template_string=None, title=None):
110110
license_file_name_and_key=license_file_name_and_key,
111111
license_key_to_license_name=license_key_to_license_name,
112112
license_name_to_license_key=license_name_to_license_key,
113-
utcnow=utcnow, title=title)
113+
utcnow=utcnow, vartext_dict=vartext_dict)
114114
except Exception as e:
115115
line = getattr(e, 'lineno', None)
116116
ln_msg = ' at line: %r' % line if line else ''
@@ -134,7 +134,7 @@ def check_template(template_string):
134134
default_template = join(os.path.dirname(os.path.realpath(__file__)),
135135
'templates', 'default_html.template')
136136

137-
def generate_from_file(abouts, template_loc=None, title=None):
137+
def generate_from_file(abouts, template_loc=None, vartext_dict=None):
138138
"""
139139
Generate and return attribution string from a list of About objects and a
140140
template location.
@@ -144,11 +144,11 @@ def generate_from_file(abouts, template_loc=None, title=None):
144144
template_loc = add_unc(template_loc)
145145
with codecs.open(template_loc, 'rb', encoding='utf-8') as tplf:
146146
tpls = tplf.read()
147-
return generate(abouts, template_string=tpls, title=title)
147+
return generate(abouts, template_string=tpls, vartext_dict=vartext_dict)
148148

149149

150150
def generate_and_save(abouts, output_location, use_mapping=False, mapping_file=None,
151-
template_loc=None, inventory_location=None, title=None):
151+
template_loc=None, inventory_location=None, vartext=None):
152152
"""
153153
Generate attribution file using the `abouts` list of About object
154154
at `output_location`.
@@ -167,6 +167,7 @@ def generate_and_save(abouts, output_location, use_mapping=False, mapping_file=N
167167
afp_list = []
168168
not_match_path = []
169169
errors = []
170+
vartext_dict = {}
170171

171172
if not inventory_location:
172173
updated_abouts = abouts
@@ -239,7 +240,14 @@ def generate_and_save(abouts, output_location, use_mapping=False, mapping_file=N
239240
else:
240241
about.license.value = lic_list
241242

242-
rendered = generate_from_file(updated_abouts, template_loc=template_loc, title=title)
243+
# Parse the vartext and save to the vartext dictionary
244+
if vartext:
245+
for var in vartext:
246+
key = var.partition('=')[0]
247+
value = var.partition('=')[2]
248+
vartext_dict[key] = value
249+
250+
rendered = generate_from_file(updated_abouts, template_loc=template_loc, vartext_dict=vartext_dict)
243251

244252
if rendered:
245253
output_location = add_unc(output_location)

src/attributecode/cmd.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,8 @@ def gen(location, output, mapping, mapping_file, license_notice_text_location, f
293293
@click.option('--template', type=click.Path(exists=True), nargs=1,
294294
help='Path to an optional custom attribution template used for generation.')
295295

296-
@click.option('--title', nargs=1, help='Title for the attribution output.')
296+
@click.option('--vartext', nargs=1, multiple=True,
297+
help='Variable texts to the attribution template.')
297298

298299
@click.option('--verbose', is_flag=True, default=False,
299300
help='Show all errors and warnings. '
@@ -308,7 +309,7 @@ def gen(location, output, mapping, mapping_file, license_notice_text_location, f
308309

309310
@click.help_option('-h', '--help')
310311

311-
def attrib(location, output, template, mapping, mapping_file, inventory, title, quiet, verbose):
312+
def attrib(location, output, template, mapping, mapping_file, inventory, vartext, quiet, verbose):
312313
"""
313314
Generate an attribution document at OUTPUT using .ABOUT files at LOCATION.
314315
@@ -327,7 +328,7 @@ def attrib(location, output, template, mapping, mapping_file, inventory, title,
327328
no_match_errors = attrib_generate_and_save(
328329
abouts=abouts, output_location=output,
329330
use_mapping=mapping, mapping_file=mapping_file, template_loc=template,
330-
inventory_location=inventory, title=title)
331+
inventory_location=inventory, vartext=vartext)
331332

332333
if not no_match_errors:
333334
# Check for template error

0 commit comments

Comments
 (0)