From 8bc29f30588b1fea0455208d3db5c47a1ae43a55 Mon Sep 17 00:00:00 2001 From: VG Date: Thu, 17 Dec 2015 17:44:00 +0100 Subject: makes the imap connection work in a context (with statement) --- climl_imap_bridge.py | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/climl_imap_bridge.py b/climl_imap_bridge.py index c0ddb82..be0864a 100644 --- a/climl_imap_bridge.py +++ b/climl_imap_bridge.py @@ -4,7 +4,7 @@ import os import subprocess import confparser import imapclient - +import contextlib import backports.ssl def conf_boolean_postprocess(src): @@ -23,6 +23,7 @@ def conf_postprocess(conf): return {key: d[key](value) if key in d else value for key, value in conf.items()} +@contextlib.contextmanager def connect_to_imap(conf, password): cafile = conf.get('imap.tls_ca', None) @@ -48,7 +49,16 @@ def connect_to_imap(conf, password): print('connection succeed') connection.login(username=conf.get('imap.username'), password=password) print('successfuly logged') - return connection + + try: + yield connection + except IMAPClient.AbortError, socket.error, socket.timeout, \ + backports.ssl.SSLError, backports.ssl.CertificateError: + raise + else: + connection.logout() + #finally: + # connection.shutdown() def main(): confpath = os.path.expanduser('~/temp/conf.cfg') @@ -59,8 +69,16 @@ def main(): password = subprocess.check_output(password_command, shell=True) password = password.rstrip().decode('utf8') print('got pasword:', password) - connection = connect_to_imap(conf, password) + #connection = connect_to_imap(conf, password) + with connect_to_imap(conf, password) as connection: + print('entering idle mode') + connection.idle() + print('waiting for an event') + result = connection.idle_check(timeout=10) + print('result:', result) + + print('end of connection') main() # dev mode for now -- cgit v1.2.3