def _recursiveScanMaildir(self, relativePath=None): """Scan the Maildir recusively. Updates self._folders with what is found in the configured maildir path. TODO: fix encoding. :args: - maildirPath: path to the Maildir (as defined in the conf). """ def isFolder(path): return (os.path.isdir(os.path.join(path, 'cur')) and os.path.isdir(os.path.join(path, 'new')) and os.path.isdir(os.path.join(path, 'tmp'))) def scanChildren(path, relativePath): for directory in os.listdir(path): if directory in ['cur', 'new', 'tmp']: continue # Ignore special directories ASAP. folderPath = os.path.join(path, directory) if not os.path.isdir(folderPath): continue if relativePath is None: newRelativePath = directory else: newRelativePath = os.path.join(relativePath, directory) self._recursiveScanMaildir(newRelativePath) # Recurse! # Fix local variables to their default values if needed. maildirPath = self.conf.get('path') sep = self.conf.get('sep') # Set the fullPath. if relativePath is None: fullPath = maildirPath else: fullPath = os.path.join(maildirPath, relativePath) if isFolder(fullPath): #TODO: get encoding from conf. if relativePath is None: # We are the root of the maildir. Fix the name to '/'. folder = Folder('/', encoding='UTF-8') else: # Fix separator to '/' ASAP. ,-) folder = Folder('/'.join(relativePath.split(sep)), encoding='UTF-8') self._folders.append(folder) if sep == '/': # Recurse if nested folders are allowed. scanChildren(fullPath, relativePath) else: # The maildirPath as given by the user might not be a real maildir # but a base path of maildirs. Scan this path. if relativePath is None: scanChildren(fullPath, relativePath)
def test_getFolders_of_recursive_B(self): folders = self.driverB.getFolders() expected = Folders( Folder(b'/'), Folder(b'subfolder_A'), Folder(b'subfolder_B'), Folder(b'subfolder_B/subsubfolder_X'), ) self.assertEqual(folders, expected)
def setUp(self): self.folderA = Folder(b'A') self.folderB = Folder(b'A') self.folderI = Folder(b'A/B') self.folderJ = Folder(b'A/B') self.foldersX = Folders(self.folderA, self.folderI) self.foldersY = Folders(self.folderB, self.folderJ)
def getFolders(self) -> List[Dict[str, Union[str, bool]]]: folders = Folders() # (typ, [data]) # e.g. ('OK', [b'(\\HasNoChildren) "." INBOX.DRAFT']) response = self.imap.list() self._debugResponse('list', response) status, data = response if status == 'OK': for bytes_folderInfo in data: foldersInfo = bytes_folderInfo.decode(ENCODING) lst_foldersInfo = foldersInfo.split(' ') folderName = ' '.join(lst_foldersInfo[2:]).encode(ENCODING) folder = Folder(folderName) folder.setRoot(lst_foldersInfo[1]) childrenInfo = lst_foldersInfo[0] if childrenInfo == "(\\HasNoChildren)": folder.setHasChildren(False) else: folder.setHasChildren(True) folders.append(folder) self._debug('getFolders', folders) return folders raise ImapCommandError(str(data))
def select(self, folder: Folder) -> int: """Return number of existing messages.""" self._debug("select", str(folder)) # (typ, [data]) # e.g. ('OK', [b'1 2']) response = self.imap.select(folder.getName()) self._debugResponse("select", response) status, data = response if status == 'OK': #TODO: make a collection of UIDs. return int(data[0]) data = data.decode(ENCODING) raise ImapCommandError(data)
def _folders(self): folders = Folders() for folderName in self.conf.get('folders'): folders.append(Folder(folderName)) return folders
def test_getFolders_of_recursive_A(self): folders = self.driverA.getFolders() expected = Folders(Folder(b'/')) self.assertEqual(folders, expected)