some streamlining and test fixing

This commit is contained in:
holger krekel
2020-06-05 22:01:14 +02:00
parent e29dcbf8eb
commit 7a5759de4b
3 changed files with 30 additions and 36 deletions

View File

@@ -1,4 +1,3 @@
import sys
import imaplib
import pathlib
@@ -15,11 +14,11 @@ def db_folder_attr(name):
class ImapConn:
def __init__(self, account):
self.account = account
self.conn_info = (account.get_config("configured_mail_server"),
account.get_config("addr"),
account.get_config("mail_pw"))
host, user, pw = self.conn_info
imap_conn_info = (
account.get_config("configured_mail_server"),
account.get_config("addr"),
account.get_config("mail_pw"))
host, user, pw = imap_conn_info
self.connection = imaplib.IMAP4_SSL(host)
self.connection.login(user, pw)
self._original_msg_count = {}
@@ -104,11 +103,9 @@ class ImapConn:
try:
return int(messages[0]) - self._original_msg_count[self.foldername]
except IndexError:
return 0
return int(messages[0])
def dump_imap_structures(self, dir, file=None):
if file is None:
file = sys.stdout
def dump_imap_structures(self, dir, file):
ac = self.account
acinfo = ac.logid + "-" + ac.get_config("addr")
@@ -138,10 +135,11 @@ class ImapConn:
body = data[0][1]
typ, data = c.fetch(num, '(UID FLAGS)')
info = data[0]
info = data[0].decode()
path = pathlib.Path(dir.strpath).joinpath("IMAP-MESSAGES", acinfo, imapfolder)
path.mkdir(parents=True, exist_ok=True)
fn = path.joinpath(str(info).replace("b'", "").replace("'", "").replace("\\", ""))
num = info.split()[0]
fn = path.joinpath(num)
fn.write_bytes(body)
log("Message", info, "saved as", fn)

View File

@@ -389,11 +389,12 @@ def acfactory(pytestconfig, tmpdir, request, session_liveconfig, data):
am = AccountMaker()
request.addfinalizer(am.finalize)
yield am
if request.node.rep_call.failed:
if hasattr(request.node, "rep_call") and request.node.rep_call.failed:
file = io.StringIO()
am.dump_imap_structures(file=file)
s = file.getvalue()
request.node.add_report_section("call", "imap-server-state", s)
print(s)
# request.node.add_report_section("call", "imap-server-state", s)
class BotProcess:
@@ -461,6 +462,7 @@ def lp():
def step(self, msg):
print("-" * 5, "step " + msg, "-" * 5)
return Printer()