#!/usr/bin/env python3 import asyncio import sys from espeak import espeak class Speaker(object): """Speak aloud something instead of calling numbers.""" def __init__(self, voice='europe/it'): espeak.set_voice(voice) def _output(self, text): print(text) espeak.synth(text) def speak(self, number): try: getattr(self, 'call_'+number, None)() except TypeError: self._output("Sto chiamando il numero %s." % number) def call_1234(self): self._output(u"Questa รจ la combinazione della mia valigia.") def call_1234567890(self): self._output(u"Stiamo testando tutti i numeri, vero?") def call_666(self): self._output(u"Risponde la segreteria telefonica di Satana.") def call_42(self): self._output(u"E' la risposta alla domanda fondamentale sulla vita, l'universo e tuttoquanto. Giusto?") class Reader(object): """Read digits from stdin and turn them into phone numbers.""" def __init__(self): self.number = "" self.handle = None self.speaker = Speaker() def read_number(self): text = sys.stdin.readline() if len(text) == 2 and text[0] in "0123456789": self.number += text[0] if self.handle: self.handle.cancel() self.handle = self.loop.call_later(3, self.call_number) # TODO: do we want a warning in case we receive something wrong? def call_number(self): self.speaker.speak(self.number) self.number = "" def run(self): self.loop = asyncio.get_event_loop() self.loop.add_reader(sys.stdin, self.read_number) try: self.loop.run_forever() finally: self.loop.close() def main(): reader = Reader() reader.run() if __name__ == '__main__': main()