Skip to content

Commit b45ad61

Browse files
committed
Remove duplicated code in validators
Signed-off-by: Philippe Ombredanne <[email protected]>
1 parent d23f4c3 commit b45ad61

File tree

1 file changed

+22
-32
lines changed

1 file changed

+22
-32
lines changed

src/attributecode/cmd.py

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from __future__ import unicode_literals
2020

2121
from collections import defaultdict
22-
import errno
22+
from functools import partial
2323
import io
2424
import logging
2525
import os
@@ -103,20 +103,21 @@ def about():
103103

104104

105105
######################################################################
106-
# inventory subcommand
106+
# option validators
107107
######################################################################
108108

109-
def validate_filter(ctx, param, value):
109+
def validate_key_values(ctx, param, value):
110110
"""
111-
Return the parsed filter if valid or raise a UsageError otherwise.
111+
Return the a mapping of {key: [values,...] if valid or raise a UsageError
112+
otherwise.
112113
"""
113114
if not value:
114115
return
115116

116117
kvals, errors = parse_key_values(value)
117118
if errors:
118119
ive = '\n'.join(sorted(' ' + x for x in errors))
119-
msg = ('Invalid --filter option(s):\n'
120+
msg = ('Invalid {param} option(s):\n'
120121
'{ive}'.format(**locals()))
121122
raise click.UsageError(msg)
122123
return kvals
@@ -136,6 +137,20 @@ def validate_mapping(mapping, mapping_file):
136137
return mapping_file or None
137138

138139

140+
def validate_extensions(ctx, param, value, extensions=tuple(('.csv', '.json',))):
141+
if not value:
142+
return
143+
if not value.endswith(extensions):
144+
msg = ' '.join(extensions)
145+
raise click.UsageError(
146+
'Invalid {param} file extension: must be one of: {msg}'.format(**locals()))
147+
return value
148+
149+
150+
######################################################################
151+
# inventory subcommand
152+
######################################################################
153+
139154
@about.command(cls=AboutCommand,
140155
short_help='Collect the inventory of .ABOUT files to a CSV or JSON file.')
141156

@@ -154,7 +169,7 @@ def validate_mapping(mapping, mapping_file):
154169
@click.option('--filter',
155170
multiple=True,
156171
metavar='<key>=<value>',
157-
callback=validate_filter,
172+
callback=validate_key_values,
158173
help='Filter the inventory to ABOUT matching these key=value e.g. "license_expression=gpl-2.0')
159174

160175
@click.option('-f', '--format',
@@ -242,15 +257,6 @@ def inventory(location, output, mapping, mapping_file,
242257
# gen subcommand
243258
######################################################################
244259

245-
def validate_location_extension(ctx, param, value):
246-
if not value:
247-
return
248-
if not value.endswith(('.csv', '.json',)):
249-
raise click.UsageError(
250-
'Invalid input file extension: must be one .csv or .json.')
251-
return value
252-
253-
254260
@about.command(cls=AboutCommand,
255261
short_help='Generate .ABOUT files from an inventory as CSV or JSON.')
256262

@@ -342,22 +348,6 @@ def gen(location, output,
342348
# attrib subcommand
343349
######################################################################
344350

345-
def validate_variables(ctx, param, value):
346-
"""
347-
Return the variable texts if valid or raise a UsageError otherwise.
348-
"""
349-
if not value:
350-
return
351-
352-
kvals, errors = parse_key_values(value)
353-
if errors:
354-
ive = '\n'.join(sorted(' ' + x for x in errors))
355-
msg = ('Invalid --vartext option(s):\n'
356-
'{ive}'.format(**locals()))
357-
raise click.UsageError(msg)
358-
return kvals
359-
360-
361351
def validate_template(ctx, param, value):
362352
if not value:
363353
return DEFAULT_TEMPLATE_FILE
@@ -396,7 +386,7 @@ def validate_template(ctx, param, value):
396386

397387
@click.option('--vartext',
398388
multiple=True,
399-
callback=validate_variables,
389+
callback=validate_key_values,
400390
metavar='<key>=<value>',
401391
help='Add variable text as key=value for use in a custom attribution template.')
402392

0 commit comments

Comments
 (0)