aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVG <vg@devys.org>2015-12-17 17:44:00 +0100
committerVG <vg@devys.org>2015-12-17 17:44:00 +0100
commit8bc29f30588b1fea0455208d3db5c47a1ae43a55 (patch)
tree7d36549ef0252aa28f750df668e7e3a207817fd7
parentee8e536d83e7fd38a0ad9f88a172fae956c15754 (diff)
downloadcliml-8bc29f30588b1fea0455208d3db5c47a1ae43a55.tar.gz
climl-8bc29f30588b1fea0455208d3db5c47a1ae43a55.tar.bz2
climl-8bc29f30588b1fea0455208d3db5c47a1ae43a55.zip
makes the imap connection work in a context (with statement)
-rw-r--r--climl_imap_bridge.py24
1 files 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