if filename is not None)
+def get_user_mail_files(user, mailbox='INBOX'):
+ """
+ Iterate over mails in given folder of given user; yields file names
+
+ :param str mailbox: name of mailbox to use, INBOX (default) for base folder;
+ name is modified using :py:func:`cyrus_escape`
+ :returns: nothing; but yields full path to messages on disc
+ """
+ # base folder of user mail
+ folder = os.path.join('/datastore', 'imap-mails', 'user', user)
+
+ # adapt paths like "INBOX/sub/dir" need to "sub/dir"
+ subdirs = mailbox.split('/')
+ if subdirs[0].upper() == 'INBOX':
+ subdirs = subdirs[1:]
+ folder = os.path.join(folder,
+ *(cyrus_escape(subdir) for subdir in subdirs))
+
+ for filename in os.listdir(folder):
+ if not re.match(r'\d+\.', filename):
+ continue
+ full_path = os.path.join(folder, filename)
+ yield full_path
+
+
def get_user_mail(user, mailbox='INBOX', **kwargs):
"""
Iterate over mails in given folder of given user; yields parsed mails.
full path to the message on disc, and the latter is the outcome
of :py:func:`parse_mail_file` for that file
"""
- folder = os.path.join('/datastore', 'imap-mails', 'user', user)
- if mailbox != 'INBOX':
- folder = os.path.join(folder, cyrus_escape(mailbox))
- for filename in os.listdir(folder):
- if not re.match(r'\d+\.', filename):
- continue
- full_path = os.path.join(folder, filename)
- yield full_path, parse_mail_file(os.path.join(folder, filename),
- **kwargs)
+ for full_path in get_user_mail_files(user, mailbox):
+ yield full_path, parse_mail_file(full_path, **kwargs)
def get_message_text(filename, fallback_encoding='iso8859-1',