mirror of
https://github.com/chatmail/relay.git
synced 2026-05-22 13:58:07 +00:00
further reduce code
This commit is contained in:
@@ -8,34 +8,13 @@ import shutil
|
|||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
from argparse import ArgumentParser
|
from argparse import ArgumentParser
|
||||||
|
from collections import namedtuple
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from stat import S_ISREG
|
from stat import S_ISREG
|
||||||
|
|
||||||
from chatmaild.config import read_config
|
from chatmaild.config import read_config
|
||||||
|
|
||||||
|
FileEntry = namedtuple("FileEntry", ("relpath", "mtime", "size"))
|
||||||
class FileEntry:
|
|
||||||
def __init__(self, relpath, mtime, size):
|
|
||||||
self.relpath = relpath
|
|
||||||
self.mtime = mtime
|
|
||||||
self.size = size
|
|
||||||
|
|
||||||
def __hash__(self):
|
|
||||||
return hash(self.relpath)
|
|
||||||
|
|
||||||
def __repr__(self):
|
|
||||||
return f"<FileEntry size={self.size} '{self.relpath}' >"
|
|
||||||
|
|
||||||
def __eq__(self, other):
|
|
||||||
return (
|
|
||||||
self.relpath == other.relpath
|
|
||||||
and self.size == other.size
|
|
||||||
and self.mtime == other.mtime
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def joinpath(name, extra):
|
|
||||||
return name + "/" + extra
|
|
||||||
|
|
||||||
|
|
||||||
class Stats:
|
class Stats:
|
||||||
@@ -46,7 +25,7 @@ class Stats:
|
|||||||
def iter_mailboxes(self, callback=None):
|
def iter_mailboxes(self, callback=None):
|
||||||
for name in os.listdir(self.basedir)[: self.maxnum]:
|
for name in os.listdir(self.basedir)[: self.maxnum]:
|
||||||
if "@" in name:
|
if "@" in name:
|
||||||
basedir = joinpath(self.basedir, name)
|
basedir = self.basedir + "/" + name
|
||||||
mailbox = MailboxStat(basedir)
|
mailbox = MailboxStat(basedir)
|
||||||
if callback is not None:
|
if callback is not None:
|
||||||
callback(mailbox)
|
callback(mailbox)
|
||||||
@@ -56,7 +35,7 @@ class MailboxStat:
|
|||||||
last_login = None
|
last_login = None
|
||||||
|
|
||||||
def __init__(self, basedir):
|
def __init__(self, basedir):
|
||||||
self.basedir = basedir = str(basedir)
|
self.basedir = str(basedir)
|
||||||
# all detected messages in cur/new/tmp folders
|
# all detected messages in cur/new/tmp folders
|
||||||
self.messages = []
|
self.messages = []
|
||||||
|
|
||||||
@@ -67,19 +46,16 @@ class MailboxStat:
|
|||||||
self.totalsize = 0
|
self.totalsize = 0
|
||||||
|
|
||||||
# scan all relevant files (without recursion)
|
# scan all relevant files (without recursion)
|
||||||
for name in os.listdir(basedir):
|
os.chdir(self.basedir)
|
||||||
fpath = joinpath(basedir, name)
|
for name in os.listdir("."):
|
||||||
if name in ("cur", "new", "tmp"):
|
if name in ("cur", "new", "tmp"):
|
||||||
for msg_name in os.listdir(fpath):
|
for msg_name in os.listdir(name):
|
||||||
msg_path = joinpath(fpath, msg_name)
|
relpath = name + "/" + msg_name
|
||||||
st = os.stat(msg_path)
|
st = os.stat(relpath)
|
||||||
relpath = joinpath(name, msg_name)
|
self.messages.append(FileEntry(relpath, st.st_mtime, st.st_size))
|
||||||
self.messages.append(
|
|
||||||
FileEntry(relpath, mtime=st.st_mtime, size=st.st_size)
|
|
||||||
)
|
|
||||||
self.totalsize += st.st_size
|
self.totalsize += st.st_size
|
||||||
else:
|
else:
|
||||||
st = os.stat(fpath)
|
st = os.stat(name)
|
||||||
if S_ISREG(st.st_mode):
|
if S_ISREG(st.st_mode):
|
||||||
self.extrafiles.append(FileEntry(name, st.st_mtime, st.st_size))
|
self.extrafiles.append(FileEntry(name, st.st_mtime, st.st_size))
|
||||||
if name == "password":
|
if name == "password":
|
||||||
|
|||||||
Reference in New Issue
Block a user