コード例 #1
0
ファイル: email_message.py プロジェクト: jeske/csla
 def decode_part (self, m, as_utf8 = 0):
   ct = m.getmaintype("text")
   if ct != "text": as_utf8 = 0
   body = m.get_payload()
   if not isinstance(body, StringType):
     body = str(body)
   encoding = string.lower(m.get('content-transfer-encoding', '7bit'))
   if encoding == "quoted-printable":
     from quopri import decode
     ## how annoying
     inb = StringIO(body)
     outb = StringIO()
     decode(inb, outb)
     body = outb.getvalue()
   elif encoding == "base64":
     from base64 import decodestring
     try:
       body = decodestring(body)
     except:
       pass
   if ct == "text":
     # don't allow embedded nulls in text parts
     body = string.replace(body, '\0', '')
   if as_utf8: return to_utf8(body, self.charset(m))
   return body
コード例 #2
0
def quopri_decode(input, errors = 'strict'):
    assert errors == 'strict'
    f = StringIO(str(input))
    g = StringIO()
    quopri.decode(f, g)
    output = g.getvalue()
    return (output, len(input))
コード例 #3
0
ファイル: test_quopri.py プロジェクト: BillyboyD/main
 def test_decode(self):
     for p, e in self.STRINGS:
         infp = cStringIO.StringIO(e)
         outfp = cStringIO.StringIO()
         if not test_support.due_to_ironpython_bug("http://tkbgitvstfat01:8080/WorkItemTracking/WorkItem.aspx?artifactMoniker=317834"):
             quopri.decode(infp, outfp) #binascii.a2b_qp() not implemented which is needed in function decode() 
             self.assertTrue(outfp.getvalue() == p)
コード例 #4
0
ファイル: unpack.py プロジェクト: donnut/software
 def mime_unpack(self, input, stack):
     entity = mimetools.Message(input)
     stack = (entity,) + stack
     if entity.getmaintype() == 'multipart':
         self.parts.append(MessagePart(None, None, stack))
         boundary = entity.getparam('boundary')
         if boundary:
             input.push(boundary)
             while input.next():
                 self.mime_unpack(input, stack)
             input.pop()
     elif entity.gettype() == 'message/rfc822':
         self.parts.append(MessagePart(None, None, stack))
         self.mime_unpack(input, stack)
     else:
         name = get_filename(entity)
         work = tempfile.mktemp()
         cte = entity.getencoding()
         if cte == 'quoted-printable':
             output = cStringIO.StringIO()
             quopri.decode(input, output)
         elif cte == 'base64':
             output = cStringIO.StringIO()
             base64_decode(input, output)
         else:
             output = None
         if output:
             output.reset()
             if entity.gettype() == "application/x-gzip" or \
                    (name and name.endswith(".gz")):
                 work2 = tempfile.mktemp()
                 tmp2 = open(work2+".gz", "w")
                 tmp2.write(output.read())
                 tmp2.close()
                 inst = popen2.Popen4("gunzip %s.gz" % work2)
                 data = inst.fromchild.read()
                 if inst.wait() != 0:
                     self.write_fatal(_("Uncompressing your submission with gzip has failed. gzip said:\n%s\n") % data)
                     try:
                         os.unlink(work2+".gz")
                     except OSError:
                         pass
                     return
                 output=open(work2)
                 os.unlink(work2)
                 if name and name.endswith(".gz"):
                     name=name[:-3]
             tmp = open(work,"w")
             tmp.write(output.read())
             tmp.close()
             del output
             self.simple_unpack(open(work), name, stack)
             os.remove(work)
         else:
             self.simple_unpack(input, name, stack)
コード例 #5
0
ファイル: Utils.py プロジェクト: paulgay/crf_dia_ident
    def _qdecode(s):
        import quopri as _quopri

        if not s:
            return s
        infp = StringIO(s)
        outfp = StringIO()
        _quopri.decode(infp, outfp)
        value = outfp.getvalue()
        if not s.endswith('\n') and value.endswith('\n'):
            return value[:-1]
        return value
コード例 #6
0
def quopri_decode(input, errors='strict'):
    """Decode the input, returning a tuple (output object, length consumed).

    errors defines the error handling to apply. It defaults to
    'strict' handling which is the only currently supported
    error handling for this codec.

    """
    assert errors == 'strict'
    f = StringIO(str(input))
    g = StringIO()
    quopri.decode(f, g)
    output = g.getvalue()
    return (output, len(input))
