fundamentally working with cmd

This commit is contained in:
Anika 2024-02-12 12:43:24 +01:00
commit 2ba8ae8772
5 changed files with 117 additions and 0 deletions

3
README.md Normal file
View File

@ -0,0 +1,3 @@
# Fetenzentrale
Joa, kann Log schreiben und alerten. Nicht sehr kompliziert.

BIN
alarm.mp3 Normal file

Binary file not shown.

7
alarms.txt Normal file
View File

@ -0,0 +1,7 @@
02/08/24 15:10:56 ~ ters
02/08/24 14:53:07 ~ test
02/08/24 15:53:07 ~ test
02/08/24 15:21:32 ~ asdfjklö sdfjkl
02/08/24 15:27:44 ~ juhu
02/08/24 15:39:49 ~ lds
02/08/24 15:43:17 ~ asfhk ksadf

104
fete.py Executable file
View File

@ -0,0 +1,104 @@
import cmd
import os
import sys
import time
import subprocess
from datetime import datetime, timedelta
alarms_queue = list()
class Fete(cmd.Cmd):
intro = "Willkommen zum Logging und Alarm Skript Ihres Vertrauens. \nBei Fragen help oder ? benutzen."
use_rawinput = False
def do_log(self, toLog):
"Log message with current timestamp"
log = open("log.txt", "a")
datetime_obj = datetime.now()
log_message = datetime_obj.strftime("%x") +", " + datetime_obj.strftime("%X") +" Uhr: "+ toLog
print( log_message )
# log als was besseres als txt, aber meh
log.write(log_message + "\n")
def do_alarm(self, args):
"set an alarm in x minutes for y, example: alarm 30 Cocktail wants stuff"
alarms = open("alarms.txt", "a")
args_arr = args.split()
print("\a")
if len(args_arr) < 2:
print("Heeer! Dir fehlt ein Argument. ")
return
try:
delta = int(args_arr[0])
except ValueError:
print("Das erste Argument muss ne Ganzzahl sein!")
return
args_arr.pop(0)
text = ' '.join(args_arr)
alarm_time = datetime.now() + timedelta(minutes = delta)
print("Alarm set for " + alarm_time.strftime("%X") + " " + text)
alarms.write(alarm_time.strftime("%x") + " " +alarm_time.strftime("%X") + " ~ " + text + "\n")
global alarms_queue
alarms_queue.append((alarm_time,text))
def do_readAlarms(self, minutes):
"read alarms from file into alarm queue that haven't happened or should have happened in the last x minutes (default=5)"
try:
if minutes:
delta = int(minutes)
else:
delta = 5
except ValueError:
print("Zeit bitte in ganzen Minuten!")
return
earliest_alarm_time = datetime.now() - timedelta(minutes = delta)
alarms = open("alarms.txt", "r").readlines()
for line in alarms:
line_sep = line.split(" ~ ")
alarm_time = datetime.strptime(line_sep[0], "%m/%d/%y %H:%M:%S")
if alarm_time > earliest_alarm_time:
global alarms_queue
alarms_queue.append((alarm_time,line_sep[1]))
class NonblockingStdin:
def __init__(self, stdin, idle_callback):
self._stdin = stdin
self._idle_callback = idle_callback
os.set_blocking(self._stdin.fileno(), False)
def readline(self):
buffer = ""
char = self._stdin.read(1)
while char != "\n":
buffer += char
char = self._stdin.read(1)
if char == "":
time.sleep(0.1)
self._idle_callback()
return buffer
def alarm():
alarms_copy = alarms_queue
for alarm_read in alarms_copy:
if datetime.now() >= alarm_read[0]:
subprocess.call(['mpv', './alarm.mp3'], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
print("\n\n")
print(alarm_read[1])
print("")
alarms_queue.remove(alarm_read)
def main():
wrapped = NonblockingStdin(sys.stdin, alarm)
fete = Fete(stdin=wrapped)
fete.cmdloop()
if __name__ == '__main__':
main()

3
log.txt Normal file
View File

@ -0,0 +1,3 @@
02/07/24, 19:18:19Uhr: 02/08/24, 15:18:54Uhr: hilfe, ich habe mein Pasenbrot vergessen
02/08/24, 15:26:12 Uhr: Es ist dumm nen Alarm zusetzen bevor man die alten eingelesen hat
02/08/24, 15:26:52 Uhr: außerdem, doppelte leerzeichen als trennsymbol, das kann nur schief gehen