Imbox() usable as with statement
This commit is contained in:
@@ -30,11 +30,11 @@ Usage
|
||||
|
||||
# SSL Context docs https://docs.python.org/3/library/ssl.html#ssl.create_default_context
|
||||
|
||||
imbox = Imbox('imap.gmail.com',
|
||||
with Imbox('imap.gmail.com',
|
||||
username='username',
|
||||
password='password',
|
||||
ssl=True,
|
||||
ssl_context=None)
|
||||
ssl_context=None) as imbox:
|
||||
|
||||
# Gets all messages
|
||||
all_messages = imbox.messages()
|
||||
|
||||
@@ -21,6 +21,12 @@ class Imbox:
|
||||
logger.info("Connected to IMAP Server with user {username} on {hostname}{ssl}".format(
|
||||
hostname=hostname, username=username, ssl=(" over SSL" if ssl else "")))
|
||||
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
def __exit__(self, type, value, traceback):
|
||||
self.logout()
|
||||
|
||||
def logout(self):
|
||||
self.connection.close()
|
||||
self.connection.logout()
|
||||
|
||||
Reference in New Issue
Block a user