comparison src/plugins/plugin_misc_maildir.py @ 993:301b342c697a

core: use of the new core.log module: /!\ this is a massive refactoring and was largely automated, it probably did bring some bugs /!\
author Goffi <goffi@goffi.org>
date Sat, 19 Apr 2014 19:19:19 +0200
parents 1a759096ccbd
children 3b1c5f723c4b
comparison
equal deleted inserted replaced
992:f51a1895275c 993:301b342c697a
17 # You should have received a copy of the GNU Affero General Public License 17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 from sat.core.i18n import _ 20 from sat.core.i18n import _
21 from sat.core.constants import Const as C 21 from sat.core.constants import Const as C
22 from logging import debug, info, error 22 from sat.core.log import getLogger
23 log = getLogger(__name__)
23 import warnings 24 import warnings
24 warnings.filterwarnings('ignore', 'the MimeWriter', DeprecationWarning, 'twisted') # FIXME: to be removed, see http://twistedmatrix.com/trac/ticket/4038 25 warnings.filterwarnings('ignore', 'the MimeWriter', DeprecationWarning, 'twisted') # FIXME: to be removed, see http://twistedmatrix.com/trac/ticket/4038
25 from twisted.internet import protocol 26 from twisted.internet import protocol
26 from twisted.words.protocols.jabber import error as jab_error 27 from twisted.words.protocols import jabber
27 from twisted.cred import portal, checkers 28 from twisted.cred import portal, checkers
28 from twisted.mail import imap4, maildir 29 from twisted.mail import imap4, maildir
29 from email.parser import Parser 30 from email.parser import Parser
30 import email.message 31 import email.message
31 from email.charset import Charset 32 from email.charset import Charset
56 57
57 58
58 class MaildirBox(object): 59 class MaildirBox(object):
59 60
60 def __init__(self, host): 61 def __init__(self, host):
61 info(_("Plugin Maildir initialization")) 62 log.info(_("Plugin Maildir initialization"))
62 self.host = host 63 self.host = host
63 64
64 self.__observed = {} 65 self.__observed = {}
65 self.data = {} # list of profile spectific data. key = profile, value = PersistentBinaryDict where key=mailbox name, 66 self.data = {} # list of profile spectific data. key = profile, value = PersistentBinaryDict where key=mailbox name,
66 # and value is a dictionnary with the following value 67 # and value is a dictionnary with the following value
124 """Remove a reference to a box 125 """Remove a reference to a box
125 @param name: name of the box 126 @param name: name of the box
126 @param mailboxUser: MailboxUser instance""" 127 @param mailboxUser: MailboxUser instance"""
127 if boxname not in self.__mailboxes: 128 if boxname not in self.__mailboxes:
128 err_msg = _("Trying to remove an mailboxUser not referenced") 129 err_msg = _("Trying to remove an mailboxUser not referenced")
129 error(_("INTERNAL ERROR: ") + err_msg) 130 log.error(_("INTERNAL ERROR: ") + err_msg)
130 raise MaildirError(err_msg) 131 raise MaildirError(err_msg)
131 assert self.__mailboxes[profile][boxname] == mailboxUser 132 assert self.__mailboxes[profile][boxname] == mailboxUser
132 del self.__mailboxes[profile][boxname] 133 del self.__mailboxes[profile][boxname]
133 134
134 def _checkBoxReference(self, boxname, profile): 135 def _checkBoxReference(self, boxname, profile):
143 """Return the date of a box""" 144 """Return the date of a box"""
144 try: 145 try:
145 return self.data[profile][boxname] # the boxname MUST exist in the data 146 return self.data[profile][boxname] # the boxname MUST exist in the data
146 except KeyError: 147 except KeyError:
147 err_msg = _("Boxname doesn't exist in internal data") 148 err_msg = _("Boxname doesn't exist in internal data")
148 error(_("INTERNAL ERROR: ") + err_msg) 149 log.error(_("INTERNAL ERROR: ") + err_msg)
149 raise MaildirError(err_msg) 150 raise MaildirError(err_msg)
150 151
151 def getUid(self, boxname, message_id, profile): 152 def getUid(self, boxname, message_id, profile):
152 """Return an unique integer, always ascending, for a message 153 """Return an unique integer, always ascending, for a message
153 This is mainly needed for the IMAP protocol 154 This is mainly needed for the IMAP protocol
282 @param callback: method to remove from obervers 283 @param callback: method to remove from obervers
283 @param boxname: name of the box which was observed 284 @param boxname: name of the box which was observed
284 @param signal: which signal was observed by the caller""" 285 @param signal: which signal was observed by the caller"""
285 if (profile, boxname) not in self.__observed: 286 if (profile, boxname) not in self.__observed:
286 err_msg = _("Trying to remove an observer for an inexistant mailbox") 287 err_msg = _("Trying to remove an observer for an inexistant mailbox")
287 error(_("INTERNAL ERROR: ") + err_msg) 288 log.error(_("INTERNAL ERROR: ") + err_msg)
288 raise MaildirError(err_msg) 289 raise MaildirError(err_msg)
289 if signal not in self.__observed[(profile, boxname)]: 290 if signal not in self.__observed[(profile, boxname)]:
290 err_msg = _("Trying to remove an inexistant observer, no observer for this signal") 291 err_msg = _("Trying to remove an inexistant observer, no observer for this signal")
291 error(_("INTERNAL ERROR: ") + err_msg) 292 log.error(_("INTERNAL ERROR: ") + err_msg)
292 raise MaildirError(err_msg) 293 raise MaildirError(err_msg)
293 if not callback in self.__observed[(profile, boxname)][signal]: 294 if not callback in self.__observed[(profile, boxname)][signal]:
294 err_msg = _("Trying to remove an inexistant observer") 295 err_msg = _("Trying to remove an inexistant observer")
295 error(_("INTERNAL ERROR: ") + err_msg) 296 log.error(_("INTERNAL ERROR: ") + err_msg)
296 raise MaildirError(err_msg) 297 raise MaildirError(err_msg)
297 self.__observed[(profile, boxname)][signal].remove(callback) 298 self.__observed[(profile, boxname)][signal].remove(callback)
298 299
299 def emitSignal(self, profile, boxname, signal_name): 300 def emitSignal(self, profile, boxname, signal_name):
300 """Emit the signal to observer""" 301 """Emit the signal to observer"""
301 debug('emitSignal %s %s %s' % (profile, boxname, signal_name)) 302 log.debug('emitSignal %s %s %s' % (profile, boxname, signal_name))
302 try: 303 try:
303 for observer_cb in self.__observed[(profile, boxname)][signal_name]: 304 for observer_cb in self.__observed[(profile, boxname)][signal_name]:
304 observer_cb() 305 observer_cb()
305 except KeyError: 306 except KeyError:
306 pass 307 pass
332 """@param _maildir: the main MaildirBox instance 333 """@param _maildir: the main MaildirBox instance
333 @param name: name of the mailbox 334 @param name: name of the mailbox
334 @param profile: real profile (ie not a profile_key) 335 @param profile: real profile (ie not a profile_key)
335 THIS OBJECT MUST NOT BE USED DIRECTLY: use MaildirBox.accessMessageBox instead""" 336 THIS OBJECT MUST NOT BE USED DIRECTLY: use MaildirBox.accessMessageBox instead"""
336 if _maildir._checkBoxReference(name, profile): 337 if _maildir._checkBoxReference(name, profile):
337 error("INTERNAL ERROR: MailboxUser MUST NOT be instancied directly") 338 log.error("INTERNAL ERROR: MailboxUser MUST NOT be instancied directly")
338 raise MaildirError('double MailboxUser instanciation') 339 raise MaildirError('double MailboxUser instanciation')
339 if name != "INBOX": 340 if name != "INBOX":
340 raise NotImplementedError 341 raise NotImplementedError
341 self.name = name 342 self.name = name
342 self.profile = profile 343 self.profile = profile
350 self.mailbox = maildir.MaildirMailbox(mailbox_path) 351 self.mailbox = maildir.MaildirMailbox(mailbox_path)
351 self.observer = observer 352 self.observer = observer
352 self.__uid_table_update() 353 self.__uid_table_update()
353 354
354 if observer: 355 if observer:
355 debug("adding observer for %s (%s)" % (name, profile)) 356 log.debug("adding observer for %s (%s)" % (name, profile))
356 self.maildir.addObserver(observer, profile, name, "NEW_MESSAGE") 357 self.maildir.addObserver(observer, profile, name, "NEW_MESSAGE")
357 358
358 def __uid_table_update(self): 359 def __uid_table_update(self):
359 existant_id = [] 360 existant_id = []
360 for mess_idx in range(self.getMessageCount()): 361 for mess_idx in range(self.getMessageCount()):
363 self.getUid(mess_idx) 364 self.getUid(mess_idx)
364 self.maildir.cleanTable(self.name, existant_id, profile=self.profile) 365 self.maildir.cleanTable(self.name, existant_id, profile=self.profile)
365 366
366 def __del__(self): 367 def __del__(self):
367 if self.observer: 368 if self.observer:
368 debug("removing observer for %s" % self.name) 369 log.debug("removing observer for %s" % self.name)
369 self._maildir.removeObserver(self.observer, self.name, "NEW_MESSAGE") 370 self._maildir.removeObserver(self.observer, self.name, "NEW_MESSAGE")
370 self.maildir._removeBoxAccess(self.name, self, profile=self.profile) 371 self.maildir._removeBoxAccess(self.name, self, profile=self.profile)
371 372
372 def addMessage(self, message): 373 def addMessage(self, message):
373 """Add a message to the box 374 """Add a message to the box