sqlitedict #2

asyncio
inpos 2016-05-26 12:47:26 +03:00
parent 5052e485bc
commit fcaeba9363
1 changed files with 38 additions and 53 deletions

View File

@ -68,11 +68,9 @@ class IMAPMailbox(ExtendedMaildir):
maildir.initializeMaildir(path) maildir.initializeMaildir(path)
self.listeners = [] self.listeners = []
self.path = path self.path = path
self.path_msg_info = os.path.join(path, conf.imap_msg_info)
self.path_mbox_info = os.path.join(path, conf.imap_mbox_info)
self.lastadded = None self.lastadded = None
if not os.path.exists(os.path.join(path, conf.imap_flags)):
self.__init_flags_()
else:
self.__load_flags_()
self.__check_flags() self.__check_flags()
def _start_monitor(self): def _start_monitor(self):
@ -88,41 +86,28 @@ class IMAPMailbox(ExtendedMaildir):
for l in self.listeners: for l in self.listeners:
l.newMessages(self.getMessageCount(), self.getRecentCount()) l.newMessages(self.getMessageCount(), self.getRecentCount())
def __init_flags_(self): def __check_flags_(self):
for fdir in ['new','cur']: with SqliteDict(self.path_msg_info) as msg_info, SqliteDict(self.path_mbox_info) as mbox_info:
for fn in os.listdir(os.path.join(self.path, fdir)):
if fn not in self.flags['uid'].keys():
self.flags['uid'][fn] = self.getUIDNext()
if fdir == 'new':
self.flags['flags'][fn] = []
else:
self.flags['flags'][fn] = misc.IMAP_FLAGS['SEEN']
self._save_flags()
self.__load_flags_()
def __load_flags_(self):
with SqliteDict(conf.imap_msg_info) as msg_info, SqliteDict(conf.imap_mbox_info) as mbox_info:
if 'subscribed' not in mbox_info.keys(): mbox_info['subscribed'] = False if 'subscribed' not in mbox_info.keys(): mbox_info['subscribed'] = False
if 'uidvalidity' not in mbox_info.keys(): mbox_info['uidvalidity'] = random.randint(0, 2**32) if 'uidvalidity' not in mbox_info.keys(): mbox_info['uidvalidity'] = random.randint(0, 2**32)
if 'uidnext' not in mbox_info.keys(): mbox_info['uidnext'] = 1 if 'uidnext' not in mbox_info.keys(): mbox_info['uidnext'] = 1
mbox_info.commit()
l = [l for l in self.__msg_list_()] l = [l for l in self.__msg_list_()]
for i in l: for i in l:
fn = i.split('/')[-1] fn = i.split('/')[-1]
if fn not in msg_info.keys(): if fn not in msg_info.keys():
msg_info[fn] = {'uid': self.getUIDNext()} val1 = {'uid': self.getUIDNext()}
if i.split('/')[-2] == 'new': if i.split('/')[-2] == 'new':
msg_info[fn]['flags'] = [] val1['flags'] = []
else: else:
msg_info[fn]['flags'] = [misc.IMAP_FLAGS['SEEN']] val1['flags'] = [misc.IMAP_FLAGS['SEEN']]
msg_info[fn] = val1
msg_info.commit()
def __count_flagged_msgs_(self, flag): def __count_flagged_msgs_(self, flag):
c = 0 with SqliteDict(self.path_msg_info) as msg_info:
self.__load_flags_() val1 = [0 for fn in msg_info.keys() if flag in msg_info[fn]['flags']]
for dir in ['new','cur']: return len(val1)
for fn in os.listdir(os.path.join(self.path, dir)):
if flag in self.flags['flags'][fn]:
c += 1
return c
def getHierarchicalDelimiter(self): def getHierarchicalDelimiter(self):
return misc.IMAP_HDELIM return misc.IMAP_HDELIM
@ -131,10 +116,9 @@ class IMAPMailbox(ExtendedMaildir):
return misc.IMAP_FLAGS.values() return misc.IMAP_FLAGS.values()
def getMessageCount(self): def getMessageCount(self):
self.__load_flags_() with SqliteDict(self.path_msg_info) as msg_info:
c = 0 val1 = [0 for fn in msg_info.keys() if misc.IMAP_FLAGS['DELETED'] not in msg_info[fn]['flags']]
c += len([n for n in self.__msg_list_() if misc.IMAP_FLAGS['DELETED'] not in self.flags['flags'][n.split('/')[-1]]]) return len(val1)
return c
def getRecentCount(self): def getRecentCount(self):
return self.__count_flagged_msgs_(misc.IMAP_FLAGS['RECENT']) return self.__count_flagged_msgs_(misc.IMAP_FLAGS['RECENT'])
@ -146,14 +130,14 @@ class IMAPMailbox(ExtendedMaildir):
return True return True
def getUIDValidity(self): def getUIDValidity(self):
self.__load_flags_() return SqliteDict(self.path_mbox_info)['uidvalidity']
return self.flags['uidvalidity']
def getUIDNext(self): def getUIDNext(self):
self.__load_flags_() with SqliteDict(self.path_mbox_info) as mbox_info:
self.flags['uidnext'] += 1 un = mbox_info['uidnext']
self._save_flags() mbox_info['uidnext'] += 1
return self.flags['uidnext'] - 1 mbox_info.commit()
return un
def getUID(self, num): def getUID(self, num):
return num return num
@ -162,16 +146,15 @@ class IMAPMailbox(ExtendedMaildir):
return self.appendMessage(message).addCallback(self._cbAddMessage, flags) return self.appendMessage(message).addCallback(self._cbAddMessage, flags)
def _cbAddMessage(self, obj, flags): def _cbAddMessage(self, obj, flags):
self.__load_flags_()
path = self.lastadded path = self.lastadded
self.lastadded = None self.lastadded = None
fn = path.split('/')[-1] fn = path.split('/')[-1]
self.flags['uid'][fn] = self.getUIDNext() with SqliteDict(self.path_msg_info) as msg_info:
self.flags['flags'][fn] = flags msg_info[fn] = {'uid': self.getUIDNext(), 'flags': flags}
msg_info.commit()
if misc.IMAP_FLAGS['SEEN'] in flags and path.split('/')[-2] != 'cur': if misc.IMAP_FLAGS['SEEN'] in flags and path.split('/')[-2] != 'cur':
new_path = os.path.join(self.path, 'cur', fn) new_path = os.path.join(self.path, 'cur', fn)
os.rename(path, new_path) os.rename(path, new_path)
self._save_flags()
def __msg_list_(self): def __msg_list_(self):
a = [] a = []
@ -195,7 +178,7 @@ class IMAPMailbox(ExtendedMaildir):
def fetch(self, messages, uid): def fetch(self, messages, uid):
return [[seq, MaildirMessage(seq, return [[seq, MaildirMessage(seq,
file(filename, 'rb').read(), file(filename, 'rb').read(),
self.flags['flags'][filename.split('/')[-1]], SqliteDict(self.path_msg_info)[filename.split('/')[-1]]['flags'],
rfc822date())] rfc822date())]
for seq, filename in self.__fetch_(messages, uid).iteritems()] for seq, filename in self.__fetch_(messages, uid).iteritems()]
def __fetch_(self, messages, uid): def __fetch_(self, messages, uid):
@ -203,10 +186,12 @@ class IMAPMailbox(ExtendedMaildir):
if uid: if uid:
messagesToFetch = {} messagesToFetch = {}
if not messages.last: if not messages.last:
messages.last = self.flags['uidnext'] messages.last = SqliteDict(self.path_mbox_info)['uidnext']
with SqliteDict(self.path_msg_info) as msg_info:
fn_uid = dict((fn, msg_info[fn]['uid']) for fn in msg_info.keys())
for uid in messages: for uid in messages:
if uid in self.flags['uid'].values(): if uid in fn_uid.values():
for name, _id in self.flags['uid'].iteritems(): for name, _id in fn_uid.iteritems():
if uid == _id: if uid == _id:
if os.path.exists(os.path.join(self.path,'new', name)): if os.path.exists(os.path.join(self.path,'new', name)):
messagesToFetch[uid] = os.path.join(self.path,'new', name) messagesToFetch[uid] = os.path.join(self.path,'new', name)