offlineimap.repository – Email repositories

A derivative of class Base.BaseRepository represents an email repository depending on the type of storage, possible options are:

Which class you need depends on your account configuration. The helper class offlineimap.repository.Repository is an autoloader, that returns the correct class depending on your configuration. So when you want to instanciate a new offlineimap.repository, you will mostly do it through this class.

class Repository(account, reqtype)[source]

Abstract class that returns the correct Repository type instance based on ‘account’ and ‘reqtype’, e.g. a class:ImapRepository instance.

Load the correct Repository type and return that. The __init__ of the corresponding Repository class will be executed instead of this stub

Parameters:
  • accountAccount
  • regtype – ‘remote’, ‘local’, or ‘status’

offlineimap.repository.Base.BaseRepository – Representation of a mail repository

class BaseRepository(reposname, account)[source]
accountname

Account name as string

connect()[source]

Establish a connection to the remote, if necessary. This exists so that IMAP connections can all be established up front, gathering passwords as needed. It was added in order to support the error recovery – we need to connect first outside of the error trap in order to validate the password, and that’s the point of this function.

deletefolder(foldername)[source]
dropconnections()[source]
forgetfolders()[source]

Forgets the cached list of folders, if any. Useful to run after a sync run.

get_create_folders()[source]

Is folder creation enabled on this repository?

It is disabled by either setting the whole repository ‘readonly’ or by using the ‘createfolders’ setting.

getaccount()[source]
getconf(option, default=<function CustomConfigDefault>)

Retrieves string from the configuration.

Arguments:
  • option: option name whose value is to be retrieved;
  • default: default return value if no such option exists.
getconf_xform(option, xforms, default=<function CustomConfigDefault>)

Retrieves string from the configuration transforming the result.

Arguments:
  • option: option name whose value is to be retrieved;
  • xforms: iterable that returns transform functions to be applied to the value of the option, both retrieved and default one;
  • default: default value for string if no such option exists.
getconfboolean(option, default=<function CustomConfigDefault>)

Retrieves boolean value from the configuration.

Arguments:
  • option: option name whose value is to be retrieved;
  • default: default return value if no such option exists.
getconffloat(option, default=<function CustomConfigDefault>)

Retrieves floating-point value from the configuration.

Arguments:
  • option: option name whose value is to be retrieved;
  • default: default return value if no such option exists.
getconfig()[source]
getconfint(option, default=<function CustomConfigDefault>)

Retrieves integer value from the configuration.

Arguments:
  • option: option name whose value is to be retrieved;
  • default: default return value if no such option exists.
getconflist(option, separator_re, default=<function CustomConfigDefault>)

Retrieves strings from the configuration and splits it into the list of strings.

Arguments:
  • option: option name whose value is to be retrieved;
  • separator_re: regular expression for separator to be used for split operation;
  • default: default return value if no such option exists.
getfolder(foldername)[source]
getfolders()[source]

Returns a list of ALL folders on this server.

getkeywordmap()[source]
getlocaleval()[source]
getlocalroot()[source]

Local root folder for storing messages. Will not be set for remote repositories.

getmapdir()[source]
getname()[source]
getsection()[source]
getsep()[source]
getuiddir()[source]
holdordropconnections()[source]
makefolder(foldername)[source]

Create a new folder.

readonly

Is the repository readonly?

restore_atime()[source]

Sets folders’ atime back to their values after a sync

Controlled by the ‘restoreatime’ config parameter (default False), applies only to local Maildir mailboxes and does nothing on all other repository types.

should_sync_folder(fname)[source]

Should this folder be synced?

startkeepalive()[source]

The default implementation will do nothing.

stopkeepalive()[source]

Stop keep alive, but don’t bother waiting for the threads to terminate.

sync_folder_structure(dst_repo, status_repo)[source]

Syncs the folders in this repository to those in dest.

