Back to index

python3.2  3.2.2
Functions | Variables
quopri Namespace Reference

Functions

def needsquoting
def quote
def encode
def encodestring
def decode
def decodestring
def ishex
def unhex
def main

Variables

list __all__ = ["encode", "decode", "encodestring", "decodestring"]
string ESCAPE = '='
int MAXLINESIZE = 76
string HEX = '0123456789ABCDEF'
string EMPTYSTRING = ''
 a2b_qp = None
 b2a_qp = None

Function Documentation

def quopri.decode (   input,
  output,
  header = False 
)
Read 'input', apply quoted-printable decoding, and write to 'output'.
'input' and 'output' are files with readline() and write() methods.
If 'header' is true, decode underscore as space (per RFC 1522).

Definition at line 119 of file quopri.py.

00119 
00120 def decode(input, output, header=False):
00121     """Read 'input', apply quoted-printable decoding, and write to 'output'.
00122     'input' and 'output' are files with readline() and write() methods.
00123     If 'header' is true, decode underscore as space (per RFC 1522)."""
00124 
00125     if a2b_qp is not None:
00126         data = input.read()
00127         odata = a2b_qp(data, header=header)
00128         output.write(odata)
00129         return
00130 
00131     new = b''
00132     while 1:
00133         line = input.readline()
00134         if not line: break
00135         i, n = 0, len(line)
00136         if n > 0 and line[n-1:n] == b'\n':
00137             partial = 0; n = n-1
00138             # Strip trailing whitespace
00139             while n > 0 and line[n-1:n] in b" \t\r":
00140                 n = n-1
00141         else:
00142             partial = 1
00143         while i < n:
00144             c = line[i:i+1]
00145             if c == b'_' and header:
00146                 new = new + b' '; i = i+1
00147             elif c != ESCAPE:
00148                 new = new + c; i = i+1
00149             elif i+1 == n and not partial:
00150                 partial = 1; break
00151             elif i+1 < n and line[i+1] == ESCAPE:
00152                 new = new + ESCAPE; i = i+2
00153             elif i+2 < n and ishex(line[i+1:i+2]) and ishex(line[i+2:i+3]):
00154                 new = new + bytes((unhex(line[i+1:i+3]),)); i = i+3
00155             else: # Bad escape sequence -- leave it in
00156                 new = new + c; i = i+1
00157         if not partial:
00158             output.write(new + b'\n')
00159             new = b''
00160     if new:
00161         output.write(new)

Here is the call graph for this function:

Here is the caller graph for this function:

def quopri.decodestring (   s,
  header = False 
)

Definition at line 162 of file quopri.py.

00162 
00163 def decodestring(s, header=False):
00164     if a2b_qp is not None:
00165         return a2b_qp(s, header=header)
00166     from io import BytesIO
00167     infp = BytesIO(s)
00168     outfp = BytesIO()
00169     decode(infp, outfp, header=header)
00170     return outfp.getvalue()
00171 
00172 
00173 
# Other helper functions

Here is the call graph for this function:

Here is the caller graph for this function:

def quopri.encode (   input,
  output,
  quotetabs,
  header = False 
)
Read 'input', apply quoted-printable encoding, and write to 'output'.

'input' and 'output' are files with readline() and write() methods.
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.

Definition at line 44 of file quopri.py.

00044 
00045 def encode(input, output, quotetabs, header=False):
00046     """Read 'input', apply quoted-printable encoding, and write to 'output'.
00047 
00048     'input' and 'output' are files with readline() and write() methods.
00049     The 'quotetabs' flag indicates whether embedded tabs and spaces should be
00050     quoted.  Note that line-ending tabs and spaces are always encoded, as per
00051     RFC 1521.
00052     The 'header' flag indicates whether we are encoding spaces as _ as per
00053     RFC 1522.
00054     """
00055 
00056     if b2a_qp is not None:
00057         data = input.read()
00058         odata = b2a_qp(data, quotetabs=quotetabs, header=header)
00059         output.write(odata)
00060         return
00061 
00062     def write(s, output=output, lineEnd=b'\n'):
00063         # RFC 1521 requires that the line ending in a space or tab must have
00064         # that trailing character encoded.
00065         if s and s[-1:] in b' \t':
00066             output.write(s[:-1] + quote(s[-1:]) + lineEnd)
00067         elif s == b'.':
00068             output.write(quote(s) + lineEnd)
00069         else:
00070             output.write(s + lineEnd)
00071 
00072     prevline = None
00073     while 1:
00074         line = input.readline()
00075         if not line:
00076             break
00077         outline = []
00078         # Strip off any readline induced trailing newline
00079         stripped = b''
00080         if line[-1:] == b'\n':
00081             line = line[:-1]
00082             stripped = b'\n'
00083         # Calculate the un-length-limited encoded line
00084         for c in line:
00085             c = bytes((c,))
00086             if needsquoting(c, quotetabs, header):
00087                 c = quote(c)
00088             if header and c == b' ':
00089                 outline.append(b'_')
00090             else:
00091                 outline.append(c)
00092         # First, write out the previous line
00093         if prevline is not None:
00094             write(prevline)
00095         # Now see if we need any soft line breaks because of RFC-imposed
00096         # length limitations.  Then do the thisline->prevline dance.
00097         thisline = EMPTYSTRING.join(outline)
00098         while len(thisline) > MAXLINESIZE:
00099             # Don't forget to include the soft line break `=' sign in the
00100             # length calculation!
00101             write(thisline[:MAXLINESIZE-1], lineEnd=b'=\n')
00102             thisline = thisline[MAXLINESIZE-1:]
00103         # Write out the current line
00104         prevline = thisline
00105     # Write out the last line, without a trailing newline
00106     if prevline is not None:
00107         write(prevline, lineEnd=stripped)

