Did I find the right examples for you? yes no      Crawl my project      Python Jobs

All Samples(5)  |  Call(3)  |  Derive(0)  |  Import(2)
Parse a list of OpenPGP-style ASCII-armored messages from 's',
and return a list of (type, headers, body) tuples, where 'headers'
is a list of key,val tuples.

s -- the string to parse.
findTypes -- a list of types to search for; others are ignored.
base64 -- if false, we do cleartext armor.
base64fn -- if provided, called with (type, headers) to tell whether
   we do cleartext armor.

        def unarmorText(s, findTypes, base64=1, base64fn=None):
    """Parse a list of OpenPGP-style ASCII-armored messages from 's',
       and return a list of (type, headers, body) tuples, where 'headers'
       is a list of key,val tuples.

       s -- the string to parse.
       findTypes -- a list of types to search for; others are ignored.
       base64 -- if false, we do cleartext armor.
       base64fn -- if provided, called with (type, headers) to tell whether
          we do cleartext armor.
    """
    result = []

    while 1:
        tp = None
        fields = []
        value = None

        mBegin = BEGIN_LINE_RE.search(s)
        if not mBegin:
            return result

        tp = mBegin.group(1)
        endPat = r"^-----END %s-----[ \t]*\r?$" % tp

        endRE = re.compile(endPat, re.M)
        mEnd = endRE.search(s, mBegin.start())
        if not mEnd:
            raise ValueError("Couldn't find end line for '%s'"%tp.lower())

        if tp not in findTypes:
            s = s[mEnd.end()+1:]
            continue

        idx = mBegin.end()+1
        endIdx = mEnd.start()

        assert s[idx-1] == s[endIdx-1] == '\n'
        while idx < endIdx:
            nl = s.index("\n", idx, endIdx)
            line = s[idx:nl]
            idx = nl+1
            if ":" in line:
                m = ARMOR_KV_RE.match(line)
                if not m:
                    raise ValueError("Bad header for '%s'"%tp.lower())
                fields.append((m.group(1), m.group(2)))
            elif line.strip() == '':
                break

        if base64fn:
            base64 = base64fn(tp,fields)

        if base64:
            try:
                if stringContains(s[idx:endIdx], "\n[...]"):
                    raise UIError("Corrupted data: value seems to be truncated by a Mixminion/Mixmaster gateway")
                value = binascii.a2b_base64(s[idx:endIdx])
            except (TypeError, binascii.Incomplete, binascii.Error), e:
                raise ValueError(str(e))
        else:
            v = s[idx:endIdx].split("\n")
            for i in xrange(len(v)):
                if v[i].startswith("- "):
                    v[i] = v[i][2:]
            value = "\n".join(v)

        result.append((tp, fields, value))

        s = s[mEnd.end()+1:]

    raise MixFatalError("Unreachable code somehow reached.")
        


src/m/i/mixminion-HEAD/lib/mixminion/Packet.py   mixminion(Download)
import zlib
from socket import inet_ntoa, inet_aton
from mixminion.Common import MixError, MixFatalError, encodeBase64, \
     floorDiv, formatBase64, formatTime, isSMTPMailbox, LOG, armorText, \
     unarmorText, isPlausibleHostname
 
    try:
        res = unarmorText(s, (RB_ARMOR_NAME,), base64=1)
    except ValueError, e:
        raise ParseError(str(e))
        return 0
 
    unarmored = unarmorText(msg, (MESSAGE_ARMOR_NAME,), base64fn=isBase64)
    res = []
    for tp,fields,val in unarmored:

src/m/i/mixminion-HEAD/lib/mixminion/ClientUtils.py   mixminion(Download)
import mixminion.Packet
 
from mixminion.Common import LOG, MixError, UIError, ceilDiv, \
     createPrivateDir, floorDiv, previousMidnight, readFile, \
     succeedingMidnight, writeFile, armorText, unarmorText, MixFatalError
 
    text = readFile(fname)
    r = unarmorText(text, ["TYPE III KEYRING"])
    if len(r) != 1:
        raise ValueError("Bad ascii armor on keyring")