suppression des variables globales

This commit is contained in:
François Poulain 2020-08-24 19:05:59 +02:00
parent c147f844c5
commit 4ae4b7fe87
1 changed files with 47 additions and 58 deletions

View File

@ -28,74 +28,66 @@ from email.header import Header
from email.message import EmailMessage from email.message import EmailMessage
parser = argparse.ArgumentParser( def parse_args():
description=""" parser = argparse.ArgumentParser(description="Simple mailing script.")
Simple mailing script. ma = parser.add_argument_group(title="mandatory arguments", description=None)
""" ma.add_argument(
)
ma = parser.add_argument_group(title="mandatory arguments", description=None)
ma.add_argument(
"-t", "-t",
"--tofile", "--tofile",
metavar="TO.FILE", metavar="TO.FILE",
type=str, type=str,
required=True, required=True,
help="Sort of CSV file containing addresses of recipients.", help="Sort of CSV file containing addresses of recipients.",
) )
ma.add_argument( ma.add_argument(
"-b", "-b",
"--bodyfile", "--bodyfile",
metavar="BODY.FILE", metavar="BODY.FILE",
type=str, type=str,
required=True, required=True,
help="Template of the mail to be sent", help="Template of the mail to be sent",
) )
parser.add_argument(
parser.add_argument( "-d",
"-d", "--dry-run", action="store_true", help="Load data but don't send anything.", "--dry-run",
) action="store_true",
help="Load data but don't send anything.",
)
parser.add_argument( parser.add_argument(
"-a", "-a",
"--attachement", "--attachement",
metavar="ATTACHED.FILE", metavar="ATTACHED.FILE",
type=str, type=str,
nargs="?", nargs="?",
help="Optionnal attachment.", help="Optionnal attachment.",
) )
return parser.parse_args()
TOFILE = None
BODYFILE = None
JOINFILE = None
# read the recipients file where values are separated by | characters # read the recipients file where values are separated by | characters
def read_recipients(): def read_recipients(args):
recipientfile = open(TOFILE) recipientfile = open(args.tofile)
lines = recipientfile.readlines() lines = recipientfile.readlines()
return [line[:-1].split("|") for line in lines] return [line[:-1].split("|") for line in lines]
def read_body(): def read_body(args):
bodyfile = open(BODYFILE) bodyfile = open(args.bodyfile)
body = "" body = ""
for line in bodyfile.readlines(): for line in bodyfile.readlines():
body = body + line body = body + line
return body return body
def read_join(): def read_join(args):
try: try:
with open(JOINFILE, "rb") as fp: with open(args.attachement, "rb") as fp:
ctype, encoding = mimetypes.guess_type(JOINFILE) ctype, encoding = mimetypes.guess_type(args.attachement)
if ctype is None or encoding is not None: if ctype is None or encoding is not None:
ctype = "application/octet-stream" ctype = "application/octet-stream"
maintype, subtype = ctype.split("/", 1) maintype, subtype = ctype.split("/", 1)
metadata = { metadata = {
"filename": JOINFILE, "filename": args.attachement,
"maintype": maintype, "maintype": maintype,
"subtype": subtype, "subtype": subtype,
} }
@ -203,21 +195,18 @@ def send_message(message, to, attachment, args):
if __name__ == "__main__": if __name__ == "__main__":
args = parser.parse_args() args = parse_args()
TOFILE = args.tofile
BODYFILE = args.bodyfile
JOINFILE = args.attachement
# read the recipients file # read the recipients file
sets = read_recipients() sets = read_recipients(args)
# read the template of the mail # read the template of the mail
bodytemplate = read_body() bodytemplate = read_body(args)
# optionnaly read the attachment of the mail # optionnaly read the attachment of the mail
attachment = None attachment = None
if JOINFILE: if args.attachement:
attachment = read_join() attachment = read_join(args)
# counter to be able to pause each 10 mails send # counter to be able to pause each 10 mails send
counter = 0 counter = 0