Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files 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 = 0 
)
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 116 of file quopri.py.

References a2b_qp, ishex(), and unhex().

117 def decode(input, output, header = 0):
118  """Read 'input', apply quoted-printable decoding, and write to 'output'.
119  'input' and 'output' are files with readline() and write() methods.
120  If 'header' is true, decode underscore as space (per RFC 1522)."""
121 
122  if a2b_qp is not None:
123  data = input.read()
124  odata = a2b_qp(data, header = header)
125  output.write(odata)
126  return
127 
128  new = ''
129  while 1:
130  line = input.readline()
131  if not line: break
132  i, n = 0, len(line)
133  if n > 0 and line[n-1] == '\n':
134  partial = 0; n = n-1
135  # Strip trailing whitespace
136  while n > 0 and line[n-1] in " \t\r":
137  n = n-1
138  else:
139  partial = 1
140  while i < n:
141  c = line[i]
142  if c == '_' and header:
143  new = new + ' '; i = i+1
144  elif c != ESCAPE:
145  new = new + c; i = i+1
146  elif i+1 == n and not partial:
147  partial = 1; break
148  elif i+1 < n and line[i+1] == ESCAPE:
149  new = new + ESCAPE; i = i+2
150  elif i+2 < n and ishex(line[i+1]) and ishex(line[i+2]):
151  new = new + chr(unhex(line[i+1:i+3])); i = i+3
152  else: # Bad escape sequence -- leave it in
153  new = new + c; i = i+1
154  if not partial:
155  output.write(new + '\n')
156  new = ''
157  if new:
158  output.write(new)
def quopri.decodestring (   s,
  header = 0 
)

Definition at line 159 of file quopri.py.

References a2b_qp, and decode().

160 def decodestring(s, header = 0):
161  if a2b_qp is not None:
162  return a2b_qp(s, header = header)
163  from cStringIO import StringIO
164  infp = StringIO(s)
165  outfp = StringIO()
166  decode(infp, outfp, header = header)
167  return outfp.getvalue()
168 
169 
170 
# Other helper functions
def quopri.encode (   input,
  output,
  quotetabs,
  header = 0 
)
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 42 of file quopri.py.

References b2a_qp, needsquoting(), and quote().

42 
43 def encode(input, output, quotetabs, header = 0):
44  """Read 'input', apply quoted-printable encoding, and write to 'output'.
45 
46  'input' and 'output' are files with readline() and write() methods.
47  The 'quotetabs' flag indicates whether embedded tabs and spaces should be
48  quoted. Note that line-ending tabs and spaces are always encoded, as per
49  RFC 1521.
50  The 'header' flag indicates whether we are encoding spaces as _ as per
51  RFC 1522.
52  """
53 
54  if b2a_qp is not None:
55  data = input.read()
56  odata = b2a_qp(data, quotetabs = quotetabs, header = header)
57  output.write(odata)
58  return
59 
60  def write(s, output=output, lineEnd='\n'):
61  # RFC 1521 requires that the line ending in a space or tab must have
62  # that trailing character encoded.
63  if s and s[-1:] in ' \t':
64  output.write(s[:-1] + quote(s[-1]) + lineEnd)
65  elif s == '.':
66  output.write(quote(s) + lineEnd)
67  else:
68  output.write(s + lineEnd)
69 
70  prevline = None
71  while 1:
72  line = input.readline()
73  if not line:
74  break
75  outline = []
76  # Strip off any readline induced trailing newline
77  stripped = ''
78  if line[-1:] == '\n':
79  line = line[:-1]
80  stripped = '\n'
81  # Calculate the un-length-limited encoded line
82  for c in line:
83  if needsquoting(c, quotetabs, header):
84  c = quote(c)
85  if header and c == ' ':
86  outline.append('_')
87  else:
88  outline.append(c)
89  # First, write out the previous line
90  if prevline is not None:
91  write(prevline)
92  # Now see if we need any soft line breaks because of RFC-imposed
93  # length limitations. Then do the thisline->prevline dance.
94  thisline = EMPTYSTRING.join(outline)
95  while len(thisline) > MAXLINESIZE:
96  # Don't forget to include the soft line break `=' sign in the
97  # length calculation!
98  write(thisline[:MAXLINESIZE-1], lineEnd='=\n')
99  thisline = thisline[MAXLINESIZE-1:]
100  # Write out the current line
101  prevline = thisline
102  # Write out the last line, without a trailing newline
103  if prevline is not None:
104  write(prevline, lineEnd=stripped)
def quopri.encodestring (   s,
  quotetabs = 0,
  header = 0 
)

