def RunCommand(self, cmdline, env=None, timeout=None, logfile=None): """Run an external command. This method will block until the command returns. An optional timeout may be supplied to prevent hanging forever. Arguments: A string that is the command line to be run. A (optional) dictionary containing the environment variables. An (optional) timeout value that will forcibly return if the call takes longer than the timeout value. Returns: A tuple of ExitStatus object and stdout/stderr (string) of the program. """ from pycopia import proctools p = proctools.spawnpipe(cmdline, logfile=logfile, env=env) try: if timeout: sched = scheduler.get_scheduler() text = sched.iotimeout(p.read, timeout=timeout) else: text = p.read() finally: p.wait() p.close() return p.exitstatus, text
def remove_alarm(self, handle): """ Remove an alarm. Returns True if the alarm exists, False otherwise """ return scheduler.get_scheduler().remove(handle)
def __init__(self, fo=None, prompt="$", timeout=90.0, logfile=None, engine=None): if hasattr(fo, "fileno"): self._fo = fo try: # for Process objects. This needs to catch EINTR for timeouts. self._fo.restart(0) except AttributeError: pass else: raise ValueError, "Expect: first parameter not a file-like object." self.default_timeout = timeout self._log = logfile self.cmd_interp = None self.prompt = prompt self._patt_cache = {} self._buf = '' self.eof = 0 self.sched = scheduler.get_scheduler() self._engine = engine self.expectindex = -1 # if a match on a list occurs, the index in the list
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()
def Timed(self, function, args=(), kwargs={}, timeout=30): """Run a function with a failsafe timer. Call the provided function with a failsafe timeout value. The function will be interrupted if it takes longer than `timeout` seconds. """ sched = scheduler.get_scheduler() return sched.timeout(function, args, kwargs, timeout)
def Timedio(self, function, args=(), kwargs={}, timeout=30): """Run a function that may block on I/O with a failsafe timer. Call the provided function with a failsafe timeout value. The function will be interrupted if it takes longer than `timeout` seconds. The method should be one that blocks on I/O. """ sched = scheduler.get_scheduler() return sched.iotimeout(function, args, kwargs, timeout)
def alarm(self, seconds, callback): """ Call callback() given time from from now. No parameters are passed to callback. Returns a handle that may be passed to remove_alarm() seconds -- floating point time to wait before calling callback callback -- function to call from event loop """ return scheduler.get_scheduler().add(seconds, callback=callback)
def __init__(self, fo=None, prompt="$", timeout=90.0, logfile=None, engine=None): if hasattr(fo, "fileno"): self._fo = fo try: # for Process objects. This needs to catch EINTR for timeouts. self._fo.restart(0) except AttributeError: pass else: raise ValueError("Expect: first parameter not a file-like object.") self.default_timeout = timeout self._log = logfile self.cmd_interp = None self._prompt = prompt.encode() self._patt_cache = {} self._buf = '' self.eof = 0 self.sched = scheduler.get_scheduler() self._engine = engine self.expectindex = -1 # if a match on a list occurs, the index in the list
os.unlink(_get_path()) if __name__ == "__main__": from pycopia import reports from pycopia import scheduler rpt = reports.get_report( ("StandardReport", "-", "text/plain") ) rx = ReportReceiver(rpt) tx = RemoteReport() def txstepper(tx): yield tx.initialize() yield tx.add_title("The Title") yield tx.add_heading("Some heading") yield tx.info("some info") yield tx.passed("A message for a passed condition.") yield tx.finalize() scheduler.get_scheduler().add(0.1, callback=txstepper(tx).next, repeat=True) try: try: asyncio.poller.loop() except StopIteration: pass finally: tx.close() rx.close()
def loop(self, timeout=-1.0, callback=NULL): while self._procs: poller.poll(timeout) callback(self) if scheduler.get_scheduler(): # wait for any restarts scheduler.sleep(1.5)
def timedio(self, function, args=(), kwargs={}, timeout=30): """Call a method with a failsafe timeout value.""" sched = scheduler.get_scheduler() return sched.iotimeout(function, args, kwargs, timeout)