From cfb929223e74ff860e1ec76b49e4dca6e159b396 Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Fri, 12 Sep 2014 21:28:32 -0700 Subject: [PATCH] Collapse some duplication in Confit templates --- beets/ui/__init__.py | 2 +- beets/util/confit.py | 34 +++++++++++++++------------------- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/beets/ui/__init__.py b/beets/ui/__init__.py index 37912dd46..7e3e7559c 100644 --- a/beets/ui/__init__.py +++ b/beets/ui/__init__.py @@ -830,7 +830,7 @@ def vararg_callback(option, opt_str, value, parser): def _load_plugins(config): """Load the plugins specified in the configuration. """ - paths = config['pluginpath'].get(confit.EnsureStringList()) + paths = config['pluginpath'].get(confit.StrSeq(split=False)) paths = map(util.normpath, paths) import beetsplug diff --git a/beets/util/confit.py b/beets/util/confit.py index 16a484b78..40cefc1fa 100644 --- a/beets/util/confit.py +++ b/beets/util/confit.py @@ -1055,15 +1055,27 @@ class Choice(Template): class StrSeq(Template): """A template for values that are lists of strings. - Validates both actual YAML string lists and whitespace-separated - strings. + Validates both actual YAML string lists and single strings. Strings + can optionally be split on whitespace. """ + def __init__(self, split=True): + """Create a new template. + + `split` indicates whether, when the underlying value is a single + string, it should be split on whitespace. Otherwise, the + resulting value is a list containing a single string. + """ + self.split = split + def convert(self, value, view): if isinstance(value, bytes): value = value.decode('utf8', 'ignore') if isinstance(value, STRING): - return value.split() + if self.split: + return value.split() + else: + return [value] else: try: value = list(value) @@ -1076,22 +1088,6 @@ class StrSeq(Template): self.fail('must be a list of strings', view, True) -class EnsureStringList(Template): - """Always return a list of strings. - - The raw value may either be a single string or a list of strings. - Otherwise a type error is raised. For single strings a singleton - list is returned. - """ - def convert(self, paths, view): - if isinstance(paths, basestring): - paths = [paths] - if not isinstance(paths, list) or \ - not all(map(lambda p: isinstance(p, basestring), paths)): - self.fail(u'must be string or a list of strings', view, True) - return paths - - class Filename(Template): """A template that validates strings as filenames.