diff options
author | Lucas Hoffmann <lucc@users.noreply.github.com> | 2017-01-27 10:27:57 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-01-27 10:27:57 +0100 |
commit | 9d0a67842e5a9c85f08d619d60360cb90255b6a5 (patch) | |
tree | 7f1709304d33c7e4db6c568a265515a1909fd902 /alot/settings | |
parent | b7497be293683eb5fe6b761939dd245de2b292e8 (diff) | |
parent | 83d706d86142c693cdc92c4824b38c96ad0e4988 (diff) |
Merge pull request #1000 from dcbaker/pr/argument-validator
argparse validators
Diffstat (limited to 'alot/settings')
-rw-r--r-- | alot/settings/checks.py | 143 | ||||
-rw-r--r-- | alot/settings/manager.py | 19 | ||||
-rw-r--r-- | alot/settings/theme.py | 13 |
3 files changed, 13 insertions, 162 deletions
diff --git a/alot/settings/checks.py b/alot/settings/checks.py deleted file mode 100644 index 125d1860..00000000 --- a/alot/settings/checks.py +++ /dev/null @@ -1,143 +0,0 @@ -# Copyright (C) 2011-2012 Patrick Totzke <patricktotzke@gmail.com> -# This file is released under the GNU GPL, version 3 or a later revision. -# For further details see the COPYING file -from __future__ import absolute_import - -import mailbox -import re -from urwid import AttrSpec, AttrSpecError -from urlparse import urlparse -from validate import VdtTypeError -from validate import is_list -from validate import ValidateError, VdtValueTooLongError, VdtValueError - -from .. import crypto -from ..errors import GPGProblem - - -def attr_triple(value): - """ - Check that interprets the value as `urwid.AttrSpec` triple for the colour - modes 1,16 and 256. It assumes a <6 tuple of attribute strings for - mono foreground, mono background, 16c fg, 16c bg, 256 fg and 256 bg - respectively. If any of these are missing, we downgrade to the next - lower available pair, defaulting to 'default'. - - :raises: VdtValueTooLongError, VdtTypeError - :rtype: triple of `urwid.AttrSpec` - """ - keys = ['dfg', 'dbg', '1fg', '1bg', '16fg', '16bg', '256fg', '256bg'] - acc = {} - if not isinstance(value, (list, tuple)): - value = value, - if len(value) > 6: - raise VdtValueTooLongError(value) - # ensure we have exactly 6 attribute strings - attrstrings = (value + (6 - len(value)) * [None])[:6] - # add fallbacks for the empty list - attrstrings = (2 * ['default']) + attrstrings - for i, value in enumerate(attrstrings): - if value: - acc[keys[i]] = value - else: - acc[keys[i]] = acc[keys[i - 2]] - try: - mono = AttrSpec(acc['1fg'], acc['1bg'], 1) - normal = AttrSpec(acc['16fg'], acc['16bg'], 16) - high = AttrSpec(acc['256fg'], acc['256bg'], 256) - except AttrSpecError as e: - raise ValidateError(e.message) - return mono, normal, high - - -def align_mode(value): - """ - test if value is one of 'left', 'right' or 'center' - """ - if value not in ['left', 'right', 'center']: - raise VdtValueError - return value - - -def width_tuple(value): - """ - test if value is a valid width indicator (for a sub-widget in a column). - This can either be - ('fit', min, max): use the length actually needed for the content, padded - to use at least width min, and cut of at width max. - Here, min and max are positive integers or 0 to disable - the boundary. - ('weight',n): have it relative weight of n compared to other columns. - Here, n is an int. - """ - if value is None: - res = 'fit', 0, 0 - elif not isinstance(value, (list, tuple)): - raise VdtTypeError(value) - elif value[0] not in ['fit', 'weight']: - raise VdtTypeError(value) - if value[0] == 'fit': - if not isinstance(value[1], int) or not isinstance(value[2], int): - VdtTypeError(value) - res = 'fit', int(value[1]), int(value[2]) - else: - if not isinstance(value[1], int): - VdtTypeError(value) - res = 'weight', int(value[1]) - return res - - -def mail_container(value): - """ - Check that the value points to a valid mail container, - in URI-style, e.g.: `mbox:///home/username/mail/mail.box`. - The value is cast to a :class:`mailbox.Mailbox` object. - """ - if not re.match(r'.*://.*', value): - raise VdtTypeError(value) - mburl = urlparse(value) - if mburl.scheme == 'mbox': - box = mailbox.mbox(mburl.path) - elif mburl.scheme == 'maildir': - box = mailbox.Maildir(mburl.path) - elif mburl.scheme == 'mh': - box = mailbox.MH(mburl.path) - elif mburl.scheme == 'babyl': - box = mailbox.Babyl(mburl.path) - elif mburl.scheme == 'mmdf': - box = mailbox.MMDF(mburl.path) - else: - raise VdtTypeError(value) - return box - - -def force_list(value, min=None, max=None): - r""" - Check that a value is a list, coercing strings into - a list with one member. - - You can optionally specify the minimum and maximum number of members. - A minumum of greater than one will fail if the user only supplies a - string. - - The difference to :func:`validate.force_list` is that this test - will return an empty list instead of `['']` if the config value - matches `r'\s*,?\s*'`. - """ - if not isinstance(value, (list, tuple)): - value = [value] - rlist = is_list(value, min, max) - if rlist == ['']: - rlist = [] - return rlist - - -def gpg_key(value): - """ - test if value points to a known gpg key - and return that key as :class:`pyme.pygpgme._gpgme_key`. - """ - try: - return crypto.get_key(value) - except GPGProblem as e: - raise ValidateError(e.message) diff --git a/alot/settings/manager.py b/alot/settings/manager.py index 7ec79347..dcdd853a 100644 --- a/alot/settings/manager.py +++ b/alot/settings/manager.py @@ -14,15 +14,11 @@ from ..account import SendmailAccount from ..addressbook.abook import AbookAddressBook from ..addressbook.external import ExternalAddressbook from ..helper import pretty_datetime, string_decode +from ..utils import configobj as checks from .errors import ConfigError from .utils import read_config from .utils import resolve_att -from .checks import force_list -from .checks import mail_container -from .checks import gpg_key -from .checks import attr_triple -from .checks import align_mode from .theme import Theme @@ -60,12 +56,13 @@ class SettingsManager(object): def read_config(self, path): """parse alot's config file from path""" spec = os.path.join(DEFAULTSPATH, 'alot.rc.spec') - newconfig = read_config(path, spec, - checks={'mail_container': mail_container, - 'force_list': force_list, - 'align': align_mode, - 'attrtriple': attr_triple, - 'gpg_key_hint': gpg_key}) + newconfig = read_config( + path, spec, checks={ + 'mail_container': checks.mail_container, + 'force_list': checks.force_list, + 'align': checks.align_mode, + 'attrtriple': checks.attr_triple, + 'gpg_key_hint': checks.gpg_key}) self._config.merge(newconfig) hooks_path = os.path.expanduser(self._config.get('hooksfile')) diff --git a/alot/settings/theme.py b/alot/settings/theme.py index c05a4db0..f2a59763 100644 --- a/alot/settings/theme.py +++ b/alot/settings/theme.py @@ -5,11 +5,8 @@ from __future__ import absolute_import import os +from ..utils import configobj as checks from .utils import read_config -from .checks import align_mode -from .checks import attr_triple -from .checks import width_tuple -from .checks import force_list from .errors import ConfigError DEFAULTSPATH = os.path.join(os.path.dirname(__file__), '..', 'defaults') @@ -26,10 +23,10 @@ class Theme(object): """ self._spec = os.path.join(DEFAULTSPATH, 'theme.spec') self._config = read_config(path, self._spec, - checks={'align': align_mode, - 'widthtuple': width_tuple, - 'force_list': force_list, - 'attrtriple': attr_triple}) + checks={'align': checks.align_mode, + 'widthtuple': checks.width_tuple, + 'force_list': checks.force_list, + 'attrtriple': checks.attr_triple}) self._colours = [1, 16, 256] # make sure every entry in 'order' lists have their own subsections threadline = self._config['search']['threadline'] |