LocalStatus.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. """
  2. Local status cache repository support
  3. Copyright (C) 2002-2017 John Goerzen & contributors
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  15. """
  16. import os
  17. from offlineimap.folder.LocalStatus import LocalStatusFolder
  18. from offlineimap.folder.LocalStatusSQLite import LocalStatusSQLiteFolder
  19. from offlineimap.repository.Base import BaseRepository
  20. from offlineimap.error import OfflineImapError
  21. class LocalStatusRepository(BaseRepository):
  22. """
  23. Local Status Repository Class, child of Base Repository Class
  24. """
  25. def __init__(self, reposname, account):
  26. BaseRepository.__init__(self, reposname, account)
  27. # class and root for all backends.
  28. self.backends = {}
  29. self.backends['sqlite'] = {
  30. 'class': LocalStatusSQLiteFolder,
  31. 'root': os.path.join(account.getaccountmeta(), 'LocalStatus-sqlite')
  32. }
  33. self.backends['plain'] = {
  34. 'class': LocalStatusFolder,
  35. 'root': os.path.join(account.getaccountmeta(), 'LocalStatus')
  36. }
  37. if self.account.getconf('status_backend', None) is not None:
  38. raise OfflineImapError(
  39. "the 'status_backend' configuration option is not supported"
  40. " anymore; please, remove this configuration option.",
  41. OfflineImapError.ERROR.REPO
  42. )
  43. # Set class and root for sqlite.
  44. self.setup_backend('sqlite')
  45. if not os.path.exists(self.root):
  46. os.mkdir(self.root, 0o700)
  47. # self._folders is a dict of name:LocalStatusFolders().
  48. self._folders = {}
  49. def _instanciatefolder(self, foldername):
  50. return self.LocalStatusFolderClass(foldername, self) # Instantiate.
  51. def setup_backend(self, backend):
  52. """
  53. Setup the backend.
  54. Args:
  55. backend: backend to use
  56. Returns: None
  57. """
  58. if backend in list(self.backends.keys()):
  59. self._backend = backend
  60. self.root = self.backends[backend]['root']
  61. self.LocalStatusFolderClass = self.backends[backend]['class']
  62. def import_other_backend(self, folder):
  63. """
  64. Import other backend
  65. Args:
  66. folder: folder
  67. Returns: None
  68. """
  69. for bkend, dic in list(self.backends.items()):
  70. # Skip folder's own type.
  71. if dic['class'] == type(folder):
  72. continue
  73. repobk = LocalStatusRepository(self.name, self.account)
  74. repobk.setup_backend(bkend) # Fake the backend.
  75. folderbk = dic['class'](folder.name, repobk)
  76. # If backend contains data, import it to folder.
  77. if not folderbk.isnewfolder():
  78. self.ui._msg("Migrating LocalStatus cache from %s to %s "
  79. "status folder for %s:%s" %
  80. (bkend, self._backend, self.name, folder.name))
  81. folderbk.cachemessagelist()
  82. folder.messagelist = folderbk.messagelist
  83. folder.saveall()
  84. break
  85. def getsep(self):
  86. return '.'
  87. def makefolder(self, foldername):
  88. """Create a LocalStatus Folder."""
  89. if self.account.dryrun:
  90. return # Bail out in dry-run mode.
  91. # Create an empty StatusFolder.
  92. folder = self._instanciatefolder(foldername)
  93. # First delete any existing data to make sure we won't consider obsolete
  94. # data. This might happen if the user removed the folder (maildir) and
  95. # it is re-created afterwards.
  96. folder.purge()
  97. folder.openfiles()
  98. folder.save()
  99. folder.closefiles()
  100. # Invalidate the cache.
  101. self.forgetfolders()
  102. def getfolder(self, foldername):
  103. """Return the Folder() object for a foldername.
  104. Caller must call closefiles() on the folder when done."""
  105. if foldername in self._folders:
  106. return self._folders[foldername]
  107. folder = self._instanciatefolder(foldername)
  108. # If folder is empty, try to import data from an other backend.
  109. if folder.isnewfolder():
  110. self.import_other_backend(folder)
  111. self._folders[foldername] = folder
  112. return folder
  113. def getfolders(self):
  114. """Returns a list of all cached folders.
  115. Does nothing for this backend. We mangle the folder file names
  116. (see getfolderfilename) so we can not derive folder names from
  117. the file names that we have available. TODO: need to store a
  118. list of folder names somehow?"""
  119. def forgetfolders(self):
  120. """Forgets the cached list of folders, if any. Useful to run
  121. after a sync run."""
  122. self._folders = {}