aboutsummaryrefslogblamecommitdiffstats
path: root/listFolders.py
blob: 741ae41b9b8beb20d511a16635e3ae464522e205 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11


                        
              

              




                                                                    
 



















                                                                            

                                                              







                                                                











                                                                 
#!/usr/bin/env python3.6

import configparser
import logging
import os.path

import imaplib

logging.basicConfig(format='%(levelname)s:%(funcName)s:%(message)s',
                    level=logging.DEBUG)
log = logging.getLogger('listFolders')

def list_folder(name, wildcards='*'):
        ok, data = box.list(name, wildcards)
        if ok != 'OK':
            raise ServerError('Cannot list folder %s' % name)
        return data[0]

def get_separator():
        data = list_folder('pumpa')

        log.debug('data = %s (%s)', data, type(data))

        if data is None:
            data = list_folder('""', '""')

        data = data.decode().split(' ')
        if len(data) == 3:
            return data[1].strip(' "')
        else:
            raise ServerError('Cannot find folder separator from %s' % data)

config = configparser.ConfigParser()
config.read(os.path.expanduser('~/.config/imap_archiver.cfg'))
acc_name = config['general']['account']
cfg = dict(config.items(acc_name))

box = imaplib.IMAP4_SSL(host=cfg['host'])
ok, data = box.login(cfg['username'], cfg['password'])
if ok != 'OK':
    raise IOError('Cannot login with credentials %s' % str(cfg))

sep = get_separator()
print('sep = %s' % sep)
# data = list_folder('""', '""')
# log.debug('data = %s', data)
# sep = data.split()[1].decode()
#
# data = list_folder('Work/punk')
# log.debug('ok = %s, data = %s (type %s)', ok, data, type(data))

# for fld in data:
#     spl_fld = fld.decode().split(sep)
#     print(spl_fld[1].strip())