From 3ecafa19a298d6534662d54b0e338c270ab41e14 Mon Sep 17 00:00:00 2001 From: Daniel Langbein Date: Sat, 24 Jul 2021 15:20:59 +0200 Subject: [PATCH] minor improvements --- src/de/p1st/exec_notify/execNotify.py | 4 ++-- src/de/p1st/exec_notify/lib/mail.py | 15 +++++++++------ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/de/p1st/exec_notify/execNotify.py b/src/de/p1st/exec_notify/execNotify.py index fbc917a..228a203 100755 --- a/src/de/p1st/exec_notify/execNotify.py +++ b/src/de/p1st/exec_notify/execNotify.py @@ -28,13 +28,13 @@ def executeCommand(command: List) -> int: BODY = '' for key, value in zip(keys, values): - BODY += '=== {} ===\n{}\n'.format(key, value) + BODY += f'=== {key} ===\n{value}\n' print(BODY) if exitCode != 0: hostname = socket.gethostname() - SUBJECT = '{} | {}'.format(hostname, str(command)) + SUBJECT = '{hostname} | {str(command)}' mail.sendMailOrWriteToFile(SUBJECT=SUBJECT, BODY=BODY) return exitCode diff --git a/src/de/p1st/exec_notify/lib/mail.py b/src/de/p1st/exec_notify/lib/mail.py index e7044c5..5193a93 100644 --- a/src/de/p1st/exec_notify/lib/mail.py +++ b/src/de/p1st/exec_notify/lib/mail.py @@ -10,9 +10,10 @@ from de.p1st.exec_notify.lib import config def sendMailOrWriteToFile(SUBJECT: str, BODY: str, informAboutLocalMail: bool = True): - if informAboutLocalMail and localMailExists(): - BODY=f'[!] Note [!]\nThere is some local mail inside [file][maildir] that could not be sent previously! ' \ - f'Please read and then delete the local mail.\n\n\n' \ + if informAboutLocalMail and _localMailExists(): + SUBJECT=f'{SUBJECT} | UNREAD LOCAL MAIL' + BODY=f'[!] Note [!]\nThere is local mail inside [file][maildir] that was not delivered previously! ' \ + f'Please read and then delete it to get rid of this warning.\n\n\n' \ f'{BODY}' try: @@ -60,19 +61,21 @@ def saveMail(SUBJECT: str, BODY: str): time = datetime.datetime.now() timeStr = time.strftime('%Y%m%d_%H%M%S') + mailDir = config.getMailDir() + mailFile = mailDir.joinpath(timeStr) + try: # create parent directory if not existent - mailDir = config.getMailDir() mailDir.mkdir(parents=True, exist_ok=True) # append to file; create file if not existent - with open(mailDir.joinpath(timeStr), "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') except Exception as e: print(f'execNotify>> Could not write to file: {e}', file=sys.stderr) -def localMailExists(): +def _localMailExists(): """ :return: True if local mail exists in maildir folder. Once the mail is read the user shall delete (or move) it. """