Here is the call graph for this function:

Here is the caller graph for this function:

def quopri.encodestring (   s,
  quotetabs = False,
  header = False 
)

Definition at line 108 of file quopri.py.

00108 
00109 def encodestring(s, quotetabs=False, header=False):
00110     if b2a_qp is not None:
00111         return b2a_qp(s, quotetabs=quotetabs, header=header)
00112     from io import BytesIO
00113     infp = BytesIO(s)
00114     outfp = BytesIO()
00115     encode(infp, outfp, quotetabs, header)
00116     return outfp.getvalue()
00117 
00118 

Here is the call graph for this function:

Here is the caller graph for this function:

def quopri.ishex (   c)
Return true if the byte ordinal 'c' is a hexadecimal digit in ASCII.

Definition at line 174 of file quopri.py.

00174 
00175 def ishex(c):
00176     """Return true if the byte ordinal 'c' is a hexadecimal digit in ASCII."""
00177     assert isinstance(c, bytes)
00178     return b'0' <= c <= b'9' or b'a' <= c <= b'f' or b'A' <= c <= b'F'

Here is the caller graph for this function:

def quopri.main ( void  )

Definition at line 197 of file quopri.py.

00197 
00198 def main():
00199     import sys
00200     import getopt
00201     try:
00202         opts, args = getopt.getopt(sys.argv[1:], 'td')
00203     except getopt.error as msg:
00204         sys.stdout = sys.stderr
00205         print(msg)
00206         print("usage: quopri [-t | -d] [file] ...")
00207         print("-t: quote tabs")
00208         print("-d: decode; default encode")
00209         sys.exit(2)
00210     deco = 0
00211     tabs = 0
00212     for o, a in opts:
00213         if o == '-t': tabs = 1
00214         if o == '-d': deco = 1
00215     if tabs and deco:
00216         sys.stdout = sys.stderr
00217         print("-t and -d are mutually exclusive")
00218         sys.exit(2)
00219     if not args: args = ['-']
00220     sts = 0
00221     for file in args:
00222         if file == '-':
00223             fp = sys.stdin.buffer
00224         else:
00225             try:
00226                 fp = open(file, "rb")
00227             except IOError as msg:
00228                 sys.stderr.write("%s: can't open (%s)\n" % (file, msg))
00229                 sts = 1
00230                 continue
00231         try:
00232             if deco:
00233                 decode(fp, sys.stdout.buffer)
00234             else:
00235                 encode(fp, sys.stdout.buffer, tabs)
00236         finally:
00237             if file != '-':
00238                 fp.close()
00239     if sts:
00240         sys.exit(sts)
00241 
00242 

Here is the call graph for this function:

def quopri.needsquoting (   c,
  quotetabs,
  header 
)
Decide 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.

Definition at line 21 of file quopri.py.

00021 
00022 def needsquoting(c, quotetabs, header):
00023     """Decide whether a particular byte ordinal needs to be quoted.
00024 
00025     The 'quotetabs' flag indicates whether embedded tabs and spaces should be
00026     quoted.  Note that line-ending tabs and spaces are always encoded, as per
00027     RFC 1521.
00028     """
00029     assert isinstance(c, bytes)
00030     if c in b' \t':
00031         return quotetabs
00032     # if header, we have to escape _ because _ is used to escape space
00033     if c == b'_':
00034         return header
00035     return c == ESCAPE or not (b' ' <= c <= b'~')

Here is the caller graph for this function:

def quopri.quote (   c)
Quote a single character.

Definition at line 36 of file quopri.py.

00036 
00037 def quote(c):
00038     """Quote a single character."""
00039     assert isinstance(c, bytes) and len(c)==1
00040     c = ord(c)
00041     return ESCAPE + bytes((HEX[c//16], HEX[c%16]))
00042 
00043 

Here is the call graph for this function:

Here is the caller graph for this function:

def quopri.unhex (   s)
Get the integer value of a hexadecimal number.

Definition at line 179 of file quopri.py.

00179 
00180 def unhex(s):
00181     """Get the integer value of a hexadecimal number."""
00182     bits = 0
00183     for c in s:
00184         c = bytes((c,))
00185         if b'0' <= c <= b'9':
00186             i = ord('0')
00187         elif b'a' <= c <= b'f':
00188             i = ord('a')-10
00189         elif b'A' <= c <= b'F':
00190             i = ord(b'A')-10
00191         else:
00192             assert False, "non-hex digit "+repr(c)
00193         bits = bits*16 + (ord(c) - i)
00194     return bits
00195 
00196 

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

list quopri.__all__ = ["encode", "decode", "encodestring", "decodestring"]

Definition at line 7 of file quopri.py.

quopri.a2b_qp = None

Definition at line 17 of file quopri.py.

quopri.b2a_qp = None

Definition at line 18 of file quopri.py.

Definition at line 12 of file quopri.py.

Definition at line 9 of file quopri.py.

string quopri.HEX = '0123456789ABCDEF'

Definition at line 11 of file quopri.py.

Definition at line 10 of file quopri.py.