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: - account –
Account
- regtype – ‘remote’, ‘local’, or ‘status’
- account –
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.
-
forgetfolders
()[source]¶ Forgets the cached list of folders, if any. Useful to run after a sync run.
-
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.
-
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.
-
getlocalroot
()[source]¶ Local root folder for storing messages. Will not be set for remote repositories.
-
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.
-
-
class
GmailRepository
(reposname, account)[source]¶ Gmail IMAP repository.
This class just has default settings for GMail’s IMAP service. So you can do ‘type = Gmail’ instead of ‘type = IMAP’ and skip specifying the hostname, port etc. See http://mail.google.com/support/bin/answer.py?answer=78799&topic=12814 for the values we use.
Initialize a GmailRepository object.
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.
-
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.
-
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.
-
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.
-
getmessagelist
()[source]¶ Gets the current message list.
You must call cachemessagelist() before calling this function!
-
getmessageuidlist
()[source]¶ Gets a list of UIDs.
You may have to call cachemessagelist() before calling this function!
-
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.
-
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.