It does NOT sync the contents of those folders. nametrans rules in both directions will be honored, but there are NO checks yet that forward and backward nametrans actually match up! Configuring nametrans on BOTH repositories therefore could lead to infinite folder creation cycles.

class IMAPRepository(reposname, account)[source]
class MappedIMAPRepository(reposname, account)[source]
class GmailRepository(reposname, account)[source]

Gmail IMAP repository.

Falls back to hard-coded gmail host name and port, if none were specified: http://mail.google.com/support/bin/answer.py?answer=78799&topic=12814

Initialize a GmailRepository object.

class MaildirRepository(reposname, account)[source]

Initialize a MaildirRepository object. Takes a path name to the directory holding all the Maildir directories.

class LocalStatusRepository(reposname, account)[source]

offlineimap.folder – Basic representation of a local or remote Mail folder

class BaseFolder(name, repository)[source]
Parameters:
  • name – Path & name of folder minus root or reference
  • repository – Repository() in which the folder is.
accountname

Account name as string

addmessageflags(uid, flags)[source]

Adds the specified flags to the message’s flag set.

If a given flag is already present, it will not be duplicated.

Note that this function does not check against dryrun settings, so you need to ensure that it is never called in a dryrun mode.

Parameters:flags – A set() of flags
addmessageheader(content, linebreak, headername, headervalue)[source]

Adds new header to the provided message.

WARNING: This function is a bit tricky, and modifying it in the wrong way, may easily lead to data-loss.

Arguments: - content: message content, headers and body as a single string - linebreak: string that carries line ending - headername: name of the header to add - headervalue: value of the header to add

Note

The following documentation will not get displayed correctly after being processed by Sphinx. View the source of this method to read it.

This has to deal with strange corner cases where the header is missing or empty. Here are illustrations for all the cases, showing where the header gets inserted and what the end result is. In each illustration, ‘+’ means the added contents. Note that these examples assume LF for linebreak, not CRLF, so ‘

denotes a linebreak and ‘
‘ corresponds to the transition
between header and body. However if the linebreak parameter is set to ‘

‘ then you would have to substitute ‘ ‘ for

‘ in the below examples.

  • Case 1: No ‘

‘, leading ‘ ‘

+X-Flying-Pig-Header: i am here

This is the body

next line

  • Case 2: ‘

‘ at position 0

+X-Flying-Pig-Header: i am here

This is the body

next line

  • Case 3: No ‘

‘, no leading ‘ ‘

+X-Flying-Pig-Header: i am here

This is the body

next line

  • Case 4: ‘

‘ at non-zero position

Subject: Something wrong with OI

From: some@person.at +

X-Flying-Pig-Header: i am here

This is the body

next line

addmessagelabels(uid, labels)[source]

Adds the specified labels to the message’s labels set. If a given label is already present, it will not be duplicated.

Note that this function does not check against dryrun settings, so you need to ensure that it is never called in a dryrun mode.

Parameters:labels – A set() of labels
addmessagesflags(uidlist, flags)[source]

Note that this function does not check against dryrun settings, so you need to ensure that it is never called in a dryrun mode.

addmessageslabels(uidlist, labels)[source]

Note that this function does not check against dryrun settings, so you need to ensure that it is never called in a dryrun mode.

cachemessagelist()[source]

Cache the list of messages.

Reads the message list from disk or network and stores it in memory for later use. This list will not be re-read from disk or memory unless this function is called again.

change_message_uid(uid, new_uid)[source]

Change the message from existing uid to new_uid.

If the backend supports it (IMAP does not).

Parameters:new_uid – (optional) If given, the old UID will be changed to a new UID. This allows backends efficient renaming of messages if the UID has changed.
check_uidvalidity()[source]

Tests if the cached UIDVALIDITY match the real current one

If required it saves the UIDVALIDITY value. In this case the function is not threadsafe. So don’t attempt to call it from concurrent threads.