コード例 #7
0
def decode(encoding, s):
  if encoding == '7bit' or encoding == '8bit' or encoding == 'binary':
    return s
  elif encoding == 'quoted-printable':
    import quopri
    ifp = StringIO.StringIO(s)
    ofp = StringIO.StringIO()
    quopri.decode(ifp, ofp)
    return ofp.getvalue()
  elif encoding == 'base64':
    import base64
    return base64.decodestring(s)
  else:
    raise Error("Unknown encoding %s" % repr(encoding))
コード例 #8
0
ファイル: mime.py プロジェクト: osuburger/Zaarly-Feed
def decode(encoding, s):
  if encoding == '7bit' or encoding == '8bit' or encoding == 'binary':
    return s
  elif encoding == 'quoted-printable':
    import quopri
    ifp = StringIO.StringIO(s)
    ofp = StringIO.StringIO()
    quopri.decode(ifp, ofp)
    return ofp.getvalue()
  elif encoding == 'base64':
    import base64
    return base64.decodestring(s)
  else:
    raise Error("Unknown encoding %s" % repr(encoding))
コード例 #9
0
def quopri_decode(input, errors='strict'):
    """Decode the input, returning a tuple (output object, length consumed).

    errors defines the error handling to apply. It defaults to
    'strict' handling which is the only currently supported
    error handling for this codec.

    """
    assert errors == 'strict'
    f = StringIO(str(input))
    g = StringIO()
    quopri.decode(f, g)
    output = g.getvalue()
    return (output, len(input))
コード例 #10
0
ファイル: textviewer.py プロジェクト: davidgiven/sqmail
	def __init__(self, reader, a):
		sqmail.gui.viewer.Viewer.__init__(self, reader, a, "textmessage")
		font = gtk.load_font(sqmail.preferences.get_textmessagefont())
		# Ensure the text box is 80 columns wide.
		width = gtk.gdk_char_width(font, "m")*82
		# The text box is guaranteed to be empty.
		self.viewer_widget.messagetext.freeze()
		self.viewer_widget.messagetext.set_usize(width, 0)
		if (self.attachment[1] == "text/quoted-printable"):
			infp = cStringIO.StringIO(self.attachment[2])
			outfp = cStringIO.StringIO()
			quopri.decode(infp, outfp)
			body = outfp.getvalue()
		else:
			body = self.attachment[2]
		self.viewer_widget.messagetext.insert(font, None, None, body)
		self.viewer_widget.messagetext.thaw()
コード例 #11
0
ファイル: htmlviewer.py プロジェクト: davidgiven/sqmail
 def __init__(self, reader, a):
     sqmail.gui.viewer.Viewer.__init__(self, reader, a, "htmlmessage")
     # Glade doesn't do the GtkXmHTML widget yet. So we need to add
     # it manually.
     self.htmlwidget = gnome.xmhtml.GtkXmHTML()
     self.viewer_widget.frame.add(self.htmlwidget)
     self.htmlwidget.show()
     self.htmlwidget.freeze()
     if self.attachment[1] == "text/quoted-printable":
         infp = cStringIO.StringIO(self.attachment[2])
         outfp = cStringIO.StringIO()
         quopri.decode(infp, outfp)
         body = outfp.getvalue()
     else:
         body = self.attachment[2]
     self.htmlwidget.set_allow_images(1)
     self.htmlwidget.source(body)
     self.htmlwidget.thaw()
コード例 #12
0
    def _decode(self, headers, fileobj):
        encoding = headers[-1].get_all("content-transfer-encoding", ["7bit"])[0]
        encoding = encoding.lower()

        if encoding == "base64":
            try:
                data = base64.b64decode(fileobj.read())
            except TypeError as error:
                self.log.error("Base64 decoding failed ({0})".format(error))
                idiokit.stop(False)
            return StringIO(data)

        if encoding == "quoted-printable":
            output = StringIO()
            quopri.decode(fileobj, output)
            output.seek(0)
            return output

        return fileobj
