]>
code.delx.au - offlineimap/blob - offlineimap/repository/Base.py
1 # Base repository support
2 # Copyright (C) 2002-2007 John Goerzen
3 # <jgoerzen@complete.org>
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 from offlineimap
import CustomConfig
22 def LoadRepository(name
, account
, reqtype
):
23 from offlineimap
.repository
.Gmail
import GmailRepository
24 from offlineimap
.repository
.IMAP
import IMAPRepository
, MappedIMAPRepository
25 from offlineimap
.repository
.Maildir
import MaildirRepository
26 if reqtype
== 'remote':
27 # For now, we don't support Maildirs on the remote side.
28 typemap
= {'IMAP': IMAPRepository
,
29 'Gmail': GmailRepository
}
30 elif reqtype
== 'local':
31 typemap
= {'IMAP': MappedIMAPRepository
,
32 'Maildir': MaildirRepository
}
34 raise ValueError, "Request type %s not supported" % reqtype
35 config
= account
.getconfig()
36 repostype
= config
.get('Repository ' + name
, 'type').strip()
37 return typemap
[repostype
](name
, account
)
39 class BaseRepository(CustomConfig
.ConfigHelperMixin
):
40 def __init__(self
, reposname
, account
):
41 self
.account
= account
42 self
.config
= account
.getconfig()
44 self
.localeval
= account
.getlocaleval()
45 self
.accountname
= self
.account
.getname()
46 self
.uiddir
= os
.path
.join(self
.config
.getmetadatadir(), 'Repository-' + self
.name
)
47 if not os
.path
.exists(self
.uiddir
):
48 os
.mkdir(self
.uiddir
, 0700)
49 self
.mapdir
= os
.path
.join(self
.uiddir
, 'UIDMapping')
50 if not os
.path
.exists(self
.mapdir
):
51 os
.mkdir(self
.mapdir
, 0700)
52 self
.uiddir
= os
.path
.join(self
.uiddir
, 'FolderValidity')
53 if not os
.path
.exists(self
.uiddir
):
54 os
.mkdir(self
.uiddir
, 0700)
56 # The 'restoreatime' config parameter only applies to local Maildir
58 def restore_atime(self
):
59 if self
.config
.get('Repository ' + self
.name
, 'type').strip() != \
63 if not self
.config
.has_option('Repository ' + self
.name
, 'restoreatime') or not self
.config
.getboolean('Repository ' + self
.name
, 'restoreatime'):
66 return self
.restore_folder_atimes()
69 """Establish a connection to the remote, if necessary. This exists
70 so that IMAP connections can all be established up front, gathering
71 passwords as needed. It was added in order to support the
72 error recovery -- we need to connect first outside of the error
73 trap in order to validate the password, and that's the point of
77 def holdordropconnections(self
):
80 def dropconnections(self
):
95 def getaccountname(self
):
96 return self
.accountname
99 return 'Repository ' + self
.name
104 def getlocaleval(self
):
105 return self
.account
.getlocaleval()
107 def getfolders(self
):
108 """Returns a list of ALL folders on this server."""
111 def forgetfolders(self
):
112 """Forgets the cached list of folders, if any. Useful to run
117 raise NotImplementedError
119 def makefolder(self
, foldername
):
120 raise NotImplementedError
122 def deletefolder(self
, foldername
):
123 raise NotImplementedError
125 def getfolder(self
, foldername
):
126 raise NotImplementedError
128 def syncfoldersto(self
, dest
, copyfolders
):
129 """Syncs the folders in this repository to those in dest.
130 It does NOT sync the contents of those folders.
132 For every time dest.makefolder() is called, also call makefolder()
133 on each folder in copyfolders."""
135 srcfolders
= src
.getfolders()
136 destfolders
= dest
.getfolders()
138 # Create hashes with the names, but convert the source folders
139 # to the dest folder's sep.
142 for folder
in srcfolders
:
143 srchash
[folder
.getvisiblename().replace(src
.getsep(), dest
.getsep())] = \
146 for folder
in destfolders
:
147 desthash
[folder
.getvisiblename()] = folder
153 for key
in srchash
.keys():
154 if not key
in desthash
:
156 for copyfolder
in copyfolders
:
157 copyfolder
.makefolder(key
.replace(dest
.getsep(), copyfolder
.getsep()))
160 # Find deleted folders.
162 # We don't delete folders right now.
164 #for key in desthash.keys():
165 # if not key in srchash:
166 # dest.deletefolder(key)
170 def startkeepalive(self
):
171 """The default implementation will do nothing."""
174 def stopkeepalive(self
):
175 """Stop keep alive, but don't bother waiting
176 for the threads to terminate."""