Definition at line 105 of file quopri.py.

References b2a_qp, and encode().

106 def encodestring(s, quotetabs = 0, header = 0):
107  if b2a_qp is not None:
108  return b2a_qp(s, quotetabs = quotetabs, header = header)
109  from cStringIO import StringIO
110  infp = StringIO(s)
111  outfp = StringIO()
112  encode(infp, outfp, quotetabs, header)
113  return outfp.getvalue()
114 
115 
def quopri.ishex (   c)
Return true if the character 'c' is a hexadecimal digit.

Definition at line 171 of file quopri.py.

172 def ishex(c):
173  """Return true if the character 'c' is a hexadecimal digit."""
174  return '0' <= c <= '9' or 'a' <= c <= 'f' or 'A' <= c <= 'F'
def quopri.main ( )

Definition at line 192 of file quopri.py.

References decode(), encode(), getopt.getopt(), and aifc.open().

193 def main():
194  import sys
195  import getopt
196  try:
197  opts, args = getopt.getopt(sys.argv[1:], 'td')
198  except getopt.error, msg:
199  sys.stdout = sys.stderr
200  print msg
201  print "usage: quopri [-t | -d] [file] ..."
202  print "-t: quote tabs"
203  print "-d: decode; default encode"
204  sys.exit(2)
205  deco = 0
206  tabs = 0
207  for o, a in opts:
208  if o == '-t': tabs = 1
209  if o == '-d': deco = 1
210  if tabs and deco:
211  sys.stdout = sys.stderr
212  print "-t and -d are mutually exclusive"
213  sys.exit(2)
214  if not args: args = ['-']
215  sts = 0
216  for file in args:
217  if file == '-':
218  fp = sys.stdin
219  else:
220  try:
221  fp = open(file)
222  except IOError, msg:
223  sys.stderr.write("%s: can't open (%s)\n" % (file, msg))
224  sts = 1
225  continue
226  if deco:
227  decode(fp, sys.stdout)
228  else:
229  encode(fp, sys.stdout, tabs)
230  if fp is not sys.stdin:
231  fp.close()
232  if sts:
233  sys.exit(sts)
234 
235 
def quopri.needsquoting (   c,
  quotetabs,
  header 
)
Decide whether a particular character 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.

21 
22 def needsquoting(c, quotetabs, header):
23  """Decide whether a particular character needs to be quoted.
24 
25  The 'quotetabs' flag indicates whether embedded tabs and spaces should be
26  quoted. Note that line-ending tabs and spaces are always encoded, as per
27  RFC 1521.
28  """
29  if c in ' \t':
30  return quotetabs
31  # if header, we have to escape _ because _ is used to escape space
32  if c == '_':
33  return header
34  return c == ESCAPE or not (' ' <= c <= '~')
def quopri.quote (   c)
Quote a single character.

Definition at line 35 of file quopri.py.

35 
36 def quote(c):
37  """Quote a single character."""
38  i = ord(c)
39  return ESCAPE + HEX[i//16] + HEX[i%16]
40 
41 
def quopri.unhex (   s)
Get the integer value of a hexadecimal number.

Definition at line 175 of file quopri.py.

176 def unhex(s):
177  """Get the integer value of a hexadecimal number."""
178  bits = 0
179  for c in s:
180  if '0' <= c <= '9':
181  i = ord('0')
182  elif 'a' <= c <= 'f':
183  i = ord('a')-10
184  elif 'A' <= c <= 'F':
185  i = ord('A')-10
186  else:
187  break
188  bits = bits*16 + (ord(c) - i)
189  return bits
190 
191 

Variable Documentation

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

Definition at line 7 of file quopri.py.

a2b_qp = None

Definition at line 17 of file quopri.py.

b2a_qp = None

Definition at line 18 of file quopri.py.

string EMPTYSTRING = ''

Definition at line 12 of file quopri.py.

string ESCAPE = '='

Definition at line 9 of file quopri.py.

string HEX = '0123456789ABCDEF'

Definition at line 11 of file quopri.py.

int MAXLINESIZE = 76

Definition at line 10 of file quopri.py.