mylib.scripts.helpers: add possibility to specify config dict to handle default option values

This commit is contained in:
Benjamin Renard 2021-05-26 14:44:15 +02:00
parent 51a4d35823
commit b2d33e3ddf

View file

@ -3,7 +3,10 @@
""" Scripts helpers """
import argparse
import getpass
import logging
import socket
import sys
from mylib.email import EmailClient
@ -30,8 +33,17 @@ def init_logging(options, name, report=None):
logging.basicConfig(level=loglevel, format=logformat, handlers=handlers)
def get_opts_parser(desc=None, just_try=False, just_one=False, progress=False):
def get_default_opt_value(config, default_config, key):
""" Retreive default option value from config or default config dictionaries """
if config and key in config:
return config[key]
return default_config.get(key)
def get_opts_parser(desc=None, just_try=False, just_one=False, progress=False, config=None):
""" Retrieve options parser """
default_config = dict(logfile=None)
parser = argparse.ArgumentParser(description=desc)
parser.add_argument(
@ -53,7 +65,8 @@ def get_opts_parser(desc=None, just_try=False, just_one=False, progress=False):
action="store",
type=str,
dest="logfile",
help="Log file path"
help="Log file path (default: %s)" % get_default_opt_value(config, default_config, 'logfile'),
default=get_default_opt_value(config, default_config, 'logfile')
)
parser.add_argument(
@ -90,16 +103,24 @@ def get_opts_parser(desc=None, just_try=False, just_one=False, progress=False):
return parser
def add_email_opts(parser):
def add_email_opts(parser, config=None):
""" Add email options """
email_opts = parser.add_argument_group('Email options')
default_config = dict(
smtp_host="127.0.0.1", smtp_port=25, smtp_ssl=False, smtp_tls=False, smtp_user=None,
smtp_password=None, smtp_debug=False, email_encoding=sys.getdefaultencoding(),
sender_name=getpass.getuser(), sender_email=getpass.getuser() + '@' + socket.gethostname(),
catch_all=False
)
email_opts.add_argument(
'-H', '--smtp-host',
action="store",
type=str,
dest="email_smtp_host",
help="SMTP host"
help="SMTP host (default: %s)" % get_default_opt_value(config, default_config, 'smtp_host'),
default=get_default_opt_value(config, default_config, 'smtp_host')
)
email_opts.add_argument(
@ -107,21 +128,24 @@ def add_email_opts(parser):
action="store",
type=int,
dest="email_smtp_port",
help="SMTP port"
help="SMTP port (default: %s)" % get_default_opt_value(config, default_config, 'smtp_port'),
default=get_default_opt_value(config, default_config, 'smtp_port')
)
email_opts.add_argument(
'-S', '--smtp-ssl',
action="store_true",
dest="email_smtp_ssl",
help="Use SSL"
help="Use SSL (default: %s)" % get_default_opt_value(config, default_config, 'smtp_ssl'),
default=get_default_opt_value(config, default_config, 'smtp_ssl')
)
email_opts.add_argument(
'-T', '--smtp-tls',
action="store_true",
dest="email_smtp_tls",
help="Use TLS"
help="Use TLS (default: %s)" % get_default_opt_value(config, default_config, 'smtp_tls'),
default=get_default_opt_value(config, default_config, 'smtp_tls')
)
email_opts.add_argument(
@ -129,7 +153,8 @@ def add_email_opts(parser):
action="store",
type=str,
dest="email_smtp_user",
help="SMTP username"
help="SMTP username (default: %s)" % get_default_opt_value(config, default_config, 'smtp_user'),
default=get_default_opt_value(config, default_config, 'smtp_user')
)
email_opts.add_argument(
@ -137,14 +162,16 @@ def add_email_opts(parser):
action="store",
type=str,
dest="email_smtp_password",
help="SMTP password"
help="SMTP password (default: %s)" % get_default_opt_value(config, default_config, 'smtp_password'),
default=get_default_opt_value(config, default_config, 'smtp_password')
)
email_opts.add_argument(
'-D', '--smtp-debug',
action="store_true",
dest="email_smtp_debug",
help="Debug SMTP connection"
help="Debug SMTP connection (default: %s)" % get_default_opt_value(config, default_config, 'smtp_debug'),
default=get_default_opt_value(config, default_config, 'smtp_debug')
)
email_opts.add_argument(
@ -152,7 +179,8 @@ def add_email_opts(parser):
action="store",
type=str,
dest="email_encoding",
help="SMTP encoding"
help="SMTP encoding (default: %s)" % get_default_opt_value(config, default_config, 'email_encoding'),
default=get_default_opt_value(config, default_config, 'email_encoding')
)
email_opts.add_argument(
@ -160,7 +188,8 @@ def add_email_opts(parser):
action="store",
type=str,
dest="email_sender_name",
help="Sender name"
help="Sender name (default: %s)" % get_default_opt_value(config, default_config, 'sender_name'),
default=get_default_opt_value(config, default_config, 'sender_name')
)
email_opts.add_argument(
@ -168,7 +197,8 @@ def add_email_opts(parser):
action="store",
type=str,
dest="email_sender_email",
help="Sender email"
help="Sender email (default: %s)" % get_default_opt_value(config, default_config, 'sender_email'),
default=get_default_opt_value(config, default_config, 'sender_email')
)
email_opts.add_argument(
@ -176,7 +206,8 @@ def add_email_opts(parser):
action="store",
type=str,
dest="email_catch_all",
help="Catch all sent email: specify catch recipient email address"
help="Catch all sent email: specify catch recipient email address (default: %s)" % get_default_opt_value(config, default_config, 'catch_all'),
default=get_default_opt_value(config, default_config, 'catch_all')
)
@ -194,7 +225,7 @@ def init_email_client(options, **kwargs):
sender_name=options.email_sender_name,
sender_email=options.email_sender_email,
catch_all_addr=options.email_catch_all,
just_try=options.just_try,
just_try=options.just_try if hasattr(options, 'just_try') else False,
encoding=options.email_encoding,
**kwargs
)