コード例 #13
0
ファイル: pubsublib.py プロジェクト: gburd/mod_pubsub
 def parseFrame(self, frame):
     # Parse a passed-in frame for name-value pairs.
     # Same as from_rfc_822_format in cgi-bin/PubSub/EventFormat.pm .
     message = {}
     while len(frame):
         pos = string.index(frame, "\n")
         header = None
         value = None
         header = frame[:pos]
         frame = frame[pos + 1:]
         if not pos:
             # The rest of the frame is the "kn_payload" value.
             name = "kn_payload"
             value = frame
             frame = ""
         else:
             # Now we've parsed out a header line.  Split into name and value.
             sep = string.index(header, ":")
             nameEscaped = header[:sep]
             valueEscaped = string.lstrip(header[sep + 1:])
             # Decode them.
             nameEscapedStream = cStringIO.StringIO(nameEscaped)
             valueEscapedStream = cStringIO.StringIO(valueEscaped)
             nameStream = cStringIO.StringIO()
             valueStream = cStringIO.StringIO()
             quopri.decode(nameEscapedStream, nameStream)
             quopri.decode(valueEscapedStream, valueStream)
             nameStream.seek(0)
             valueStream.seek(0)
             name = nameStream.read()
             value = valueStream.read()
         # Decode UTF-8.
         nameU = unicode(name, "UTF-8", "replace")
         valueU = unicode(value, "UTF-8", "replace")
         # Add this name-value pair to the message.
         if message.has_key(nameU):
             valueU = message[nameU] + ", " + valueU
         message[nameU] = valueU
         continue
     self.onMessage(message)
コード例 #14
0
ファイル: pubsublib.py プロジェクト: kragen/mod_pubsub
 def parseFrame(self, frame):
     # Parse a passed-in frame for name-value pairs.
     # Same as from_rfc_822_format in cgi-bin/PubSub/EventFormat.pm .
     message = { }
     while len(frame):
         pos = string.index(frame, "\n")
         header = None
         value = None
         header = frame[ : pos ]
         frame = frame[ pos + 1 : ]
         if not pos:
             # The rest of the frame is the "kn_payload" value.
             name = "kn_payload"
             value = frame
             frame = ""
         else:
             # Now we've parsed out a header line.  Split into name and value.
             sep = string.index(header, ":")
             nameEscaped = header[ : sep ]
             valueEscaped = string.lstrip(header[ sep + 1 : ])
             # Decode them.
             nameEscapedStream = cStringIO.StringIO(nameEscaped)
             valueEscapedStream = cStringIO.StringIO(valueEscaped)
             nameStream = cStringIO.StringIO()
             valueStream = cStringIO.StringIO()
             quopri.decode(nameEscapedStream, nameStream)
             quopri.decode(valueEscapedStream, valueStream)
             nameStream.seek(0)
             valueStream.seek(0)
             name = nameStream.read()
             value = valueStream.read()
         # Decode UTF-8.
         nameU = unicode(name, "UTF-8", "replace")
         valueU = unicode(value, "UTF-8", "replace")
         # Add this name-value pair to the message.
         if message.has_key(nameU):
             valueU = message[nameU] + ", " + valueU
         message[nameU] = valueU
         continue
     self.onMessage(message)
コード例 #15
0
ファイル: message.py プロジェクト: gocept/gocept.imapapi
    def fetch_file(self, f):
        """Fetch the body part into a file-like object."""
        # XXX This is icky. This means that on multipart messages we will
        # fetch everything but on non-multipart messages we only fetch the
        # first element. I also tried creating a fake multi-part body but
        # that ends up in an even worse abstraction once you hand it out to
        # the json encoding code.
        if (not self['content_type'].startswith('multipart/')
                and self['partnumber'] == ''):
            # RfC 3501: The part number of a single non-multipart message is
            # always 1.
            partnumber = '1'
        else:
            partnumber = self['partnumber']

        encoding = self.get('encoding')
        if encoding in ['base64', 'quoted-printable']:
            # XXX Make StringIO first, swap to temporary file on a size threshold.
            encoded = tempfile.NamedTemporaryFile()
        else:
            encoded = f

        for chunk_no in itertools.count():
            data = _fetch(self.server, self.message.parent, self.message.UID,
                          'BODY[%s]' % partnumber, chunk_no)
            if data == '':
                break
            if data == gocept.imapapi.parser.NIL:
                raise gocept.imapapi.interfaces.BrokenMIMEPart()
            encoded.write(data)

        encoded.seek(0)
        if encoding == 'base64':
            base64.decode(encoded, f)
        elif encoding == 'quoted-printable':
            quopri.decode(encoded, f)
        f.seek(0)
