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: following packages:
- git (recommended for getting the source) - git (recommended for getting the source)
- python3 - python3
- python3-jinja2
### Manual installation ### Manual installation

View File

@ -26,6 +26,7 @@ import argparse, csv, sys, re
import smtplib, time, mimetypes import smtplib, time, mimetypes
from email.header import Header from email.header import Header
from email.message import EmailMessage from email.message import EmailMessage
from jinja2 import Environment, FileSystemLoader, select_autoescape
def parse_args(): def parse_args():
@ -40,11 +41,12 @@ def parse_args():
) )
ma.add_argument( ma.add_argument(
"-b", "-b",
"--bodyfile", "--basename",
metavar="BODY.FILE",
type=str, type=str,
required=True, 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( parser.add_argument(
"-d", "-d",
@ -82,12 +84,19 @@ def read_recipients_datas(args):
return full_datas return full_datas
def read_body(args): def read_templates(args):
bodyfile = open(args.bodyfile) env = Environment(
body = "" loader=FileSystemLoader("."), autoescape=select_autoescape(["html"])
for line in bodyfile.readlines(): )
body = body + line templates = [
return body 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): def read_join(args):
@ -218,11 +227,11 @@ if __name__ == "__main__":
# read the recipients file # read the recipients file
datas = read_recipients_datas(args) datas = read_recipients_datas(args)
print(datas)
exit(0)
# read the template of the mail # 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 # optionnaly read the attachment of the mail
attachment = None attachment = None