'Return a list of instances of OfflineIMAP representative folder.'

def getfolders(self):

    if (self.folders is not None):
        return self.folders
    retval = []
    imapobj = self.imapserver.acquireconnection()
    listfunction = imapobj.list
    if self.getconfboolean('subscribedonly', False):
        listfunction = imapobj.lsub
    try:
        (result, listresult) = listfunction(directory=self.imapserver.reference)
        if (result != 'OK'):
            raise OfflineImapError(('Could    not    list    the    folders    for    repository    %s.    Server    responded:    %s' % (self.name, self, str(listresult))), OfflineImapError.ERROR.FOLDER)
    finally:
        self.imapserver.releaseconnection(imapobj)
    for s in listresult:
        if ((s == None) or (isinstance(s, str) and (s == ''))):
            continue
        try:
            (flags, delim, name) = imaputil.imapsplit(s)
        except ValueError:
            self.ui.error(('could    not    correctly    parse    server    response;    got:    %s' % s))
            raise
        flaglist = [x.lower() for x in imaputil.flagsplit(flags)]
        if ('\\noselect' in flaglist):
            continue
        foldername = imaputil.dequote(name)
        retval.append(self.getfoldertype()(self.imapserver, foldername, self))
    if len(self.folderincludes):
        imapobj = self.imapserver.acquireconnection()
        try:
            for foldername in self.folderincludes:
                try:
                    imapobj.select(foldername, readonly=True)
                except OfflineImapError as e:
                    if (e.severity > OfflineImapError.ERROR.FOLDER):
                        raise
                    self.ui.error(e, exc_info()[2], 'Invalid    folderinclude:')
                    continue
                retval.append(self.getfoldertype()(self.imapserver, foldername, self))
        finally:
            self.imapserver.releaseconnection(imapobj)
    if (self.foldersort is None):
        retval.sort(key=(lambda x: str.lower(x.getvisiblename())))
    else:
        def cmp2key(mycmp):
            'Converts    a    cmp=    function    into    a    key=    function\n                                                                We    need    to    keep    cmp    functions    for    backward    compatibility'
            class K(object, ):
                def __init__(self, obj, *args):
                    self.obj = obj
                def __cmp__(self, other):
                    return mycmp(self.obj.getvisiblename(), other.obj.getvisiblename())
            return K
        retval.sort(key=cmp2key(self.foldersort))
    self.folders = retval
    return self.folders