コード例 #16
0
ファイル: mimetools.py プロジェクト: tuankien2601/python222
def decode(input, output, encoding):
    if encoding == 'base64':
        import base64
        return base64.decode(input, output)
    if encoding == 'quoted-printable':
        import quopri
        return quopri.decode(input, output)
    if encoding in ('uuencode', 'x-uuencode', 'uue', 'x-uue'):
        import uu
        return uu.decode(input, output)
    if encoding in ('7bit', '8bit'):
        return output.write(input.read())
    else:
        raise ValueError, \
              'unknown Content-Transfer-Encoding: %s' % encoding
コード例 #17
0
def decode(input, output, encoding):
    if encoding == 'base64':
        import base64
        return base64.decode(input, output)
    if encoding == 'quoted-printable':
        import quopri
        return quopri.decode(input, output)
    if encoding in ('uuencode', 'x-uuencode', 'uue', 'x-uue'):
        import uu
        return uu.decode(input, output)
    if decodetab.has_key(encoding):
        pipethrough(input, decodetab[encoding], output)
    else:
        raise ValueError, \
              'unknown Content-Transfer-Encoding: %s' % encoding
コード例 #18
0
ファイル: mimetools.py プロジェクト: arandilopez/z-eves
def decode(input, output, encoding):
	if encoding == 'base64':
		import base64
		return base64.decode(input, output)
	if encoding == 'quoted-printable':
		import quopri
		return quopri.decode(input, output)
	if encoding in ('uuencode', 'x-uuencode', 'uue', 'x-uue'):
		import uu
		return uu.decode(input, output)
	if decodetab.has_key(encoding):
		pipethrough(input, decodetab[encoding], output)
	else:
		raise ValueError, \
		      'unknown Content-Transfer-Encoding: %s' % encoding
コード例 #19
0
def decode(input, output, encoding):
    if encoding == 'base64':
        import base64
        return base64.decode(input, output)
    if encoding == 'quoted-printable':
        import quopri
        return quopri.decode(input, output)
    if encoding in ('uuencode', 'x-uuencode', 'uue', 'x-uue'):
        import uu
        return uu.decode(input, output)
    if encoding in ('7bit', '8bit'):
        return output.write(input.read())
    if encoding in decodetab:
        pipethrough(input, decodetab[encoding], output)
    else:
        raise ValueError, 'unknown Content-Transfer-Encoding: %s' % encoding
コード例 #20
0
ファイル: mimetools.py プロジェクト: webiumsk/WOT-0.9.17-CT
def decode(input, output, encoding):
    """Decode common content-transfer-encodings (base64, quopri, uuencode)."""
    if encoding == 'base64':
        import base64
        return base64.decode(input, output)
    if encoding == 'quoted-printable':
        import quopri
        return quopri.decode(input, output)
    if encoding in ('uuencode', 'x-uuencode', 'uue', 'x-uue'):
        import uu
        return uu.decode(input, output)
    if encoding in ('7bit', '8bit'):
        return output.write(input.read())
    if encoding in decodetab:
        pipethrough(input, decodetab[encoding], output)
    else:
        raise ValueError, 'unknown Content-Transfer-Encoding: %s' % encoding
コード例 #21
0
ファイル: mimetools.py プロジェクト: asottile/ancient-pythons
def decode(input, output, encoding):
	"""Decode common content-transfer-encodings (base64, quopri, uuencode)."""
	if encoding == 'base64':
		import base64
		return base64.decode(input, output)
	if encoding == 'quoted-printable':
		import quopri
		return quopri.decode(input, output)
	if encoding in ('uuencode', 'x-uuencode', 'uue', 'x-uue'):
		import uu
		return uu.decode(input, output)
	if encoding in ('7bit', '8bit'):
		output.write(input.read())
	if decodetab.has_key(encoding):
		pipethrough(input, decodetab[encoding], output)
	else:
		raise ValueError, \
		      'unknown Content-Transfer-Encoding: %s' % encoding
コード例 #22
0
def parse_body(currentLine, line, lines, boundary):
    offset=1
    nextFirstLine = lines[currentLine+1]
