Files
@ 29e10c6a1551
Branch filter:
Location: rattail-project/rattail/rattail/bouncer/config.py
29e10c6a1551
4.6 KiB
text/x-python
Add email attachment MIME type for MS Word .doc files
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | # -*- coding: utf-8 -*-
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2017 Lance Edgar
#
# This file is part of Rattail.
#
# Rattail is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# Rattail. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
Email Bouncer Configuration
"""
from __future__ import unicode_literals
from rattail.config import parse_list
from rattail.util import load_object
from rattail.exceptions import ConfigurationError
class Profile(object):
"""
Simple class to hold configuration for an email bouncer "profile". Each
profile determines how to connect to an IMAP folder, which handler to
invoke when new messages appear, etc. Each instance of this class has the
following attributes:
.. attribute:: config
Reference to the underlying configuration object from which the profile
derives its other attributes.
.. attribute:: key
String which differentiates this profile from any others which may exist
within the configuration.
.. attribute:: imap_server
Hostname or IP address of the IMAP server to which to connect.
.. attribute:: imap_username
Username to use when logging into the IMAP server.
.. attribute:: imap_password
Password to use when logging into the IMAP server.
.. attribute:: imap_folder
Folder to select when checking the IMAP server for new messages.
.. attribute:: handler_spec
Spec string for the handler. This must be a subclass of
:class:`rattail.bouncer:BounceHandler`.
.. attribute:: handler
Reference to the handler instance. This will be a subclass of
:class:`rattail.bouncer:BounceHandler`.
"""
def __init__(self, config, key):
self.config = config
self.key = key
self.imap_server = self._config_string('imap.server')
self.imap_username = self._config_string('imap.username')
self.imap_password = self._config_string('imap.password')
self.imap_folder_inbox = self._config_string('imap.inbox')
self.imap_folder_backup = self._config_string('imap.backup')
self.imap_delay = self._config_int('imap.delay', default=120)
self.imap_recycle = self._config_int('imap.recycle', default=1200)
self.workdir = self._config_string('workdir')
self.handler_spec = self._config_string('handler') or 'rattail.bouncer:BounceHandler'
self.handler = load_object(self.handler_spec)(config, key)
def _config_string(self, option):
return self.config.get('rattail.bouncer', '{0}.{1}'.format(self.key, option))
def _config_int(self, option, minimum=1, default=None):
option = '{0}.{1}'.format(self.key, option)
if self.config.has_option('rattail.bouncer', option):
value = self.config.getint('rattail.bouncer', option)
if value < minimum:
log.warning("config value {0} is too small; falling back to minimum "
"of {1} for option: {2}".format(value, minimum, option))
value = minimum
elif default is not None and default >= minimum:
value = default
else:
value = minimum
return value
def get_profile_keys(config):
"""
Returns a list of keys used in the bouncer configuration.
"""
keys = config.get('rattail.bouncer', 'watch')
if keys:
return parse_list(keys)
def load_profiles(config):
"""
Load all active email bouncer profiles defined within configuration.
"""
# Make sure we have a top-level directive.
keys = get_profile_keys(config)
if not keys:
raise ConfigurationError(
"The bouncer configuration does not specify any IMAP profiles to "
"be watched. Please defined the 'watch' option within the "
"[rattail.bouncer] section of your config file.")
watched = {}
for key in keys:
profile = Profile(config, key)
watched[key] = profile
return watched
|