more readable execNotify output

This commit is contained in:
Daniel Langbein 2021-07-24 15:59:01 +02:00
parent 3ecafa19a2
commit 47cd16ac14
4 changed files with 54 additions and 8 deletions

View File

@ -4,7 +4,7 @@ from sys import argv
import socket
from typing import List
from de.p1st.exec_notify.lib import exec, config, mail
from de.p1st.exec_notify.lib import exec, config, mail, util
def main():
@ -22,19 +22,25 @@ def executeCommand(command: List) -> int:
:return: exit code of executed command
"""
keys = ['command', 'exit code', 'stderr', 'stdout']
exitCode, stdout, stderr = exec.execute(command)
values = [str(command), str(exitCode), stderr, stdout]
BODY = ''
for key, value in zip(keys, values):
BODY += f'=== {key} ===\n{value}\n'
prefix=''
BODY = f'{""*19}\n' \
f'┣╸Command:\n' \
f'{util.appendLinePrefix(prefix, str(command))}\n' \
f'┣╸Exit Code:\n' \
f'{util.appendLinePrefix(prefix, str(exitCode))}\n' \
f'┣╸stderr:\n' \
f'{util.appendLinePrefix(prefix, stderr)}\n' \
f'┣╸stdout:\n' \
f'{util.appendLinePrefix(prefix, stdout)}\n' \
f'{""*19}'
print(BODY)
if exitCode != 0:
hostname = socket.gethostname()
SUBJECT = '{hostname} | {str(command)}'
SUBJECT = f'{hostname} | {str(command)}'
mail.sendMailOrWriteToFile(SUBJECT=SUBJECT, BODY=BODY)
return exitCode

View File

@ -12,7 +12,8 @@ from de.p1st.exec_notify.lib import config
def sendMailOrWriteToFile(SUBJECT: str, BODY: str, informAboutLocalMail: bool = True):
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! ' \
BODY=f'[!] Note [!]\n' \
f'There 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}'

View File

@ -22,3 +22,12 @@ def readFirstLine(file: Path) -> str:
"""
with open(file, "r") as f:
return f.readline()
def appendLinePrefix(prefix: str, s: str) -> str:
if s is None or len(s) == 0:
return prefix
result = ''.join([prefix + line for line in s.splitlines(keepends=True)])
if result.endswith(os.linesep):
result+=prefix
return result

30
tests/util_test.py Normal file
View File

@ -0,0 +1,30 @@
import unittest
import de.p1st.exec_notify.lib.util as util
class MyTestCase(unittest.TestCase):
def test1_appendLinePrefix(self):
s = "This is a sentence.\nAnd this a second line."
expected = "PREFIX This is a sentence.\nPREFIX And this a second line."
actual = util.appendLinePrefix("PREFIX ", s)
self.assertEqual(expected, actual)
def test2_appendLinePrefix(self):
s = "\n"
expected = "PREFIX \nPREFIX "
actual = util.appendLinePrefix("PREFIX ", s)
self.assertEqual(expected, actual)
def test3_appendLinePrefix(self):
s = ""
expected = "PREFIX "
actual = util.appendLinePrefix("PREFIX ", s)
self.assertEqual(expected, actual)
if __name__ == '__main__':
unittest.main()