charge les templates jinja2 depuis le basename passé en argument

This commit is contained in:
François Poulain 2020-08-24 21:06:21 +02:00
parent a212c2bdd8
commit 1688532aee
2 changed files with 22 additions and 12 deletions

View File

@ -14,6 +14,7 @@ On a Debian-based host running at least Debian Stretch, you will need the
following packages:
- git (recommended for getting the source)
- python3
- python3-jinja2
### Manual installation

View File

@ -26,6 +26,7 @@ import argparse, csv, sys, re
import smtplib, time, mimetypes
from email.header import Header
from email.message import EmailMessage
from jinja2 import Environment, FileSystemLoader, select_autoescape
def parse_args():
@ -40,11 +41,12 @@ def parse_args():
)
ma.add_argument(
"-b",
"--bodyfile",
metavar="BODY.FILE",
"--basename",
type=str,
required=True,
help="Template of the mail to be sent",
help="Templates basename to be used. The known extensions are: "
".subject for subject, .txt for plain body "
"and optionally .html for html alternative.",
)
parser.add_argument(
"-d",
@ -82,12 +84,19 @@ def read_recipients_datas(args):
return full_datas
def read_body(args):
bodyfile = open(args.bodyfile)
body = ""
for line in bodyfile.readlines():
body = body + line
return body
def read_templates(args):
env = Environment(
loader=FileSystemLoader("."), autoescape=select_autoescape(["html"])
)
templates = [
env.get_template("{}.subject".format(args.basename)),
env.get_template("{}.txt".format(args.basename)),
]
try:
templates.append(env.get_template("{}.html".format(args.basename)))
except Exception:
pass
return templates
def read_join(args):
@ -218,11 +227,11 @@ if __name__ == "__main__":
# read the recipients file
datas = read_recipients_datas(args)
print(datas)
exit(0)
# read the template of the mail
bodytemplate = read_body(args)
templates = read_templates(args)
print(templates)
exit(0)
# optionnaly read the attachment of the mail
attachment = None