[Tutor] Directory Structure
Steven D'Aprano
steve at pearwood.info
Fri Sep 29 21:32:57 EDT 2017
On Fri, Sep 29, 2017 at 07:02:07PM +0200, Chris wrote:
> Background: Maildirs with mails older than five years should be
> archived. The folder structure should be kept in the target.
Archived to what?
> I was very surprised, that there seems no readily usable module
> available. (In Perl neither).
Reusable module to do what *precisely*? If you cannot explain what you
need, how do you expect somebody to have anticipated your requirements
and written a module to do it?
> What's the best way to save them?
Depends on what you are doing. But coding the paths in your source code
is almost certainly not what you want to do. Surely you want to read the
paths from the maildir itself, as it *actually* exists, rather than try
to hard-code what you expect it to be in your source code?
Have you looked at the contents of a maildir? Its actually an almost
flat structure. Nested mail folders are not nested on the disk: a
user's mail folder structure that looks like:
inbox
sent
trash
personal
├── family
└── friends
work
├── critical
├── important
└── low
is stored on disk as:
Maildir/
├── .sent/
├── .trash/
├── .personal/
├── .personal.family/
├── .person.friends/
├── .work
├── .work.critical
├── .work.important
└── .work.low
(Not shown: the mails themselves, some dovecot specific data files
in the top level, and the special cur/ new/ tmp/ subdirectories found
in each mail directory.)
So all you really need is to record the path to the top level maildir
directories (the original, and the place where you are archiving them).
The subdirectories, you read from the disk as you go.
Actually, *you* don't read them at all. Have you looked at the mailbox
module in the standard library? It supports Maildir. I expect that what
you would do is something like:
source = Maildir('path/to/source')
archive = Maildir('path/to/archive')
for each directory in source:
for each mail in directory:
if mail older than five years:
copy mail to archive
delete mail from source
and let the mailbox module do the hard work. (Especially the part about
copying the mail, since the module will ensure that the flags and dates
are copied correctly.)
--
Steve
More information about the Tutor
mailing list