def __init__(self, fname=None): self._fd = None self.name = "" self._eventq = Queue() self.idbus = self.idvendor = self.idproduct = self.idversion = None if fname: self._open(fname)
class EventFile(object): """Read from a file containing raw recorded events.""" def __init__(self, fname, mode="r"): self._fo = open(fname, mode) self._eventq = Queue() def read(self, amt=None): # amt not used, provided for compatibility. """Read a single Event object from stream.""" if not self._eventq: if not self._fill(): return None return self._eventq.pop() def readall(self): ev = self.read() while ev: yield ev ev = self.read() def _fill(self): raw = self._fo.read(EVsize * 32) if raw: for i in xrange(len(raw)/EVsize): ev = Event() ev.decode(raw[i*EVsize:(i+1)*EVsize]) self._eventq.push(ev) return True else: return False
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 EventFile(object): """Read from a file containing raw recorded events.""" def __init__(self, fname, mode="r"): self._fo = open(fname, mode) self._eventq = Queue() def read(self, amt=None): # amt not used, provided for compatibility. """Read a single Event object from stream.""" if not self._eventq: if not self._fill(): return None return self._eventq.pop() def readall(self): ev = self.read() while ev: yield ev ev = self.read() def _fill(self): raw = self._fo.read(EVsize * 32) if raw: for i in xrange(len(raw) / EVsize): ev = Event() ev.decode(raw[i * EVsize:(i + 1) * EVsize]) self._eventq.push(ev) return True else: return False
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 EventDevice(object): DEVNAME = None # must match name string of device def __init__(self, fname=None): self._fd = None self.name = "" self._eventq = Queue() self.idbus = self.idvendor = self.idproduct = self.idversion = None if fname: self._open(fname) def __str__(self): if self.idbus is None: self.get_deviceid() return "%s: bus=0x%x, vendor=0x%x, product=0x%x, version=0x%x" % \ (self.name, self.idbus, self.idvendor, self.idproduct, self.idversion) def _fill(self): global EVsize try: raw = os.read(self._fd, EVsize * 32) except EOFError: self.close() else: if raw: for i in range(len(raw)/EVsize): ev = Event() ev.decode(raw[i*EVsize:(i+1)*EVsize]) self._eventq.push(ev) def open(self, start=0): assert self.DEVNAME is not None for d in range(start, 16): try: self._open("/dev/input/event%d" % (d,)) except IOError: pass else: if self.name.startswith(self.DEVNAME): break else: self.close() def _open(self, filename): self._fd = os.open(filename, os.O_RDWR) name = fcntl.ioctl(self._fd, EVIOCGNAME, chr(0) * 256) self.name = name.replace(chr(0), '') def fileno(self): return self._fd def close(self): if self._fd is not None: os.close(self._fd) self._fd = None self.name = "" def read(self): if not self._eventq: self._fill() return self._eventq.pop() def readall(self): ev = self.read() while ev: yield ev ev = self.read() def write(self, evtype, code, value): ev = Event(0.0, evtype, code, value) return os.write(self._fd, ev.encode()) def get_driverversion(self): ver = fcntl.ioctl(self._fd, EVIOCGVERSION, '\x00\x00\x00\x00') return struct.unpack(INT, ver)[0] def get_deviceid(self): ver = fcntl.ioctl(self._fd, EVIOCGID, '\x00\x00\x00\x00\x00\x00\x00\x00') self.idbus, self.idvendor, self.idproduct, self.idversion = struct.unpack(SHORT4, ver) return self.idbus, self.idvendor, self.idproduct, self.idversion def readable(self): return bool(self._fd) def writable(self): return False def priority(self): return False def handle_read_event(self): self._fill() def handle_write_event(self): pass def handle_priority_event(self): pass def handle_hangup_event(self): pass
def __init__(self, fname, mode="r"): self._fo = open(fname, mode) self._eventq = Queue()
class EventDevice(object): DEVNAME = None # must match name string of device def __init__(self, fname=None): self._fd = None self.name = "" self._eventq = Queue() self.idbus = self.idvendor = self.idproduct = self.idversion = None if fname: self._open(fname) def __str__(self): if self.idbus is None: self.get_deviceid() return "%s: bus=0x%x, vendor=0x%x, product=0x%x, version=0x%x" % \ (self.name, self.idbus, self.idvendor, self.idproduct, self.idversion) def _fill(self): global EVsize try: raw = os.read(self._fd, EVsize * 32) except EOFError: self.close() else: if raw: for i in range(len(raw) / EVsize): ev = Event() ev.decode(raw[i * EVsize:(i + 1) * EVsize]) self._eventq.push(ev) def open(self, start=0): assert self.DEVNAME is not None for d in range(start, 16): try: self._open("/dev/input/event%d" % (d, )) except IOError: pass else: if self.name.startswith(self.DEVNAME): break else: self.close() def _open(self, filename): self._fd = os.open(filename, os.O_RDWR) name = fcntl.ioctl(self._fd, EVIOCGNAME, chr(0) * 256) self.name = name.replace(chr(0), '') def fileno(self): return self._fd def close(self): if self._fd is not None: os.close(self._fd) self._fd = None self.name = "" def read(self): if not self._eventq: self._fill() return self._eventq.pop() def readall(self): ev = self.read() while ev: yield ev ev = self.read() def write(self, evtype, code, value): ev = Event(0.0, evtype, code, value) return os.write(self._fd, ev.encode()) def get_driverversion(self): ver = fcntl.ioctl(self._fd, EVIOCGVERSION, '\x00\x00\x00\x00') return struct.unpack(INT, ver)[0] def get_deviceid(self): ver = fcntl.ioctl(self._fd, EVIOCGID, '\x00\x00\x00\x00\x00\x00\x00\x00') self.idbus, self.idvendor, self.idproduct, self.idversion = struct.unpack( SHORT4, ver) return self.idbus, self.idvendor, self.idproduct, self.idversion def readable(self): return bool(self._fd) def writable(self): return False def priority(self): return False def handle_read_event(self): self._fill() def handle_write_event(self): pass def handle_priority_event(self): pass def handle_hangup_event(self): pass
class EventDevice(object): DEVNAME = None # must match name string of device def __init__(self, fname=None): self._fd = None self.name = "" self._eventq = Queue() self.idbus = self.idvendor = self.idproduct = self.idversion = None if fname: self.open(fname) self.initialize() def __str__(self): if self.idbus is None: self.get_deviceid() return "%s: bus=0x%x, vendor=0x%x, product=0x%x, version=0x%x\n %s" % \ (self.name, self.idbus, self.idvendor, self.idproduct, self.idversion, self.get_features()) def _fill(self): global EVsize try: raw = os.read(self._fd, EVsize * 32) except EOFError: self.close() else: if raw: for i in range(len(raw) / EVsize): ev = Event() ev.decode(raw[i * EVsize:(i + 1) * EVsize]) self._eventq.push(ev) def find(self, start=0, name=None): name = name or self.DEVNAME assert name is not None, "EventDevice: no name to find" for d in range(start, 16): filename = "/dev/input/event%d" % (d, ) if os.path.exists(filename): try: self.open(filename) except (OSError, IOError): # probably no permissions pass else: if name in self.name: return self.close() raise IOError("Input device %r not found." % (name, )) def open(self, filename): self._fd = os.open(filename, os.O_RDWR) name = fcntl.ioctl(self._fd, EVIOCGNAME, chr(0) * 256) self.name = name.replace(chr(0), '') def fileno(self): return self._fd def close(self): if self._fd is not None: os.close(self._fd) self._fd = None self.name = "" def read(self): if not self._eventq: self._fill() return self._eventq.pop() def readall(self): ev = self.read() while ev: yield ev ev = self.read() def write(self, evtype, code, value): ev = Event(0.0, evtype, code, value) return os.write(self._fd, ev.encode()) def get_driverversion(self): ver = fcntl.ioctl(self._fd, EVIOCGVERSION, '\x00\x00\x00\x00') return struct.unpack(INT, ver)[0] def get_deviceid(self): ver = fcntl.ioctl(self._fd, EVIOCGID, '\x00\x00\x00\x00\x00\x00\x00\x00') self.idbus, self.idvendor, self.idproduct, self.idversion = struct.unpack( SHORT4, ver) return self.idbus, self.idvendor, self.idproduct, self.idversion def get_features(self): caps = fcntl.ioctl(self._fd, EVIOCGBIT(0), '\x00\x00\x00\x00') caps = struct.unpack(INT, caps)[0] return Features(caps) def readable(self): return bool(self._fd) def writable(self): return False def priority(self): return False def read_handler(self): self._fill() def write_handler(self): pass def pri_handler(self): pass def hangup_handler(self): pass def initialize(self): pass
class EventDevice(object): DEVNAME = None # must match name string of device def __init__(self, fname=None): self._fd = None self.name = "" self._eventq = Queue() self.idbus = self.idvendor = self.idproduct = self.idversion = None if fname: self.open(fname) self.initialize() def __str__(self): if self.idbus is None: self.get_deviceid() return "%s: bus=0x%x, vendor=0x%x, product=0x%x, version=0x%x\n %s" % \ (self.name, self.idbus, self.idvendor, self.idproduct, self.idversion, self.get_features()) def _fill(self): global EVsize try: raw = os.read(self._fd, EVsize * 32) except EOFError: self.close() else: if raw: for i in range(len(raw)/EVsize): ev = Event() ev.decode(raw[i*EVsize:(i+1)*EVsize]) self._eventq.push(ev) def find(self, start=0, name=None): name = name or self.DEVNAME assert name is not None, "EventDevice: no name to find" for d in range(start, 16): filename = "/dev/input/event%d" % (d,) if os.path.exists(filename): try: self.open(filename) except (OSError, IOError): # probably no permissions pass else: if name in self.name: return self.close() raise IOError("Input device %r not found." % (name,)) def open(self, filename): self._fd = os.open(filename, os.O_RDWR) name = fcntl.ioctl(self._fd, EVIOCGNAME, chr(0) * 256) self.name = name.replace(chr(0), '') def fileno(self): return self._fd def close(self): if self._fd is not None: os.close(self._fd) self._fd = None self.name = "" def read(self): if not self._eventq: self._fill() return self._eventq.pop() def readall(self): ev = self.read() while ev: yield ev ev = self.read() def write(self, evtype, code, value): ev = Event(0.0, evtype, code, value) return os.write(self._fd, ev.encode()) def get_driverversion(self): ver = fcntl.ioctl(self._fd, EVIOCGVERSION, '\x00\x00\x00\x00') return struct.unpack(INT, ver)[0] def get_deviceid(self): ver = fcntl.ioctl(self._fd, EVIOCGID, '\x00\x00\x00\x00\x00\x00\x00\x00') self.idbus, self.idvendor, self.idproduct, self.idversion = struct.unpack(SHORT4, ver) return self.idbus, self.idvendor, self.idproduct, self.idversion def get_features(self): caps = fcntl.ioctl(self._fd, EVIOCGBIT(0), '\x00\x00\x00\x00') caps = struct.unpack(INT, caps)[0] return Features(caps) def readable(self): return bool(self._fd) def writable(self): return False def priority(self): return False def read_handler(self): self._fill() def write_handler(self): pass def pri_handler(self): pass def hangup_handler(self): pass def initialize(self): pass
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)