Returns:Boolean indicating the match. Returns True in case it implicitely saved the UIDVALIDITY.
combine_flags_and_keywords(uid, dstfolder)[source]

Combine the message’s flags and keywords using the mapping for the destination folder.

copymessageto(uid, dstfolder, statusfolder, register=1)[source]

Copies a message from self to dst if needed, updating the status

Note that this function does not check against dryrun settings, so you need to ensure that it is never called in a dryrun mode.

Parameters:
  • uid – uid of the message to be copied.
  • dstfolder – A BaseFolder-derived instance
  • statusfolder – A LocalStatusFolder instance
  • register – whether we should register a new thread.”
Returns:

Nothing on success, or raises an Exception.

deletemessage(uid)[source]

Note that this function does not check against dryrun settings, so you need to ensure that it is never called in a dryrun mode.

deletemessageflags(uid, flags)[source]

Removes each flag given from the message’s flag set.

Note that this function does not check against dryrun settings, so you need to ensure that it is never called in a dryrun mode.

If a given flag is already removed, no action will be taken for that flag.

deletemessageheaders(content, header_list)[source]

Deletes headers in the given list from the message content.

Arguments: - content: message itself - header_list: list of headers to be deleted or just the header name

We expect our message to have ‘

‘ as line endings.

deletemessagelabels(uid, labels)[source]

Removes each label given from the message’s label set.

If a given label is already removed, no action will be taken for that label.

Note that this function does not check against dryrun settings, so you need to ensure that it is never called in a dryrun mode.

deletemessages(uidlist)[source]

Note that this function does not check against dryrun settings, so you need to ensure that it is never called in a dryrun mode.

deletemessagesflags(uidlist, flags)[source]

Note that this function does not check against dryrun settings, so you need to ensure that it is never called in a dryrun mode.

deletemessageslabels(uidlist, labels)[source]

Note that this function does not check against dryrun settings, so you need to ensure that it is never called in a dryrun mode.

dofsync()[source]
dropmessagelistcache()[source]

Empty everythings we know about messages.

get_min_uid_file()[source]
get_saveduidvalidity()[source]

Return the previously cached UIDVALIDITY value

Returns:UIDVALIDITY as (long) number or None, if None had been saved yet.
get_uidvalidity()[source]

Retrieve the current connections UIDVALIDITY value

This function needs to be implemented by each Backend :returns: UIDVALIDITY as a (long) number.

getexplainedname()[source]

Name that shows both real and nametrans-mangled values.

getfolderbasename()[source]

Return base file name of file to store Status/UID info in.

getfullname()[source]
getinstancelimitnamespace()[source]

For threading folders, returns the instancelimitname for InstanceLimitedThreads.

getmaxage()[source]

Return maxage.

maxage is allowed to be either an integer or a date of the form YYYY-mm-dd. This returns a time_struct.

getmaxsize()[source]
getmessage(uid)[source]

Returns the content of the specified message.

getmessagecount()[source]

Gets the number of messages.

getmessageflags(uid)[source]

Returns the flags for the specified message.

getmessageheader(content, name)[source]

Return the value of the first occurence of the given header.

Header name is case-insensitive.

Arguments: - contents: message itself - name: name of the header to be searched

Returns: header value or None if no such header was found.

getmessageheaderlist(content, name)[source]

Return a list of values for the given header.

Arguments: - contents: message itself - name: name of the header to be searched

Returns: list of header values or empty list if no such header was found.

getmessagekeywords(uid)[source]

Returns the keywords for the specified message.

getmessagelabels(uid)[source]

Returns the labels for the specified message.

getmessagelist()[source]

Gets the current message list.

You must call cachemessagelist() before calling this function!

getmessagemtime(uid)[source]

Returns the message modification time of the specified message.

getmessagetime(uid)[source]

Return the received time for the specified message.

getmessageuidlist()[source]

Gets a list of UIDs.

You may have to call cachemessagelist() before calling this function!

