@sdZddddgZdZdZdZdZyd d lmZmZWne k rjd Zd ZYnXd d Z ddZ dddZ ddddZ dddZdddZddZddZddZedkrend S)zHConversions to/from quoted-printable transport encoding as per RFC 1521.encodedecode encodestring decodestring=Ls0123456789ABCDEF)a2b_qpb2a_qpNcCs\t|tst|dkr%|S|dkr5|S|tkp[d|koXdkn S)zDecide whether a particular byte ordinal needs to be quoted. The 'quotetabs' flag indicates whether embedded tabs and spaces should be quoted. Note that line-ending tabs and spaces are always encoded, as per RFC 1521. s _ ~) isinstancebytesAssertionErrorESCAPE)c quotetabsheaderr/usr/lib/python3.4/quopri.py needsquotings   rcCsWt|tr!t|dks'tt|}ttt|dt|dfS)zQuote a single character.)rrlenrordrHEX)rrrrquote$s' rFc Cstdk rA|j}t|d|d|}|j|dS|ddd}d}x>|j}|srPng} d} |d ddkr|dd}d} nxm|D]e} t| f} t| ||rt| } n|r | d kr | jd q| j| qW|dk r6||ntj | } xHt | t kr|| dt dd d | t dd} qHW| }q\W|dk r||d | ndS)avRead 'input', apply quoted-printable encoding, and write to 'output'. 'input' and 'output' are binary file objects. The 'quotetabs' flag indicates whether embedded tabs and spaces should be quoted. Note that line-ending tabs and spaces are always encoded, as per RFC 1521. The 'header' flag indicates whether we are encoding spaces as _ as per RFC 1522.Nrrs cSs|rN|dddkrN|j|ddt|dd|n7|dkrt|jt||n|j||dS)Nrs .rr)writer)soutputlineEndrrrr ;s 2 zencode..writerrr r r#s= rr) r readr readlinerrrappend EMPTYSTRINGjoinr MAXLINESIZE) inputr"rrdataodatar ZprevlinelineZoutlineZstrippedrZthislinerrrr,s@           cCsdtdk r"t|d|d|Sddlm}||}|}t|||||jS)Nrrr)BytesIO)r ior.rgetvalue)r!rrr.infpoutfprrrrjs   c Cstdk r;|j}t|d|}|j|dSd}x*|j}|sZPndt|}}|dkr||d|dkrd} |d}x=|dkr||d|dkr|d}qWnd} xd||krI|||d} | dkr/|r/|d }|d}q| tkrR|| }|d}q|d|krs| rsd} Pq|d|kr||dtkr|t}|d }q|d |kr2t||d|d r2t||d |d r2|tt||d|d f}|d }q|| }|d}qW| sD|j|dd}qDqDW|r|j|ndS) zRead 'input', apply quoted-printable decoding, and write to 'output'. 'input' and 'output' are binary file objects. If 'header' is true, decode underscore as space (per RFC 1522).Nrrrrs s r r ) r r$r r%rrishexrunhex) r*r"rr+r,newr-inpartialrrrrrusR    & )   $ L+   cCs^tdk rt|d|Sddlm}||}|}t||d||jS)Nrr)r.)r r/r.rr0)r!rr.r1r2rrrrs   cCset|tstd|ko,dknpdd|koHdknpdd|kobdkSS)zDReturn true if the byte ordinal 'c' is a hexadecimal digit in ASCII.09afAF)rrr)rrrrr5sr5cCsd}x|D]}t|f}d|ko9dknrMtd}nzd|koddknr|tdd}nKd |kod knrtd d}nd std t||d t||}q W|S)z.Get the integer value of a hexadecimal number.rr;r<0r=r>a r?r@Fznon-hex digit r)rrrrepr)r!bitsrr8rrrr6s r6c (Cs;ddl}ddl}y)|j|jddd\}}Wni|jk r}zF|j|_t|tdtdtd|jdWYdd}~XnXd}d}x>|D]6\}}|dkrd}n|d krd}qqW|r,|r,|j|_td |jdn|s>d g}nd} x|D]} | d krl|jj } n^yt | d } WnHt k r}z(|jj d | |fd} wKWYdd}~XnXz6|rt | |jj nt| |jj |Wd| d kr| jnXqKW| r7|j| ndS)NrrZtdz"usage: quopri [-t | -d] [file] ...z-t: quote tabsz-d: decode; default encoder3z-tz-dz -t and -d are mutually exclusive-rbz%s: can't open (%s) )sysgetoptargverrorstderrstdoutprintexitstdinbufferopenOSErrorr rrclose) rHrIZoptsargsmsgZdecoZtabsorBstsfilefprrrmainsT  )               r[__main__)__doc____all__rr)rr'Zbinasciir r ImportErrorrrrrrrr5r6r[__name__rrrrs*    > +   .