File: __init__.py

package info (click to toggle)
offlineimap3 0.0~git20210225.1e7ef9e%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,328 kB
  • sloc: python: 7,974; sh: 548; makefile: 81
file content (86 lines) | stat: -rw-r--r-- 3,578 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"""
Copyright (C) 2002-2016 John Goerzen & contributors.

   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
"""
from sys import exc_info
from configparser import NoSectionError
from offlineimap.repository.IMAP import IMAPRepository, MappedIMAPRepository
from offlineimap.repository.Gmail import GmailRepository
from offlineimap.repository.Maildir import MaildirRepository
from offlineimap.repository.GmailMaildir import GmailMaildirRepository
from offlineimap.repository.LocalStatus import LocalStatusRepository
from offlineimap.error import OfflineImapError


class Repository:
    """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`
        :param reqtype: 'remote', 'local', or 'status'"""

        if reqtype == 'remote':
            name = account.getconf('remoterepository')
            # We don't support Maildirs on the remote side.
            typemap = {'IMAP': IMAPRepository,
                       'Gmail': GmailRepository}

        elif reqtype == 'local':
            name = account.getconf('localrepository')
            typemap = {'IMAP': MappedIMAPRepository,
                       'Maildir': MaildirRepository,
                       'GmailMaildir': GmailMaildirRepository}

        elif reqtype == 'status':
            # create and return a LocalStatusRepository.
            name = account.getconf('localrepository')
            return LocalStatusRepository(name, account)

        else:
            errstr = "Repository type %s not supported" % reqtype
            raise OfflineImapError(errstr, OfflineImapError.ERROR.REPO)

        # Get repository type.
        config = account.getconfig()
        try:
            repostype = config.get('Repository ' + name, 'type').strip()
        except NoSectionError as exc:
            errstr = ("Could not find section '%s' in configuration. Required "
                      "for account '%s'." % ('Repository %s' % name, account))
            raise OfflineImapError(errstr, OfflineImapError.ERROR.REPO,
                                   exc_info()[2]) from exc

        try:
            repo = typemap[repostype]
        except KeyError as exc:
            errstr = "'%s' repository not supported for '%s' repositories." % \
                     (repostype, reqtype)
            raise OfflineImapError(errstr, OfflineImapError.ERROR.REPO,
                                   exc_info()[2]) from exc

        return repo(name, account)

    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`
        :param reqtype: 'remote', 'local', or 'status'
        """