__init__.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. """
  2. Copyright (C) 2002-2016 John Goerzen & contributors.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  14. """
  15. from sys import exc_info
  16. from configparser import NoSectionError
  17. from offlineimap.repository.IMAP import IMAPRepository, MappedIMAPRepository
  18. from offlineimap.repository.Gmail import GmailRepository
  19. from offlineimap.repository.Maildir import MaildirRepository
  20. from offlineimap.repository.GmailMaildir import GmailMaildirRepository
  21. from offlineimap.repository.LocalStatus import LocalStatusRepository
  22. from offlineimap.error import OfflineImapError
  23. class Repository:
  24. """Abstract class that returns the correct Repository type
  25. instance based on 'account' and 'reqtype', e.g. a
  26. class:`ImapRepository` instance."""
  27. def __new__(cls, account, reqtype):
  28. """
  29. :param account: :class:`Account`
  30. :param reqtype: 'remote', 'local', or 'status'"""
  31. if reqtype == 'remote':
  32. name = account.getconf('remoterepository')
  33. # We don't support Maildirs on the remote side.
  34. typemap = {'IMAP': IMAPRepository,
  35. 'Gmail': GmailRepository}
  36. elif reqtype == 'local':
  37. name = account.getconf('localrepository')
  38. typemap = {'IMAP': MappedIMAPRepository,
  39. 'Maildir': MaildirRepository,
  40. 'GmailMaildir': GmailMaildirRepository}
  41. elif reqtype == 'status':
  42. # create and return a LocalStatusRepository.
  43. name = account.getconf('localrepository')
  44. return LocalStatusRepository(name, account)
  45. else:
  46. errstr = "Repository type %s not supported" % reqtype
  47. raise OfflineImapError(errstr, OfflineImapError.ERROR.REPO)
  48. # Get repository type.
  49. config = account.getconfig()
  50. try:
  51. repostype = config.get('Repository ' + name, 'type').strip()
  52. except NoSectionError as exc:
  53. errstr = ("Could not find section '%s' in configuration. Required "
  54. "for account '%s'." % ('Repository %s' % name, account))
  55. raise OfflineImapError(errstr, OfflineImapError.ERROR.REPO,
  56. exc_info()[2]) from exc
  57. try:
  58. repo = typemap[repostype]
  59. except KeyError as exc:
  60. errstr = "'%s' repository not supported for '%s' repositories." % \
  61. (repostype, reqtype)
  62. raise OfflineImapError(errstr, OfflineImapError.ERROR.REPO,
  63. exc_info()[2]) from exc
  64. return repo(name, account)
  65. def __init__(self, account, reqtype):
  66. """Load the correct Repository type and return that. The
  67. __init__ of the corresponding Repository class will be
  68. executed instead of this stub
  69. :param account: :class:`Account`
  70. :param reqtype: 'remote', 'local', or 'status'
  71. """