def __init__(self, logfile=None): self._q = Queue() self._device = None self._autostop = False self._state = "IDLE" self.sched = scheduler.get_scheduler() # FileWrapper protects against interrupted system call self._in = FileWrapper( os.fdopen(int(os.environ["VOICE_INPUT"]), "r", 1)) self._out = FileWrapper( os.fdopen(int(os.environ["VOICE_OUTPUT"]), "w", 0)) self._ppid = int(os.environ["VOICE_PID"]) self._program = os.environ["VOICE_PROGRAM"] # modem login variables self.caller_id = os.environ.get("CALLER_ID") self.caller_name = os.environ.get("CALLER_NAME") self.called_id = os.environ.get("CALLED_ID") self.connectstring = os.environ.get("CONNECT") self._device = os.environ.get("DEVICE") self._log = logfile if self._log: self._log.write("-----------\n### Starting %s\n----------\n" % (self.__class__.__name__, )) self.EVENT_DISPATCH = {} for ename in EVENTS: self.EVENT_DISPATCH[ename] = [] self.chat(['HELLO SHELL', 'HELLO VOICE PROGRAM', 'READY']) # for easy subclass initialization self.initialize()
def __init__(self, logfile=None): self._q = Queue() self._device = None self._autostop = False self._state = "IDLE" self.sched = scheduler.get_scheduler() # FileWrapper protects against interrupted system call self._in = FileWrapper(os.fdopen(int(os.environ["VOICE_INPUT"]), "r", 1)) self._out = FileWrapper(os.fdopen(int(os.environ["VOICE_OUTPUT"]), "w", 0)) self._ppid = int(os.environ["VOICE_PID"]) self._program = os.environ["VOICE_PROGRAM"] # modem login variables self.caller_id = os.environ.get("CALLER_ID") self.caller_name = os.environ.get("CALLER_NAME") self.called_id = os.environ.get("CALLED_ID") self.connectstring = os.environ.get("CONNECT") self._device = os.environ.get("DEVICE") self._log = logfile if self._log: self._log.write("-----------\n### Starting %s\n----------\n" % (self.__class__.__name__,)) self.EVENT_DISPATCH = {} for ename in EVENTS: self.EVENT_DISPATCH[ename] = [] self.chat(['HELLO SHELL', 'HELLO VOICE PROGRAM', 'READY']) # for easy subclass initialization self.initialize()
class CallProgram(object): def __init__(self, logfile=None): self._q = Queue() self._device = None self._autostop = False self._state = "IDLE" self.sched = scheduler.get_scheduler() # FileWrapper protects against interrupted system call self._in = FileWrapper( os.fdopen(int(os.environ["VOICE_INPUT"]), "r", 1)) self._out = FileWrapper( os.fdopen(int(os.environ["VOICE_OUTPUT"]), "w", 0)) self._ppid = int(os.environ["VOICE_PID"]) self._program = os.environ["VOICE_PROGRAM"] # modem login variables self.caller_id = os.environ.get("CALLER_ID") self.caller_name = os.environ.get("CALLER_NAME") self.called_id = os.environ.get("CALLED_ID") self.connectstring = os.environ.get("CONNECT") self._device = os.environ.get("DEVICE") self._log = logfile if self._log: self._log.write("-----------\n### Starting %s\n----------\n" % (self.__class__.__name__, )) self.EVENT_DISPATCH = {} for ename in EVENTS: self.EVENT_DISPATCH[ename] = [] self.chat(['HELLO SHELL', 'HELLO VOICE PROGRAM', 'READY']) # for easy subclass initialization self.initialize() def __del__(self): self.close() def register(self, evname, handler): """Register an event handler. The event name is one from the EVENTS list. The handler should be a callable.""" assert evname in EVENTS, "bad event name" if callable(handler): self.EVENT_DISPATCH[evname].append(handler) else: raise ValueError, "register: handler must be callable" def unregister(self, evname, handler): hlist = self.EVENT_DISPATCH[evname] try: hlist.remove(handler) except ValueError: pass def initialize(self): """For subclasses to add extra initialization.""" pass def log(self, entrytype, entry): if self._log: self._log.write("%s: %r\n" % (entrytype, entry)) def _timeout_cb(self): self._timed_out = 1 def _receive(self): self._timed_out = 0 ev = self.sched.add(60, 0, self._timeout_cb, ()) try: while True: try: data = self._in.readline() except EnvironmentError, why: if why.errno == EINTR: if self._timed_out == 1: raise TimeoutError, "timed out during recieve." else: continue else: raise else: break finally: self.sched.remove(ev) data = data.strip() self._state = data self.log("RCV", data) return data def receive(self): while True: if self._q: data = self._q.pop() else: data = self._receive() hlist = self.EVENT_DISPATCH.get(data, None) # checks if event type if hlist is None: # not an event return data else: raise _AsyncEvent, (data, hlist) def _run_handlers(self, args): hlist = args[1] if not hlist: self.log("EVENT", args[0]) for handler in hlist: self.log("CALL", handler) handler() if self._autostop: self._state = "IDLE" self.log("STATE", self._state) def send(self, data): while 1: try: self._out.write("%s\n" % (data, )) except EnvironmentError, why: if why.errno == EINTR: continue else: raise else: break os.kill(self._ppid, SIGPIPE) self.log("SND", data)
class CallProgram(object): def __init__(self, logfile=None): self._q = Queue() self._device = None self._autostop = False self._state = "IDLE" self.sched = scheduler.get_scheduler() # FileWrapper protects against interrupted system call self._in = FileWrapper(os.fdopen(int(os.environ["VOICE_INPUT"]), "r", 1)) self._out = FileWrapper(os.fdopen(int(os.environ["VOICE_OUTPUT"]), "w", 0)) self._ppid = int(os.environ["VOICE_PID"]) self._program = os.environ["VOICE_PROGRAM"] # modem login variables self.caller_id = os.environ.get("CALLER_ID") self.caller_name = os.environ.get("CALLER_NAME") self.called_id = os.environ.get("CALLED_ID") self.connectstring = os.environ.get("CONNECT") self._device = os.environ.get("DEVICE") self._log = logfile if self._log: self._log.write("-----------\n### Starting %s\n----------\n" % (self.__class__.__name__,)) self.EVENT_DISPATCH = {} for ename in EVENTS: self.EVENT_DISPATCH[ename] = [] self.chat(['HELLO SHELL', 'HELLO VOICE PROGRAM', 'READY']) # for easy subclass initialization self.initialize() def __del__(self): self.close() def register(self, evname, handler): """Register an event handler. The event name is one from the EVENTS list. The handler should be a callable.""" assert evname in EVENTS, "bad event name" if callable(handler): self.EVENT_DISPATCH[evname].append(handler) else: raise ValueError, "register: handler must be callable" def unregister(self, evname, handler): hlist = self.EVENT_DISPATCH[evname] try: hlist.remove(handler) except ValueError: pass def initialize(self): """For subclasses to add extra initialization.""" pass def log(self, entrytype, entry): if self._log: self._log.write("%s: %r\n" % (entrytype, entry)) def _timeout_cb(self): self._timed_out = 1 def _receive(self): self._timed_out = 0 ev = self.sched.add(60, 0, self._timeout_cb, ()) try: while True: try: data = self._in.readline() except EnvironmentError, why: if why.errno == EINTR: if self._timed_out == 1: raise TimeoutError, "timed out during recieve." else: continue else: raise else: break finally: self.sched.remove(ev) data = data.strip() self._state = data self.log("RCV", data) return data def receive(self): while True: if self._q: data = self._q.pop() else: data = self._receive() hlist = self.EVENT_DISPATCH.get(data, None) # checks if event type if hlist is None: # not an event return data else: raise _AsyncEvent, (data, hlist) def _run_handlers(self, args): hlist = args[1] if not hlist: self.log("EVENT", args[0]) for handler in hlist: self.log("CALL", handler) handler() if self._autostop: self._state = "IDLE" self.log("STATE", self._state) def send(self, data): while 1: try: self._out.write("%s\n" % (data,)) except EnvironmentError, why: if why.errno == EINTR: continue else: raise else: break os.kill(self._ppid, SIGPIPE) self.log("SND", data)