Mercurial > cpython-lru_cache-weakref
annotate Lib/quopri.py @ 79675:c0a2294de787 2.7
Issue #16199: Backport 2to3 typo fix from 3.2.
| author | Chris Jerdonek <chris.jerdonek@gmail.com> |
|---|---|
| date | Thu, 11 Oct 2012 16:15:46 -0700 |
| parents | 805be7c0e26c |
| children | 6b18a095c2e7 |
| rev | line source |
|---|---|
|
5772
5f7ddb7ca347
Added /usr/bin/env python and made executable.
Guido van Rossum <guido@python.org>
parents:
5442
diff
changeset
|
1 #! /usr/bin/env python |
|
5f7ddb7ca347
Added /usr/bin/env python and made executable.
Guido van Rossum <guido@python.org>
parents:
5442
diff
changeset
|
2 |
|
18157
4d1c78493221
Better support for RFC 1521 quoted-printable specification, along with
Barry Warsaw <barry@python.org>
parents:
17158
diff
changeset
|
3 """Conversions to/from quoted-printable transport encoding as per RFC 1521.""" |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
4 |
| 2742 | 5 # (Dec 1991 version). |
| 6 | |
|
18157
4d1c78493221
Better support for RFC 1521 quoted-printable specification, along with
Barry Warsaw <barry@python.org>
parents:
17158
diff
changeset
|
7 __all__ = ["encode", "decode", "encodestring", "decodestring"] |
|
16562
623456576d6a
__all__ for several more modules
Skip Montanaro <skip@pobox.com>
parents:
15870
diff
changeset
|
8 |
| 2742 | 9 ESCAPE = '=' |
| 10 MAXLINESIZE = 76 | |
| 11 HEX = '0123456789ABCDEF' | |
|
18157
4d1c78493221
Better support for RFC 1521 quoted-printable specification, along with
Barry Warsaw <barry@python.org>
parents:
17158
diff
changeset
|
12 EMPTYSTRING = '' |
| 2742 | 13 |
|
20187
8e4e49105595
Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents:
19694
diff
changeset
|
14 try: |
|
20240
5c4261e74478
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
20187
diff
changeset
|
15 from binascii import a2b_qp, b2a_qp |
|
22477
805be7c0e26c
import statements only raise ImportError, right?
Skip Montanaro <skip@pobox.com>
parents:
20435
diff
changeset
|
16 except ImportError: |
|
20240
5c4261e74478
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
20187
diff
changeset
|
17 a2b_qp = None |
|
5c4261e74478
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
20187
diff
changeset
|
18 b2a_qp = None |
|
18157
4d1c78493221
Better support for RFC 1521 quoted-printable specification, along with
Barry Warsaw <barry@python.org>
parents:
17158
diff
changeset
|
19 |
|
18358
56652e684737
Whitespace normalization; the plat-riscos file didn't even get by
Tim Peters <tim.peters@gmail.com>
parents:
18169
diff
changeset
|
20 |
|
20187
8e4e49105595
Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents:
19694
diff
changeset
|
21 def needsquoting(c, quotetabs, header): |
|
15031
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
22 """Decide whether a particular character needs to be quoted. |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
23 |
|
18157
4d1c78493221
Better support for RFC 1521 quoted-printable specification, along with
Barry Warsaw <barry@python.org>
parents:
17158
diff
changeset
|
24 The 'quotetabs' flag indicates whether embedded tabs and spaces should be |
|
4d1c78493221
Better support for RFC 1521 quoted-printable specification, along with
Barry Warsaw <barry@python.org>
parents:
17158
diff
changeset
|
25 quoted. Note that line-ending tabs and spaces are always encoded, as per |
|
4d1c78493221
Better support for RFC 1521 quoted-printable specification, along with
Barry Warsaw <barry@python.org>
parents:
17158
diff
changeset
|
26 RFC 1521. |
|
4d1c78493221
Better support for RFC 1521 quoted-printable specification, along with
Barry Warsaw <barry@python.org>
parents:
17158
diff
changeset
|
27 """ |
|
4d1c78493221
Better support for RFC 1521 quoted-printable specification, along with
Barry Warsaw <barry@python.org>
parents:
17158
diff
changeset
|
28 if c in ' \t': |
|
4d1c78493221
Better support for RFC 1521 quoted-printable specification, along with
Barry Warsaw <barry@python.org>
parents:
17158
diff
changeset
|
29 return quotetabs |
|
20187
8e4e49105595
Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents:
19694
diff
changeset
|
30 # if header, we have to escape _ because _ is used to escape space |
|
20240
5c4261e74478
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
20187
diff
changeset
|
31 if c == '_': |
|
20187
8e4e49105595
Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents:
19694
diff
changeset
|
32 return header |
|
18157
4d1c78493221
Better support for RFC 1521 quoted-printable specification, along with
Barry Warsaw <barry@python.org>
parents:
17158
diff
changeset
|
33 return c == ESCAPE or not (' ' <= c <= '~') |
| 2742 | 34 |
| 35 def quote(c): | |
|
15031
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
36 """Quote a single character.""" |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
37 i = ord(c) |
|
19694
3716b116fb31
The first batch of changes recommended by the fixdiv tool. These are
Guido van Rossum <guido@python.org>
parents:
18358
diff
changeset
|
38 return ESCAPE + HEX[i//16] + HEX[i%16] |
| 2742 | 39 |
|
18157
4d1c78493221
Better support for RFC 1521 quoted-printable specification, along with
Barry Warsaw <barry@python.org>
parents:
17158
diff
changeset
|
40 |
|
18358
56652e684737
Whitespace normalization; the plat-riscos file didn't even get by
Tim Peters <tim.peters@gmail.com>
parents:
18169
diff
changeset
|
41 |
|
20187
8e4e49105595
Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents:
19694
diff
changeset
|
42 def encode(input, output, quotetabs, header = 0): |
|
15031
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
43 """Read 'input', apply quoted-printable encoding, and write to 'output'. |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
44 |
|
15031
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
45 'input' and 'output' are files with readline() and write() methods. |
|
18157
4d1c78493221
Better support for RFC 1521 quoted-printable specification, along with
Barry Warsaw <barry@python.org>
parents:
17158
diff
changeset
|
46 The 'quotetabs' flag indicates whether embedded tabs and spaces should be |
|
4d1c78493221
Better support for RFC 1521 quoted-printable specification, along with
Barry Warsaw <barry@python.org>
parents:
17158
diff
changeset
|
47 quoted. Note that line-ending tabs and spaces are always encoded, as per |
|
4d1c78493221
Better support for RFC 1521 quoted-printable specification, along with
Barry Warsaw <barry@python.org>
parents:
17158
diff
changeset
|
48 RFC 1521. |
|
20187
8e4e49105595
Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents:
19694
diff
changeset
|
49 The 'header' flag indicates whether we are encoding spaces as _ as per |
|
8e4e49105595
Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents:
19694
diff
changeset
|
50 RFC 1522. |
|
18157
4d1c78493221
Better support for RFC 1521 quoted-printable specification, along with
Barry Warsaw <barry@python.org>
parents:
17158
diff
changeset
|
51 """ |
|
20187
8e4e49105595
Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents:
19694
diff
changeset
|
52 |
|
8e4e49105595
Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents:
19694
diff
changeset
|
53 if b2a_qp is not None: |
|
8e4e49105595
Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents:
19694
diff
changeset
|
54 data = input.read() |
|
8e4e49105595
Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents:
19694
diff
changeset
|
55 odata = b2a_qp(data, quotetabs = quotetabs, header = header) |
|
8e4e49105595
Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents:
19694
diff
changeset
|
56 output.write(odata) |
|
8e4e49105595
Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents:
19694
diff
changeset
|
57 return |
|
20240
5c4261e74478
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
20187
diff
changeset
|
58 |
|
18157
4d1c78493221
Better support for RFC 1521 quoted-printable specification, along with
Barry Warsaw <barry@python.org>
parents:
17158
diff
changeset
|
59 def write(s, output=output, lineEnd='\n'): |
|
4d1c78493221
Better support for RFC 1521 quoted-printable specification, along with
Barry Warsaw <barry@python.org>
parents:
17158
diff
changeset
|
60 # RFC 1521 requires that the line ending in a space or tab must have |
|
4d1c78493221
Better support for RFC 1521 quoted-printable specification, along with
Barry Warsaw <barry@python.org>
parents:
17158
diff
changeset
|
61 # that trailing character encoded. |
|
4d1c78493221
Better support for RFC 1521 quoted-printable specification, along with
Barry Warsaw <barry@python.org>
parents:
17158
diff
changeset
|
62 if s and s[-1:] in ' \t': |
|
4d1c78493221
Better support for RFC 1521 quoted-printable specification, along with
Barry Warsaw <barry@python.org>
parents:
17158
diff
changeset
|
63 output.write(s[:-1] + quote(s[-1]) + lineEnd) |
|
20435
5000b5d4c326
Patch 471400: escape single-dot lines; by Jason Hildebrand.
Guido van Rossum <guido@python.org>
parents:
20240
diff
changeset
|
64 elif s == '.': |
|
5000b5d4c326
Patch 471400: escape single-dot lines; by Jason Hildebrand.
Guido van Rossum <guido@python.org>
parents:
20240
diff
changeset
|
65 output.write(quote(s) + lineEnd) |
|
18157
4d1c78493221
Better support for RFC 1521 quoted-printable specification, along with
Barry Warsaw <barry@python.org>
parents:
17158
diff
changeset
|
66 else: |
|
4d1c78493221
Better support for RFC 1521 quoted-printable specification, along with
Barry Warsaw <barry@python.org>
parents:
17158
diff
changeset
|
67 output.write(s + lineEnd) |
|
4d1c78493221
Better support for RFC 1521 quoted-printable specification, along with
Barry Warsaw <barry@python.org>
parents:
17158
diff
changeset
|
68 |
|
4d1c78493221
Better support for RFC 1521 quoted-printable specification, along with
Barry Warsaw <barry@python.org>
parents:
17158
diff
changeset
|
69 prevline = None |
|
15031
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
70 while 1: |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
71 line = input.readline() |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
72 if not line: |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
73 break |
|
18157
4d1c78493221
Better support for RFC 1521 quoted-printable specification, along with
Barry Warsaw <barry@python.org>
parents:
17158
diff
changeset
|
74 outline = [] |
|
4d1c78493221
Better support for RFC 1521 quoted-printable specification, along with
Barry Warsaw <barry@python.org>
parents:
17158
diff
changeset
|
75 # Strip off any readline induced trailing newline |
|
4d1c78493221
Better support for RFC 1521 quoted-printable specification, along with
Barry Warsaw <barry@python.org>
parents:
17158
diff
changeset
|
76 stripped = '' |
|
4d1c78493221
Better support for RFC 1521 quoted-printable specification, along with
Barry Warsaw <barry@python.org>
parents:
17158
diff
changeset
|
77 if line[-1:] == '\n': |
|
15031
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
78 line = line[:-1] |
|
18157
4d1c78493221
Better support for RFC 1521 quoted-printable specification, along with
Barry Warsaw <barry@python.org>
parents:
17158
diff
changeset
|
79 stripped = '\n' |
|
18169
c638a7a2fe77
encode(): Fixed the handling of soft line breaks for lines over 76
Barry Warsaw <barry@python.org>
parents:
18157
diff
changeset
|
80 # Calculate the un-length-limited encoded line |
|
15031
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
81 for c in line: |
|
20187
8e4e49105595
Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents:
19694
diff
changeset
|
82 if needsquoting(c, quotetabs, header): |
|
15031
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
83 c = quote(c) |
|
20187
8e4e49105595
Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents:
19694
diff
changeset
|
84 if header and c == ' ': |
|
8e4e49105595
Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents:
19694
diff
changeset
|
85 outline.append('_') |
|
8e4e49105595
Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents:
19694
diff
changeset
|
86 else: |
|
8e4e49105595
Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents:
19694
diff
changeset
|
87 outline.append(c) |
|
18169
c638a7a2fe77
encode(): Fixed the handling of soft line breaks for lines over 76
Barry Warsaw <barry@python.org>
parents:
18157
diff
changeset
|
88 # First, write out the previous line |
|
18157
4d1c78493221
Better support for RFC 1521 quoted-printable specification, along with
Barry Warsaw <barry@python.org>
parents:
17158
diff
changeset
|
89 if prevline is not None: |
|
4d1c78493221
Better support for RFC 1521 quoted-printable specification, along with
Barry Warsaw <barry@python.org>
parents:
17158
diff
changeset
|
90 write(prevline) |
|
18169
c638a7a2fe77
encode(): Fixed the handling of soft line breaks for lines over 76
Barry Warsaw <barry@python.org>
parents:
18157
diff
changeset
|
91 # Now see if we need any soft line breaks because of RFC-imposed |
|
c638a7a2fe77
encode(): Fixed the handling of soft line breaks for lines over 76
Barry Warsaw <barry@python.org>
parents:
18157
diff
changeset
|
92 # length limitations. Then do the thisline->prevline dance. |
|
c638a7a2fe77
encode(): Fixed the handling of soft line breaks for lines over 76
Barry Warsaw <barry@python.org>
parents:
18157
diff
changeset
|
93 thisline = EMPTYSTRING.join(outline) |
|
c638a7a2fe77
encode(): Fixed the handling of soft line breaks for lines over 76
Barry Warsaw <barry@python.org>
parents:
18157
diff
changeset
|
94 while len(thisline) > MAXLINESIZE: |
|
c638a7a2fe77
encode(): Fixed the handling of soft line breaks for lines over 76
Barry Warsaw <barry@python.org>
parents:
18157
diff
changeset
|
95 # Don't forget to include the soft line break `=' sign in the |
|
c638a7a2fe77
encode(): Fixed the handling of soft line breaks for lines over 76
Barry Warsaw <barry@python.org>
parents:
18157
diff
changeset
|
96 # length calculation! |
|
c638a7a2fe77
encode(): Fixed the handling of soft line breaks for lines over 76
Barry Warsaw <barry@python.org>
parents:
18157
diff
changeset
|
97 write(thisline[:MAXLINESIZE-1], lineEnd='=\n') |
|
c638a7a2fe77
encode(): Fixed the handling of soft line breaks for lines over 76
Barry Warsaw <barry@python.org>
parents:
18157
diff
changeset
|
98 thisline = thisline[MAXLINESIZE-1:] |
|
c638a7a2fe77
encode(): Fixed the handling of soft line breaks for lines over 76
Barry Warsaw <barry@python.org>
parents:
18157
diff
changeset
|
99 # Write out the current line |
|
c638a7a2fe77
encode(): Fixed the handling of soft line breaks for lines over 76
Barry Warsaw <barry@python.org>
parents:
18157
diff
changeset
|
100 prevline = thisline |
|
18157
4d1c78493221
Better support for RFC 1521 quoted-printable specification, along with
Barry Warsaw <barry@python.org>
parents:
17158
diff
changeset
|
101 # Write out the last line, without a trailing newline |
|
4d1c78493221
Better support for RFC 1521 quoted-printable specification, along with
Barry Warsaw <barry@python.org>
parents:
17158
diff
changeset
|
102 if prevline is not None: |
|
4d1c78493221
Better support for RFC 1521 quoted-printable specification, along with
Barry Warsaw <barry@python.org>
parents:
17158
diff
changeset
|
103 write(prevline, lineEnd=stripped) |
| 2742 | 104 |
|
20187
8e4e49105595
Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents:
19694
diff
changeset
|
105 def encodestring(s, quotetabs = 0, header = 0): |
|
8e4e49105595
Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents:
19694
diff
changeset
|
106 if b2a_qp is not None: |
|
8e4e49105595
Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents:
19694
diff
changeset
|
107 return b2a_qp(s, quotetabs = quotetabs, header = header) |
|
18157
4d1c78493221
Better support for RFC 1521 quoted-printable specification, along with
Barry Warsaw <barry@python.org>
parents:
17158
diff
changeset
|
108 from cStringIO import StringIO |
|
4d1c78493221
Better support for RFC 1521 quoted-printable specification, along with
Barry Warsaw <barry@python.org>
parents:
17158
diff
changeset
|
109 infp = StringIO(s) |
|
4d1c78493221
Better support for RFC 1521 quoted-printable specification, along with
Barry Warsaw <barry@python.org>
parents:
17158
diff
changeset
|
110 outfp = StringIO() |
|
20187
8e4e49105595
Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents:
19694
diff
changeset
|
111 encode(infp, outfp, quotetabs, header) |
|
18157
4d1c78493221
Better support for RFC 1521 quoted-printable specification, along with
Barry Warsaw <barry@python.org>
parents:
17158
diff
changeset
|
112 return outfp.getvalue() |
|
4d1c78493221
Better support for RFC 1521 quoted-printable specification, along with
Barry Warsaw <barry@python.org>
parents:
17158
diff
changeset
|
113 |
|
4d1c78493221
Better support for RFC 1521 quoted-printable specification, along with
Barry Warsaw <barry@python.org>
parents:
17158
diff
changeset
|
114 |
|
18358
56652e684737
Whitespace normalization; the plat-riscos file didn't even get by
Tim Peters <tim.peters@gmail.com>
parents:
18169
diff
changeset
|
115 |
|
20187
8e4e49105595
Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents:
19694
diff
changeset
|
116 def decode(input, output, header = 0): |
|
15031
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
117 """Read 'input', apply quoted-printable decoding, and write to 'output'. |
|
20187
8e4e49105595
Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents:
19694
diff
changeset
|
118 'input' and 'output' are files with readline() and write() methods. |
|
8e4e49105595
Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents:
19694
diff
changeset
|
119 If 'header' is true, decode underscore as space (per RFC 1522).""" |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7752
diff
changeset
|
120 |
|
20187
8e4e49105595
Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents:
19694
diff
changeset
|
121 if a2b_qp is not None: |
|
8e4e49105595
Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents:
19694
diff
changeset
|
122 data = input.read() |
|
8e4e49105595
Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents:
19694
diff
changeset
|
123 odata = a2b_qp(data, header = header) |
|
8e4e49105595
Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents:
19694
diff
changeset
|
124 output.write(odata) |
|
8e4e49105595
Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents:
19694
diff
changeset
|
125 return |
|
8e4e49105595
Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents:
19694
diff
changeset
|
126 |
|
15031
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
127 new = '' |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
128 while 1: |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
129 line = input.readline() |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
130 if not line: break |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
131 i, n = 0, len(line) |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
132 if n > 0 and line[n-1] == '\n': |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
133 partial = 0; n = n-1 |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
134 # Strip trailing whitespace |
|
17158
f72173c793b6
Strip \r as trailing whitespace as part of soft line endings.
Guido van Rossum <guido@python.org>
parents:
16562
diff
changeset
|
135 while n > 0 and line[n-1] in " \t\r": |
|
15031
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
136 n = n-1 |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
137 else: |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
138 partial = 1 |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
139 while i < n: |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
140 c = line[i] |
|
20187
8e4e49105595
Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents:
19694
diff
changeset
|
141 if c == '_' and header: |
|
8e4e49105595
Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents:
19694
diff
changeset
|
142 new = new + ' '; i = i+1 |
|
8e4e49105595
Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents:
19694
diff
changeset
|
143 elif c != ESCAPE: |
|
15031
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
144 new = new + c; i = i+1 |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
145 elif i+1 == n and not partial: |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
146 partial = 1; break |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
147 elif i+1 < n and line[i+1] == ESCAPE: |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
148 new = new + ESCAPE; i = i+2 |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
149 elif i+2 < n and ishex(line[i+1]) and ishex(line[i+2]): |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
150 new = new + chr(unhex(line[i+1:i+3])); i = i+3 |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
151 else: # Bad escape sequence -- leave it in |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
152 new = new + c; i = i+1 |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
153 if not partial: |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
154 output.write(new + '\n') |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
155 new = '' |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
156 if new: |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
157 output.write(new) |
| 2742 | 158 |
|
20187
8e4e49105595
Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents:
19694
diff
changeset
|
159 def decodestring(s, header = 0): |
|
8e4e49105595
Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents:
19694
diff
changeset
|
160 if a2b_qp is not None: |
|
8e4e49105595
Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents:
19694
diff
changeset
|
161 return a2b_qp(s, header = header) |
|
18157
4d1c78493221
Better support for RFC 1521 quoted-printable specification, along with
Barry Warsaw <barry@python.org>
parents:
17158
diff
changeset
|
162 from cStringIO import StringIO |
|
4d1c78493221
Better support for RFC 1521 quoted-printable specification, along with
Barry Warsaw <barry@python.org>
parents:
17158
diff
changeset
|
163 infp = StringIO(s) |
|
4d1c78493221
Better support for RFC 1521 quoted-printable specification, along with
Barry Warsaw <barry@python.org>
parents:
17158
diff
changeset
|
164 outfp = StringIO() |
|
20187
8e4e49105595
Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents:
19694
diff
changeset
|
165 decode(infp, outfp, header = header) |
|
18157
4d1c78493221
Better support for RFC 1521 quoted-printable specification, along with
Barry Warsaw <barry@python.org>
parents:
17158
diff
changeset
|
166 return outfp.getvalue() |
|
4d1c78493221
Better support for RFC 1521 quoted-printable specification, along with
Barry Warsaw <barry@python.org>
parents:
17158
diff
changeset
|
167 |
|
4d1c78493221
Better support for RFC 1521 quoted-printable specification, along with
Barry Warsaw <barry@python.org>
parents:
17158
diff
changeset
|
168 |
|
18358
56652e684737
Whitespace normalization; the plat-riscos file didn't even get by
Tim Peters <tim.peters@gmail.com>
parents:
18169
diff
changeset
|
169 |
|
18157
4d1c78493221
Better support for RFC 1521 quoted-printable specification, along with
Barry Warsaw <barry@python.org>
parents:
17158
diff
changeset
|
170 # Other helper functions |
| 2742 | 171 def ishex(c): |
|
15031
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
172 """Return true if the character 'c' is a hexadecimal digit.""" |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
173 return '0' <= c <= '9' or 'a' <= c <= 'f' or 'A' <= c <= 'F' |
| 2742 | 174 |
| 175 def unhex(s): | |
|
15031
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
176 """Get the integer value of a hexadecimal number.""" |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
177 bits = 0 |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
178 for c in s: |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
179 if '0' <= c <= '9': |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
180 i = ord('0') |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
181 elif 'a' <= c <= 'f': |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
182 i = ord('a')-10 |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
183 elif 'A' <= c <= 'F': |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
184 i = ord('A')-10 |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
185 else: |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
186 break |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
187 bits = bits*16 + (ord(c) - i) |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
188 return bits |
| 2742 | 189 |
|
18157
4d1c78493221
Better support for RFC 1521 quoted-printable specification, along with
Barry Warsaw <barry@python.org>
parents:
17158
diff
changeset
|
190 |
|
18358
56652e684737
Whitespace normalization; the plat-riscos file didn't even get by
Tim Peters <tim.peters@gmail.com>
parents:
18169
diff
changeset
|
191 |
|
18157
4d1c78493221
Better support for RFC 1521 quoted-printable specification, along with
Barry Warsaw <barry@python.org>
parents:
17158
diff
changeset
|
192 def main(): |
|
15031
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
193 import sys |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
194 import getopt |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
195 try: |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
196 opts, args = getopt.getopt(sys.argv[1:], 'td') |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
197 except getopt.error, msg: |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
198 sys.stdout = sys.stderr |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
199 print msg |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
200 print "usage: quopri [-t | -d] [file] ..." |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
201 print "-t: quote tabs" |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
202 print "-d: decode; default encode" |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
203 sys.exit(2) |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
204 deco = 0 |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
205 tabs = 0 |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
206 for o, a in opts: |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
207 if o == '-t': tabs = 1 |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
208 if o == '-d': deco = 1 |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
209 if tabs and deco: |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
210 sys.stdout = sys.stderr |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
211 print "-t and -d are mutually exclusive" |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
212 sys.exit(2) |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
213 if not args: args = ['-'] |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
214 sts = 0 |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
215 for file in args: |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
216 if file == '-': |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
217 fp = sys.stdin |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
218 else: |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
219 try: |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
220 fp = open(file) |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
221 except IOError, msg: |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
222 sys.stderr.write("%s: can't open (%s)\n" % (file, msg)) |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
223 sts = 1 |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
224 continue |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
225 if deco: |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
226 decode(fp, sys.stdout) |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
227 else: |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
228 encode(fp, sys.stdout, tabs) |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
229 if fp is not sys.stdin: |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
230 fp.close() |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
231 if sts: |
|
9bc52706d283
Fix Bug #115907: encode '=' as '=3D' and not '=='
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11707
diff
changeset
|
232 sys.exit(sts) |
| 2742 | 233 |
|
18157
4d1c78493221
Better support for RFC 1521 quoted-printable specification, along with
Barry Warsaw <barry@python.org>
parents:
17158
diff
changeset
|
234 |
|
18358
56652e684737
Whitespace normalization; the plat-riscos file didn't even get by
Tim Peters <tim.peters@gmail.com>
parents:
18169
diff
changeset
|
235 |
| 2742 | 236 if __name__ == '__main__': |
|
18157
4d1c78493221
Better support for RFC 1521 quoted-printable specification, along with
Barry Warsaw <barry@python.org>
parents:
17158
diff
changeset
|
237 main() |
