From 1edb32ec29ca3c1bf4d1a53215ecb2544aa04a6e Mon Sep 17 00:00:00 2001 From: Mark Powers Date: Mon, 12 Jul 2021 02:00:54 +0100 Subject: Refactor code to support phone hangup button --- events.py | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 events.py (limited to 'events.py') diff --git a/events.py b/events.py new file mode 100644 index 0000000..77480b9 --- /dev/null +++ b/events.py @@ -0,0 +1,64 @@ +import subprocess +from datetime import datetime + +class Event(): + def __init__(self, dial_manager): + self.dial_manager = dial_manager + + def get_name(self): + return "generic event" + + def get_text(self): + return "event" + + def speak(self): + text = self.get_text() + print("speaking:", text) + subprocess.run(["espeak", text]) + +class FortuneEvent(Event): + def __init__(self, dial_manager): + super().__init__(dial_manager) + + def get_name(self): + return "fortune" + + def get_text(self): + res = subprocess.run(["fortune", "-s"], stdout=subprocess.PIPE) + return res.stdout.decode("utf-8") + +class DirectoryEvent(Event): + def __init__(self, dial_manager): + super().__init__(dial_manager) + + def get_name(self): + return "directory" + + def get_text(self): + def recursive_build(prefix, sequences): + sequence_list = [] + for key in sequences.keys(): + sequence = prefix+" "+str(key) + if isinstance(sequences[key], dict): + sequence_list += recursive_build(sequence, sequences[key]) + else: + sequence_list.append((sequence, sequences[key])) + return sequence_list + sequences = self.dial_manager.sequences + text = "" + for sequence, event in recursive_build("", sequences): + text += f"Dial {sequence} for {event.get_name()}. " + return text + + +class OperatorEvent(Event): + def __init__(self, dial_manager): + super().__init__(dial_manager) + + def get_name(self): + return "operator" + + def get_text(self): + now = datetime.now() + return f"Right now, it is {now.hour} {now.minute}. Dial 4 1 1 for directory." + -- cgit v1.2.3