o !^@sRdZddlmZddlmZmZddlmZmZddl Z dgZ GdddeZ dS) azThis is like pexpect, but it will work with any file descriptor that you pass it. You are responsible for opening and close the file descriptor. This allows you to use Pexpect with sockets and named pipes (FIFOs). PEXPECT LICENSE This license is approved by the OSI and FSF as GPL-compatible. http://opensource.org/licenses/isc-license.txt Copyright (c) 2012, Noah Spurrier PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ) SpawnBase)ExceptionPexpectTIMEOUT)select_ignore_interruptspoll_ignore_interruptsNfdspawncsjeZdZdZ  dddZd d Zd d Zdd dZddZddZ ddZ ddZ dfdd Z Z S)rzThis is like pexpect.spawn but allows you to supply your own open file descriptor. For example, you could use it to read through a file looking for patterns, or to control a modem or serial device. NstrictFc Cst|tdkrt|dr|}t|tdkrtdzt|Wn ty/tdwd|_d|_t j |||||||d||_ d|_ d|_ d||_| |_dS) zThis takes a file descriptor (an int) or an object that support the fileno() method (returning an int). All Python file-like objects support fileno(). rfilenozdThe fd argument is not an int. If this is a command string then maybe you want to use pexpect.spawn.z/The fd argument is not a valid file descriptor.N)encoding codec_errorsFz)typehasattrr rosfstatOSErrorargscommandr__init__child_fdown_fdclosednameuse_poll) selffdrtimeoutmaxreadsearchwindowsizelogfiler rrr"3/usr/lib/python3/dist-packages/pexpect/fdpexpect.pyr$s&   zfdspawn.__init__cCs2|jdkrdS|t|jd|_d|_dS)zClose the file descriptor. Calling this method a second time does nothing, but if the file descriptor was closed elsewhere, :class:`OSError` will be raised. NT)rflushrcloserrr"r"r#r&?s   z fdspawn.closecCs.|jdkrdSz t|jWdSYdS)zThis checks if the file descriptor is still valid. If :func:`os.fstat` does not raise an exception then we assume it is alive. r$FT)rrrr'r"r"r#isaliveMs  zfdspawn.isalivecCstd)z1Deprecated and invalid. Just raises an exception.z.This method is not valid for file descriptors.)r)rforcer"r"r# terminateYszfdspawn.terminatecCs4||}||d|jj|dd}t|j|S)z+Write to fd, return number of bytes writtensendF)final)_coerce_send_string_log_encoderencoderwriter)rsbr"r"r#r+`s  z fdspawn.sendcCs||}|||jS)zAWrite to fd with trailing newline, return number of bytes written)r-r+lineseprr2r"r"r#sendlinehs zfdspawn.sendlinecCs||dS)zWrite to fd, return NoneN)r+r5r"r"r#r1msz fdspawn.writecCs|D]}||qdS)z+Call self.write() for each item in sequenceN)r1)rsequencer2r"r"r# writelinesqs zfdspawn.writelinesrr$csptjdkr0|dkr |j}|jg}g}g}|jrt||}n t||||\}}}|j|vr0tdtt | |S)a Read from the file descriptor and return the result as a string. The read_nonblocking method of :class:`SpawnBase` assumes that a call to os.read will not block (timeout parameter is ignored). This is not the case for POSIX file-like objects such as sockets and serial ports. Use :func:`select.select`, timeout is implemented conditionally for POSIX systems. :param int size: Read at most *size* bytes. :param int timeout: Wait timeout seconds for file descriptor to be ready to read. When -1 (default), use self.timeout. When 0, poll. :return: String containing the bytes read posixr$zTimeout exceeded.) rrrrrrrrsuperrread_nonblocking)rsizerrlistwlistxlist __class__r"r#r;vs   zfdspawn.read_nonblocking)Nr r NNNr F)F)rr$)__name__ __module__ __qualname____doc__rr&r(r*r+r6r1r8r; __classcell__r"r"r@r#rs  ) rE spawnbaser exceptionsrrutilsrrr__all__rr"r"r"r#s