getname()[source]

Returns name

getrepository()[source]

Returns the repository object that this folder is within.

getroot()[source]

Returns the root of the folder, in a folder-specific fashion.

getsep()[source]

Returns the separator for this folder type.

getstartdate()[source]

Retrieve the value of the configuration option startdate

getvisiblename()[source]

The nametrans-transposed name of the folder’s name.

ismessagelistempty()[source]

Is the list of messages empty.

msglist_item_initializer(uid)[source]

Returns value for empty messagelist element with given UID.

This function must initialize all fields of messagelist item and must be called every time when one creates new messagelist entry to ensure that all fields that must be present are present.

quickchanged(statusfolder)[source]

Runs quick check for folder changes and returns changed status: True – changed, False – not changed.

Parameters:statusfolder – keeps track of the last known folder state.
retrieve_min_uid()[source]
save_uidvalidity()[source]

Save the UIDVALIDITY value of the folder to the cache

This function is not threadsafe, so don’t attempt to call it from concurrent threads.

savemessage(uid, content, flags, rtime)[source]

Writes a new message, with the specified uid.

If the uid is < 0: The backend should assign a new uid and

return it. In case it cannot assign a new uid, it returns the negative uid passed in WITHOUT saving the message.

If the backend CAN assign a new uid, but cannot find out what this UID is (as is the case with some IMAP servers), it returns 0 but DOES save the message.

IMAP backend should be the only one that can assign a new uid.

If the uid is > 0, the backend should set the uid to this, if it can.
If it cannot set the uid to that, it will save it anyway. It will return the uid assigned in any case.

Note that savemessage() does not check against dryrun settings, so you need to ensure that savemessage is never called in a dryrun mode.

savemessageflags(uid, flags)[source]

Sets the specified message’s flags to the given set.

Note that this function does not check against dryrun settings, so you need to ensure that it is never called in a dryrun mode.

savemessagelabels(uid, labels, ignorelabels=set(), mtime=0)[source]

Sets the specified message’s labels to the given set.

Note that this function does not check against dryrun settings, so you need to ensure that it is never called in a dryrun mode.

storesmessages()[source]

Should be true for any backend that actually saves message bodies. (Almost all of them). False for the LocalStatus backend. Saves us from having to slurp up messages just for localstatus purposes.

suggeststhreads()[source]

Returns True if this folder suggests using threads for actions.

Only IMAP returns True. This method must honor any CLI or configuration option.

sync_this

Should this folder be synced or is it e.g. filtered out?

syncmessagesto(dstfolder, statusfolder)[source]

Syncs messages in this folder to the destination dstfolder.

This is the high level entry for syncing messages in one direction. Syncsteps are:

Pass1: Copy locally existing messages
Copy messages in self, but not statusfolder to dstfolder if not already in dstfolder. dstfolder might assign a new UID (e.g. if uploading to IMAP). Update statusfolder.
Pass2: Remove locally deleted messages

Get all UIDS in statusfolder but not self. These are messages that were deleted in ‘self’. Delete those from dstfolder and statusfolder.

After this pass, the message lists should be identical wrt the uids present (except for potential negative uids that couldn’t be placed anywhere).

Pass3: Synchronize flag changes
Compare flag mismatches in self with those in statusfolder. If msg has a valid UID and exists on dstfolder (has not e.g. been deleted there), sync the flag change to both dstfolder and statusfolder.
Pass4: Synchronize label changes (Gmail only)
Compares label mismatches in self with those in statusfolder. If msg has a valid UID and exists on dstfolder, syncs the labels to both dstfolder and statusfolder.
Parameters:
  • dstfolder – Folderinstance to sync the msgs to.
  • statusfolder – LocalStatus instance to sync against.
uidexists(uid)[source]

Returns True if uid exists.

waitforthread()[source]

Implements method that waits for thread to be usable. Should be implemented only for folders that suggest threads.