summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJannis M. Hoffmann <jannis@fehcom.de>2023-03-11 23:33:10 +0100
committerJannis M. Hoffmann <jannis@fehcom.de>2023-03-11 23:33:10 +0100
commit2ac8a9bf66f63e44aa0c1b8a77c89cb68b92e47e (patch)
tree7e6a0f54755c62d148fe978721b8dc62eb7a06cf
parent87dd70bbd7b5bb5ea0d446f8cdb2552f8fb57600 (diff)
avoid quadratic complexity in count_mails
-rwxr-xr-xscript/qmauth.py13
1 files changed, 3 insertions, 10 deletions
diff --git a/script/qmauth.py b/script/qmauth.py
index 5874fc4..3cad208 100755
--- a/script/qmauth.py
+++ b/script/qmauth.py
@@ -46,8 +46,6 @@ class MyMaildir(Maildir):
super().__init__(dirname, *args, **kwargs)
def get_filename(self, mid):
- if mid not in self:
- raise KeyError(mid)
p_cur = glob(path.join(self.__path, 'cur', mid + '*'))
p_new = glob(path.join(self.__path, 'new', mid + '*'))
res = p_cur + p_new
@@ -69,11 +67,6 @@ class QMAuthError(Exception):
self.info = args
-@cache
-def _file_size(fname):
- return stat(fname).st_size
-
-
def _adr(addrs):
if addrs is None:
return None
@@ -141,7 +134,7 @@ def _sort_mails(f, sort):
if sort == 'date': keyfn = by_rec_date
elif sort == 'sender': keyfn = _sort_by_sender
elif sort == 'subject': keyfn = lambda midmsg: midmsg[1]['subject']
- elif sort == 'size': keyfn = lambda midmsg: _file_size(f.get_filename(midmsg[0]))
+ elif sort == 'size': keyfn = lambda midmsg: path.getsize(f.get_filename(midmsg[0]))
elif sort == '': keyfn = by_rec_date
else:
logging.warning("unknown sort-verb %r", sort)
@@ -198,7 +191,7 @@ def list_mails(f, start, end, sortby, folder):
return [
{
'message_handle': mid,
- 'byte_size': _file_size(f.get_filename(mid)),
+ 'byte_size': path.getsize(f.get_filename(mid)),
'unread': 'S' in msg.get_flags(),
'date_received': datetime.fromtimestamp(_get_rcv_time(mid)).isoformat(),
'head': _get_head_info(msg),
@@ -213,7 +206,7 @@ def count_mails(f, subfolder):
return {
'total_mails': len(f),
- 'byte_size': sum(_file_size(f.get_filename(mid)) for mid in f.keys()),
+ 'byte_size': sum(path.getsize(f.get_filename(mid)) for mid in f.keys()),
'unread_mails': len([1 for m in f if 'S' in m.get_flags()]),
}