#    if nextFirstLine.startswith("Content-Type"):
    encoding = None
    indexPlainText = nextFirstLine.find("text/plain")
    if indexPlainText == -1:
        return encoding, None
    else:
        offset = offset + 1

    nextOfNextLine = lines[currentLine+2]
    if nextOfNextLine.startswith("Content-Transfer-Encoding"):
        encoding = nextOfNextLine.split(':')[1].strip()
        offset = offset + 1
    textLines=lines[currentLine + offset:]
    actualText=[]
    for txtLine in textLines:
        if txtLine == "--{}".format(boundary) or txtLine == "--{}--".format(boundary):
            break
        else:
            actualText.append(txtLine)
    text = "\r\n".join(actualText)
    logger.info("text found")
    logger.info(text)
    logger.info("~"*8)
    #deal with encoding
    if encoding == "base64":
        text = base64.b64decode(text)
    #elif encoding == "7bit" or encoding == "8bit":
    #    text = quopri.decodestring(text)
    elif encoding == "quoted-printable":
        try:
            text = quopri.decode(text)
        except:
            pass
    elif encoding == "uuencode" or encoding == "x-uuencode" or encoding == "uue" or encoding == "x-uue":
        text = uu.decode(text)




    return encoding, text
コード例 #23
0
ファイル: quopri_codec.py プロジェクト: Josquin95/Triqui
def quopri_decode(input, errors='strict'):
    assert errors == 'strict'
    f = BytesIO(input)
    g = BytesIO()
    quopri.decode(f, g)
    return (g.getvalue(), len(input))
コード例 #24
0
def decodestring(instring):
    outfile = StringIO.StringIO()
    quopri.decode(StringIO.StringIO(instring), outfile)

    return outfile.getvalue()
コード例 #25
0
ファイル: quopri_codec.py プロジェクト: 1958/kbengine
def quopri_decode(input, errors='strict'):
    assert errors == 'strict'
    f = BytesIO(input)
    g = BytesIO()
    quopri.decode(f, g)
    return (g.getvalue(), len(input))
コード例 #26
0
ファイル: test_quopri.py プロジェクト: AojiaoZero/CrossApp
 def test_decode(self):
     for p, e in self.STRINGS:
         infp = cStringIO.StringIO(e)
         outfp = cStringIO.StringIO()
         quopri.decode(infp, outfp)
         self.assert_(outfp.getvalue() == p)
コード例 #27
0
ファイル: mimetools.py プロジェクト: mcyril/ravel-ftn
"""Various tools used by MIME-reading or MIME-writing programs."""
コード例 #28
0
ファイル: quopri_codec.py プロジェクト: mcyril/ravel-ftn
"""Codec for quoted-printable encoding.
コード例 #29
0
"""Codec for quoted-printable encoding.
コード例 #30
0
ファイル: test_quopri.py プロジェクト: zeus911/9miao
 def test_decode(self):
     for p, e in self.STRINGS:
         infp = cStringIO.StringIO(e)
         outfp = cStringIO.StringIO()
         quopri.decode(infp, outfp)
         self.assert_(outfp.getvalue() == p)
コード例 #31
0
 def test_decode(self):
     for p, e in self.STRINGS:
         infp = io.BytesIO(e)
         outfp = io.BytesIO()
         quopri.decode(infp, outfp)
         self.assertEqual(outfp.getvalue(), p)
コード例 #32
0
 def test_decode(self):
     for p, e in self.STRINGS:
         infp = io.BytesIO(e)
         outfp = io.BytesIO()
         quopri.decode(infp, outfp)
         self.assertEqual(outfp.getvalue(), p)
コード例 #33
0
ファイル: mimetools.py プロジェクト: mcyril/ravel-ftn
"""Various tools used by MIME-reading or MIME-writing programs."""
コード例 #34
0
ファイル: quopri_codec.py プロジェクト: connoryang/1v1dec
def quopri_decode(input, errors='strict'):
    f = StringIO(str(input))
    g = StringIO()
    quopri.decode(f, g)
    output = g.getvalue()
    return (output, len(input))
コード例 #35
0
ファイル: nodes.py プロジェクト: xxoolm/Ryven
 def update_event(self, inp=-1):
     self.set_output_val(
         0, quopri.decode(self.input(0), self.input(1), self.input(2)))
コード例 #36
0
def decodestring(instring):
	outfile = io.StringIO()
	quopri.decode(io.StringIO(instring), outfile)
	return outfile.getvalue()
コード例 #37
0
def decodestring(instring):
    outfile = io.BytesIO()
    quopri.decode(io.BytesIO(instring.encode()), outfile)
    return outfile.getvalue().decode("utf-8", "ignore")