Domanda

I am wondering if it is possible to get a script to grab the "remaining time" in the "timeout counter" or whatever is used internally by pexpect:

index, match_obj, text = session.expect(["New Software Release is ready -- Version"], timeout=int(DOWNLOAD_TIMEOUT))
if match_obj:
    logger.info('Download to primary card complete.')
    logger.debug('Download took %d seconds', SOMETHINGGOESHERE)
else:
    logger.critical('Download to primary card took too long.')
    quit(session)

For the life of me, I can't seem to find any attempt to do this anywhere here or via Google :)

I assume I can do something more complicated where I keep track of my own timer, but I was hoping for a simpler solution, given that pexpect is already keeping track of a time value, in seconds.

È stato utile?

Soluzione

pexpect source code shows that the remaining time is kept in a local variable therefore it is not accessible outside of the function after it returns.

It is not complicated to keep track of the remaining time yourself:

end_time = time.time() + timeout
index = child.expect([..], timeout=timeout)
remaining_time = end_time - time.time()

If you need to do it multiple times, you could subclass pexpect.spawn class and override expect() method to return the remaining time together with the index.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top