ó ùóÀPc@súdZddlZddlZddlZddlZddlZddlZddlZddlm Z m Z ddl Z ddl m Z defd„ƒYZdefd„ƒYZd efd „ƒYZd efd „ƒYZd „Zd„ZdS(sÃThis module implements PrintEngine (abstract), POSIXPrintEngine and LoggingPrintEngine, which take output messages from e.g. a progress tracker and render them to a file, a terminal, or a logger.iÿÿÿÿN(tABCMetatabstractmethod(t PipeErrortPrintEngineExceptioncBseZdZd„ZRS(s7Exception indicating the failure to create PrintEngine.cCsddj|jƒS(NsPrintEngineException: %st (tjointargs(tself((s</usr/lib/python2.7/vendor-packages/pkg/client/printengine.pyt__str__.s(t__name__t __module__t__doc__R(((s</usr/lib/python2.7/vendor-packages/pkg/client/printengine.pyR,st PrintEnginecBsJeZdZeZd„Zed„ƒZed„ƒZed„ƒZ RS(s?Abstract class defining what a PrintEngine must know how to do.cCsdS(N((R((s</usr/lib/python2.7/vendor-packages/pkg/client/printengine.pyt__init__5scCsdS(s1Returns true if out_file is 'slow' (<=9600 baud).N((R((s</usr/lib/python2.7/vendor-packages/pkg/client/printengine.pytisslow8scOsdS(s²Core print routine. Must act basically like py3k's print routine. For some printengines, additional behaviors can be indicated via keyword args.N((RRtkwargs((s</usr/lib/python2.7/vendor-packages/pkg/client/printengine.pytcprint=scCsdS(sMake the terminal or line ready for output by another subsystem. This commonly might entail issuing a newline.N((R((s</usr/lib/python2.7/vendor-packages/pkg/client/printengine.pytflushDs( R R R Rt __metaclass__R RRRR(((s</usr/lib/python2.7/vendor-packages/pkg/client/printengine.pyR 1s  tPOSIXPrintEnginecBsDeZdZd„Zd„Zd„Zd„Zd„Zd„ZRS(snThis is an engine for printing output to the end user which has been tweaked for IPS's printing needs.cCsûtj|ƒ||_d|_d|_d|_||_|jsGdStj dƒ|_ d|_ |jj ƒs€t dƒ‚nyAtjd|jjƒƒtjdƒ|_tjdƒ|_ Wn3tjk röt dtjjdd ƒƒ‚nXdS( søCreate a printengine. out_file -- the file object to print to ttymode -- Boolean indicating need for tty support. Throws PrintEngineException if out_file can't support. iNs \$<[0-9]+>s Not a TTYtcrtelsUnknown terminal '%s'tTERMt(R R t _out_filet!_POSIXPrintEngine__nchars_printedt_POSIXPrintEngine__needs_nltNonet_POSIXPrintEngine__crt_POSIXPrintEngine__ttymodetretcompilet_POSIXPrintEngine__putp_ret_POSIXPrintEngine__eltisattyRtcursest setuptermtfilenottigetstrterrortostenvirontget(Rtout_filetttymode((s</usr/lib/python2.7/vendor-packages/pkg/client/printengine.pyR Os&        cCs2|jst‚|jj|jjd|ƒƒdS(sThis routine loosely emulates python's curses.putp, but works on whatever our output file is, instead just stdoutRN(RtAssertionErrorRtwriteR tsub(Rtstring((s</usr/lib/python2.7/vendor-packages/pkg/client/printengine.pytputpnscCsGtj}ytj|jƒd}Wntjk r9nX|tjkS(s1Returns true if out_file is 'slow' (<=9600 baud).i(ttermiostB38400t tcgetattrRR'tB9600(Rtb((s</usr/lib/python2.7/vendor-packages/pkg/client/printengine.pyRys  cCsw|jr<|j|jƒ|j|jƒ|j|jƒn7|j|jƒ|jj|jdƒ|j|jƒdS(s5Send sequence to erase the current line to _out_file.RN(R!R1RRR.R(R((s</usr/lib/python2.7/vendor-packages/pkg/client/printengine.pyterase‚s cOsN|jddƒ}|j|ƒ|jddƒ}|jdƒre|jsOt‚|jƒd|_n|dkr|jdƒ rt|_n|j dƒ}|dkrÀ|jt |ƒ7_nt |ƒ|d |_y<|j j |ƒ|j j ƒ|jdƒrt|_nWn4tk rI}|jtjkrCt|‚n‚nXd S( söCore print routine. Acts largely like py3k's print command, (supports 'sep' and 'end' kwargs) with an extension: erase=true: Erase any content on the present line, intended for use in overwriting.tsepRtends R7iRiÿÿÿÿiN(R*RRR-R7RtendswithtTrueRtrfindtlenRR.RtFalsetIOErrorterrnotEPIPER(RRRR8toutmsgtnposte((s</usr/lib/python2.7/vendor-packages/pkg/client/printengine.pyRs*      cCsNy6|jr(|jjdƒt|_n|jjƒWntk rInXdS(s…If we're in the middle of writing a line, this tries to write a newline in order to allow clean output after flush().s N(RRR.R>RR?(R((s</usr/lib/python2.7/vendor-packages/pkg/client/printengine.pyRÃs   ( R R R R R1RR7RR(((s</usr/lib/python2.7/vendor-packages/pkg/client/printengine.pyRKs  4tLoggingPrintEnginecBs2eZdZd„Zd„Zd„Zd„ZRS(s;This class adapts a printengine such that it issues its output to a python logger from the logging module. Note that This class is used by the AI (install) engine. The basic trick here is to use a StringIO in place of an actual file. We then have the POSIX print engine issue its I/O to the StringIO, then splitlines() the buffer and see if there are any complete lines that we can output. If so, each complete line is issued to the logger, and any remainder is put back into the StringIO for subsequent display.cCsGtj|ƒ||_||_tjƒ|_t|jtƒ|_dS(N( R R t_loggert _logleveltStringIOt _stringioRR>t_pxpe(Rtloggertloglevel((s</usr/lib/python2.7/vendor-packages/pkg/client/printengine.pyR Ûs    cCstS(s1Returns true if out_file is 'slow' (<=9600 baud).(R>(R((s</usr/lib/python2.7/vendor-packages/pkg/client/printengine.pyRâscOs´|jj||Ž|jjƒjtƒ}d}x:|D]2}|jdƒr8|jj|j |d ƒq8q8W|jj dƒ|jj dƒ|jdƒs°|jj |ƒndS(sqAccumulates output into a buffer, emitting messages to the _logger when full lines are available.Rs iÿÿÿÿiN( RJRRItgetvaluet splitlinesR;R:RFtlogRGtseekttruncateR.(RRRtlinestline((s</usr/lib/python2.7/vendor-packages/pkg/client/printengine.pyRæs !cCsd|jjƒ}|r@d|ks't‚|jj|j|ƒn|jjdƒ|jjdƒdS(s$Log any partial line we've got left.s iN(RIRMR-RFRORGRPRQ(Rtval((s</usr/lib/python2.7/vendor-packages/pkg/client/printengine.pyR÷s (R R R R RRR(((s</usr/lib/python2.7/vendor-packages/pkg/client/printengine.pyREÐs     cCsµtjdƒ}tj|ƒ}|j|ƒt|tjƒ}|jdddƒ|jdddƒ|jdƒ|jdƒ|jdƒ|jd ddƒ|jƒ|jƒd S( sòTest driver for logging print engine. This is maintained as a standalone function in order to support the 'runprintengine' test utility in $SRC/tests/interactive/runprintengine.py. It is also called by the test suite.ttestsTesting logging print engine. R9RsDid you see this? s And this?sCIf the previous three sentences are on the same line, it's working.s-You need to see one more line after this one.s1This should be the last line, printed by flushingN( tloggingt getLoggert StreamHandlert addHandlerREtWARNINGRRR(t output_fileRKtchtpe((s</usr/lib/python2.7/vendor-packages/pkg/client/printengine.pyttest_logging_printengines     cCs(t|d|ƒ}d}d}|rQtjdƒp6d}tjdƒpKd}n|jd|ƒ|s|jdƒ|jdƒ|jƒdStjdƒp¢d}tjdƒp·d}|jd ƒ|jd ƒ|jd d dƒ|jdd tƒ|jdƒ|jdƒxÖtddƒD]Å}xqtdddƒD]]}|jd|d td dƒ|j|ƒ|jdd dƒ|j|ƒt j dƒq=WxHtdddƒD]4}|jd|dd td dƒt j dƒq±Wq$W|jdd tƒ|jdƒ|jdd dƒt j dƒ|jdd td dƒt j dƒ|jdd dd tƒt j dƒ|jdd tƒt j dƒ|jdd dƒt j dƒ|jdd tƒ|jdd dƒ|jƒ|jd ƒ|jdƒ|jƒ|jd!ƒ|j ƒdS("sðTest driver for POSIX print engine. This is maintained as a standalone function in order to support the 'runprintengine' test utility in $SRC/tests/interactive/runprintengine.py; it is also called by the test suite.R,Rtsmsotsgr0s*Testing POSIX print engine; ttymode is %s stesting 1 2 3stesting flush (2)Ns,Now we'll print something and then erase it;s,you should see a blank line below this line.s(IF YOU CAN SEE THIS, THE TEST HAS FAILEDR9R7s1You should see an X swishing back and forth; froms#left to right it should be inverse.iiiiRtXgš™™™™™©?iÿÿÿÿstesting XX XX XXgà?s!testing 4 5 6 testing XX XX XXs testing YY YYs!testing 7 8 9 testing 10 11 12stesting ZZ ZZ ZZ ZZ ZZstesting 13 14 15stesting flush...s This should be on the next line.s;This should be on the next line (with no nl's intervening).( RR#R&RRR;trangetxrangeR1ttimetsleepR(R[R,R]tstandoutR`tytx((s</usr/lib/python2.7/vendor-packages/pkg/client/printengine.pyttest_posix_printenginesd         !           (R R#R@RVR(RR2RdtabcRRRHtpkg.miscRt ExceptionRtobjectR RRER^Ri(((s</usr/lib/python2.7/vendor-packages/pkg/client/printengine.pyts         …2