Skip to content

Commit

Permalink
Moved MailHandler creation into a nice factory function
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Hoad committed Jul 5, 2011
1 parent 6da55b1 commit b3661ee
Showing 1 changed file with 31 additions and 16 deletions.
47 changes: 31 additions & 16 deletions makeme.py
Expand Up @@ -28,6 +28,14 @@ def __init__(self, username, password, smtp_server, smtp_port, imap_server, imap
self._login_imap()
self._login_smtp()

def __del__(self):
"""Clean up resources. Logs out IMAP and SMTP clients."""
self.imap.logout()
self.imap.close()

self.smtp.logout()
self.smtp.close()

def _login_imap(self):
"""Log in to the IMAP server. Set self.imap to the connection object."""
pass
Expand Down Expand Up @@ -72,6 +80,22 @@ def _act(self, message):
"""
logging.info('_act() not yet implemented')

def _get_mailhandler(self):
"""Return a MailHandler object built from the config."""
get = self.config.get
getint = self.config.getint
getboolean = self.config.getboolean
username = get('settings', 'username')
password = get('settings', 'password')
smtp_server = get('settings', 'smtp_server')
smtp_port = getint('settings', 'smtp_port')
imap_server = get('settings', 'imap_server')
imap_port = getint('settings', 'imap_port')
use_tls = getboolean('settings', 'smtp_tls')
use_ssl = getboolean('settings', 'imap_ssl')

return MailHandler(username, password, smtp_server, smtp_port, imap_server, imap_port, use_ssl, use_tls)

def _load_config(self):
"""Load global (/usr/share/makeme/makeme.conf) and user-level ($HOME/.makeme/makeme.conf) config files."""
config = dict()
Expand Down Expand Up @@ -116,22 +140,10 @@ def check_messages(self):
"""Check for messages and call _act() on each one."""
logging.info('Checking messages...')

get = self.config.get
getint = self.config.getint
getboolean = self.config.getboolean
username = get('settings', 'username')
password = get('settings', 'password')
smtp_server = get('settings', 'smtp_server')
smtp_port = getint('settings', 'smtp_port')
imap_server = get('settings', 'imap_server')
imap_port = getint('settings', 'imap_port')
use_tls = getboolean('settings', 'smtp_tls')
use_ssl = getboolean('settings', 'imap_ssl')

messages = MailHandler(username, password, smtp_server, smtp_port, imap_server, imap_port, use_ssl, use_tls).get_messages()
messages = self._get_mailhandler().get_messages()

if not messages:
logging.info('Received no messages')
if messages:
logging.info('Received {} messages'.format(len(messages)))

for m in messages:
self._act(m)
Expand All @@ -147,7 +159,7 @@ def shutdown(self):
def stop(self):
"""Stop the server."""
logging.info('Stopping Makeme')
self._running = Falsezzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
self._running = False

def wait(self):
"""Sleep until the message queue should be checked."""
Expand All @@ -164,6 +176,8 @@ def __init__(self):
def _parse_argv(self):
"""Parse sys.argv for options, overloading those set in the config."""
print('argv parsing not yet implemented.', file=sys.stderr)


try:
if __name__ == '__main__':
server = MakeMeCLI()
Expand All @@ -176,6 +190,7 @@ def _parse_argv(self):
wait()

server.shutdown()

except KeyboardInterrupt:
server.shutdown()
except (configparser.NoSectionError, configparser.NoOptionError) as e:
Expand Down

0 comments on commit b3661ee

Please sign in to comment.