refactor le style avec black

This commit is contained in:
François Poulain 2020-08-24 18:15:45 +02:00
parent f2dcc790da
commit 1064e517be
1 changed files with 73 additions and 62 deletions

View File

@ -31,13 +31,17 @@ from email.message import EmailMessage
# displays usage of the program, then quit
#
def usage(returncode):
print("""SYNTAX:
""", sys.argv[0], """ -t tofile -b bodyfile [-p attachedfile]
print(
"""SYNTAX:
""",
sys.argv[0],
""" -t tofile -b bodyfile [-p attachedfile]
tofile : sort of CSV file containing addresses of recipients
bodyfile : template of the mail to be sent
attachedfile : optionnal attachment
""")
""",
)
sys.exit(returncode)
@ -50,7 +54,7 @@ JOINFILE = None
def read_recipients():
recipientfile = open(TOFILE)
lines = recipientfile.readlines()
return [line[:-1].split('|') for line in lines]
return [line[:-1].split("|") for line in lines]
def read_body():
@ -60,23 +64,25 @@ def read_body() :
body = body + line
return body
def read_join():
try:
with open(JOINFILE, 'rb') as fp:
with open(JOINFILE, "rb") as fp:
ctype, encoding = mimetypes.guess_type(JOINFILE)
if ctype is None or encoding is not None:
ctype = 'application/octet-stream'
maintype, subtype = ctype.split('/', 1)
ctype = "application/octet-stream"
maintype, subtype = ctype.split("/", 1)
metadata = {
'filename': JOINFILE,
'maintype': maintype,
'subtype': subtype,
"filename": JOINFILE,
"maintype": maintype,
"subtype": subtype,
}
return (fp.read(), metadata)
except Exception as e:
print("read error: %s" % e)
exit(1)
def replace_values(bodytemplate, values):
body = bodytemplate
@ -96,17 +102,20 @@ CCPATTERN = re.compile(r"^Cc: *(.*)")
BCCPATTERN = re.compile(r"^Bcc: *(.*)")
REPLYTOPATTERN = re.compile(r"^Reply-To: *(.*)")
def qencode_subject(message):
subjectmatches = re.search(r"^Subject: *(.*)$", message, re.MULTILINE)
if (subjectmatches == None): return message
if subjectmatches == None:
return message
subjectstr = subjectmatches.group(1)
h = Header(subjectstr, 'utf-8')
h = Header(subjectstr, "utf-8")
qsubjectstr = h.encode()
return message.replace(subjectstr, qsubjectstr, 1)
def send_message(message, to, attachment):
lines = message.split('\n')
lines = message.split("\n")
# identify headers in the template
fromvalue = None
@ -117,7 +126,7 @@ def send_message(message, to, attachment) :
for index, line in enumerate(lines):
if not line:
body = '\n'.join(lines[index+1:])
body = "\n".join(lines[index + 1 :])
break
frommatches = FROMPATTERN.match(line)
@ -146,42 +155,45 @@ def send_message(message, to, attachment) :
dests.append(bccvalue)
msg = EmailMessage()
msg['Subject'] = subjectvalue
msg['From'] = fromvalue
msg['To'] = to
msg["Subject"] = subjectvalue
msg["From"] = fromvalue
msg["To"] = to
if replytovalue:
msg['Reply-To'] = replytovalue
msg["Reply-To"] = replytovalue
if ccvalue:
msg['Cc'] = ccvalue
msg["Cc"] = ccvalue
msg.set_content(body)
if attachment:
msg.add_attachment(attachment[0], **attachment[1])
print("Sending : %s, from %s to %s, dests : %s" % (subjectvalue, fromvalue, dests[0], repr(dests)))
print(
"Sending : %s, from %s to %s, dests : %s"
% (subjectvalue, fromvalue, dests[0], repr(dests))
)
# sending the mail to its recipients using the local mailer via SMTP
server = smtplib.SMTP('localhost','25')
server = smtplib.SMTP("localhost", "25")
server.set_debuglevel(1)
server.send_message(msg)
server.quit()
if __name__ == '__main__' :
if __name__ == "__main__":
# handle options of the program
try:
opts, args = getopt.getopt(sys.argv[1:], 't:b:p:')
opts, args = getopt.getopt(sys.argv[1:], "t:b:p:")
except getopt.error as msg:
print("getopt error: %s" % msg)
usage(1)
for name, value in opts:
if name == '-t': TOFILE = value
elif name == '-b': BODYFILE = value
elif name == '-p': JOINFILE = value
if name == "-t":
TOFILE = value
elif name == "-b":
BODYFILE = value
elif name == "-p":
JOINFILE = value
else:
print("argument: ", name, " invalid")
usage(1)
@ -223,4 +235,3 @@ if __name__ == '__main__' :
counter = 0
print("suspending execution for 5 secs")
time.sleep(5)