def _parse_imap(reply):
 if ((not reply) or (len(reply) < 2)):
  return (False, [])
 stack = []
 pdata = []
 for dline in reply[1]:
  while True:
   if isinstance(dline, (str, unicode)):
    m = IMAP_TOKEN.match(dline)
   else:
    print ('WARNING: Unparsed IMAP response data: %s' % (dline,))
    m = None
   if m:
    token = m.group(0)
    dline = dline[len(token):]
    if (token[:1] == '"'):
     pdata.append(token[1:(-1)])
    elif (token[:1] == '('):
     stack.append(pdata)
     pdata.append([])
     pdata = pdata[(-1)]
    elif (token[:1] == ')'):
     pdata = stack.pop((-1))
    elif (token[:1] not in (' ', ' DCTB ', '\n', '\r')):
     pdata.append(token)
   else:
    break
 return ((reply[0].upper() == 'OK'), pdata)

def _parse_imap ( reply ) : if ( ( not reply ) or ( len ( reply ) < 2 ) ) : return ( False , [ ] ) stack = [ ] pdata = [ ] for dline in reply [ 1 ] : while True : if isinstance ( dline , ( str , unicode ) ) : m = IMAP_TOKEN . match ( dline ) else : print ( 'WARNING: Unparsed IMAP response data: %s' % ( dline , ) ) m = None if m : token = m . group ( 0 ) dline = dline [ len ( token ) : ] if ( token [ : 1 ] == '"' ) : pdata . append ( token [ 1 : ( - 1 ) ] ) elif ( token [ : 1 ] == '(' ) : stack . append ( pdata ) pdata . append ( [ ] ) pdata = pdata [ ( - 1 ) ] elif ( token [ : 1 ] == ')' ) : pdata = stack . pop ( ( - 1 ) ) elif ( token [ : 1 ] not in ( ' ' , ' DCTB ' , '\n' , '\r' ) ) : pdata . append ( token ) else : break return ( ( reply [ 0 ] . upper ( ) == 'OK' ) , pdata )
this routine will parse common imap4 responses into pythonic data structures .