2020-11-01 08:43:16 +01:00
|
|
|
"""
|
|
|
|
Copyright (C) 2002-2016 John Goerzen & contributors.
|
2011-03-03 11:05:15 +01:00
|
|
|
|
2020-11-01 08:43:16 +01:00
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
"""
|
2016-06-29 03:42:57 +02:00
|
|
|
from sys import exc_info
|
2020-09-03 21:08:46 +02:00
|
|
|
from configparser import NoSectionError
|
2011-03-03 11:05:15 +01:00
|
|
|
from offlineimap.repository.IMAP import IMAPRepository, MappedIMAPRepository
|
|
|
|
from offlineimap.repository.Gmail import GmailRepository
|
|
|
|
from offlineimap.repository.Maildir import MaildirRepository
|
2012-10-16 20:20:35 +02:00
|
|
|
from offlineimap.repository.GmailMaildir import GmailMaildirRepository
|
2011-03-03 11:05:15 +01:00
|
|
|
from offlineimap.repository.LocalStatus import LocalStatusRepository
|
2012-08-31 20:05:01 +02:00
|
|
|
from offlineimap.error import OfflineImapError
|
|
|
|
|
2011-03-03 11:05:15 +01:00
|
|
|
|
2020-08-30 14:12:35 +02:00
|
|
|
class Repository:
|
2011-03-03 11:05:15 +01:00
|
|
|
"""Abstract class that returns the correct Repository type
|
|
|
|
instance based on 'account' and 'reqtype', e.g. a
|
|
|
|
class:`ImapRepository` instance."""
|
|
|
|
|
|
|
|
def __new__(cls, account, reqtype):
|
|
|
|
"""
|
|
|
|
:param account: :class:`Account`
|
2020-10-10 17:48:30 +02:00
|
|
|
:param reqtype: 'remote', 'local', or 'status'"""
|
2011-03-03 11:05:15 +01:00
|
|
|
|
|
|
|
if reqtype == 'remote':
|
|
|
|
name = account.getconf('remoterepository')
|
|
|
|
# We don't support Maildirs on the remote side.
|
|
|
|
typemap = {'IMAP': IMAPRepository,
|
2020-08-29 19:48:41 +02:00
|
|
|
'Gmail': GmailRepository}
|
2011-03-03 11:05:15 +01:00
|
|
|
|
|
|
|
elif reqtype == 'local':
|
|
|
|
name = account.getconf('localrepository')
|
|
|
|
typemap = {'IMAP': MappedIMAPRepository,
|
2020-08-29 19:48:41 +02:00
|
|
|
'Maildir': MaildirRepository,
|
|
|
|
'GmailMaildir': GmailMaildirRepository}
|
2011-03-03 11:05:15 +01:00
|
|
|
|
|
|
|
elif reqtype == 'status':
|
2015-01-14 22:58:25 +01:00
|
|
|
# create and return a LocalStatusRepository.
|
2011-03-03 11:05:15 +01:00
|
|
|
name = account.getconf('localrepository')
|
|
|
|
return LocalStatusRepository(name, account)
|
|
|
|
|
|
|
|
else:
|
2012-08-31 20:05:01 +02:00
|
|
|
errstr = "Repository type %s not supported" % reqtype
|
|
|
|
raise OfflineImapError(errstr, OfflineImapError.ERROR.REPO)
|
2011-03-03 11:05:15 +01:00
|
|
|
|
2015-01-14 22:58:25 +01:00
|
|
|
# Get repository type.
|
2011-03-03 11:05:15 +01:00
|
|
|
config = account.getconfig()
|
2012-08-31 20:05:01 +02:00
|
|
|
try:
|
|
|
|
repostype = config.get('Repository ' + name, 'type').strip()
|
2020-08-30 14:13:27 +02:00
|
|
|
except NoSectionError:
|
2012-08-31 20:05:01 +02:00
|
|
|
errstr = ("Could not find section '%s' in configuration. Required "
|
|
|
|
"for account '%s'." % ('Repository %s' % name, account))
|
2020-09-03 21:06:06 +02:00
|
|
|
raise OfflineImapError(errstr, OfflineImapError.ERROR.REPO,
|
|
|
|
exc_info()[2])
|
2012-08-31 20:05:01 +02:00
|
|
|
|
2011-03-03 11:05:15 +01:00
|
|
|
try:
|
|
|
|
repo = typemap[repostype]
|
|
|
|
except KeyError:
|
2020-08-29 19:48:41 +02:00
|
|
|
errstr = "'%s' repository not supported for '%s' repositories." % \
|
|
|
|
(repostype, reqtype)
|
2020-09-03 21:06:06 +02:00
|
|
|
raise OfflineImapError(errstr, OfflineImapError.ERROR.REPO,
|
|
|
|
exc_info()[2])
|
2011-03-03 11:05:15 +01:00
|
|
|
|
2012-08-31 20:05:01 +02:00
|
|
|
return repo(name, account)
|
2011-03-03 11:05:15 +01:00
|
|
|
|
|
|
|
def __init__(self, account, reqtype):
|
|
|
|
"""Load the correct Repository type and return that. The
|
|
|
|
__init__ of the corresponding Repository class will be
|
|
|
|
executed instead of this stub
|
|
|
|
|
|
|
|
:param account: :class:`Account`
|
2020-10-10 17:48:30 +02:00
|
|
|
:param reqtype: 'remote', 'local', or 'status'
|
2011-03-03 11:05:15 +01:00
|
|
|
"""
|
|
|
|
pass
|