more readable mail.saveMail output

This commit is contained in:
Daniel Langbein 2021-07-24 16:07:26 +02:00
parent 47cd16ac14
commit 76ccc6897a
3 changed files with 14 additions and 6 deletions

View File

@ -38,9 +38,7 @@ def executeCommand(command: List) -> int:
print(BODY) print(BODY)
if exitCode != 0: if exitCode != 0:
hostname = socket.gethostname() SUBJECT = f'{socket.gethostname()} | {str(command)}'
SUBJECT = f'{hostname} | {str(command)}'
mail.sendMailOrWriteToFile(SUBJECT=SUBJECT, BODY=BODY) mail.sendMailOrWriteToFile(SUBJECT=SUBJECT, BODY=BODY)
return exitCode return exitCode

View File

@ -6,7 +6,7 @@ import socket
from email.mime.text import MIMEText from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart from email.mime.multipart import MIMEMultipart
from de.p1st.exec_notify.lib import config from de.p1st.exec_notify.lib import config, util
def sendMailOrWriteToFile(SUBJECT: str, BODY: str, informAboutLocalMail: bool = True): def sendMailOrWriteToFile(SUBJECT: str, BODY: str, informAboutLocalMail: bool = True):
@ -65,13 +65,23 @@ def saveMail(SUBJECT: str, BODY: str):
mailDir = config.getMailDir() mailDir = config.getMailDir()
mailFile = mailDir.joinpath(timeStr) mailFile = mailDir.joinpath(timeStr)
prefix=''
mailStr = f'{""*19}\n' \
f'┣╸Date:\n' \
f'{util.appendLinePrefix(prefix, timeStr)}\n' \
f'┣╸Subject:\n' \
f'{util.appendLinePrefix(prefix, SUBJECT)}\n' \
f'┣╸Body:\n' \
f'{util.appendLinePrefix(prefix, BODY)}\n' \
f'{""*19}'
try: try:
# create parent directory if not existent # create parent directory if not existent
mailDir.mkdir(parents=True, exist_ok=True) mailDir.mkdir(parents=True, exist_ok=True)
# append to file; create file if not existent # append to file; create file if not existent
with open(mailFile, "a") as f: with open(mailFile, "a") as f:
f.write(f'{"=" * 20}\n=== date ===\n{timeStr}\n=== subject ===\n{SUBJECT}\n=== body ===\n{BODY}\n') f.write(mailStr)
except Exception as e: except Exception as e:
print(f'execNotify>> Could not write to file: {e}', file=sys.stderr) print(f'execNotify>> Could not write to file: {e}', file=sys.stderr)

View File

@ -28,7 +28,7 @@ def main():
if subj is None: if subj is None:
subj = 'notify' subj = 'notify'
if BODY is None: if BODY is None:
BODY = f'=== stdin ===\n{stdin.read()}' BODY = stdin.read()
SUBJECT = f'{hostname} | {subj}' SUBJECT = f'{hostname} | {subj}'
print(BODY) print(BODY)