Add an export as CSV feature of the validation errors

This commit is contained in:
Benjamin Renard 2019-09-10 13:52:33 +02:00
parent 55cfd4a7de
commit 18532f4bc0

View file

@ -317,7 +317,11 @@ def mass_validate_email(mail, simple=False):
if __name__ == '__main__':
from optparse import OptionParser
from optparse import OptionParser, OptionGroup
# Default options
default_output_delimiter = ";"
default_output_quotechar = '"'
parser = OptionParser()
# options
@ -391,6 +395,33 @@ if __name__ == '__main__':
help="Read emails addresses to validate from from"
)
output_opts = OptionGroup(parser, u"Output options")
output_opts.add_option(
'-o',
'--output-file',
action="store",
type='string',
dest="output_file",
help="Write emails addresses validation result as a CSV file"
)
output_opts.add_option(
'--delimiter',
action='store',
type='string',
dest='output_delimiter',
help="CSV ouput file delimiter (Default: %s)" % default_output_delimiter,
default=default_output_delimiter
)
output_opts.add_option(
'--quotechar',
action='store',
type='string',
dest='output_quotechar',
help="CSV ouput file quote character (Default: %s)" % default_output_quotechar,
default=default_output_quotechar
)
parser.add_option_group(output_opts)
(opts, emails) = parser.parse_args()
# Enable and configure logging
@ -417,6 +448,10 @@ if __name__ == '__main__':
if not emails:
parser.error('You must specify emails address as arguments')
# If output is enabled, import csv library
if opts.output_file:
import csv
# Configure other options from command line arguments
options.raiseonerror = True
options.debugsmtp = opts.debugsmtp
@ -469,5 +504,12 @@ if __name__ == '__main__':
else:
logging.info('All %s emails addresses provided are valid.', len(emails))
if opts.output_file:
logging.info('Write emails validation result to file %s', opts.output_file)
with open(opts.output_file, 'w') as fd:
csv_output = csv.writer(fd, delimiter=opts.output_delimiter, quotechar=opts.output_quotechar)
for email in not_validated:
csv_output.writerow([email, not_validated[email].error_msg])
# Adapt exit code on validation result
sys.exit(1 if not_validated else 0)