From 97848bc1a2bf80896680ad45bac9933e3f782d83 Mon Sep 17 00:00:00 2001 From: Alexandre Petitjean Date: Wed, 31 Jul 2013 10:05:20 +0200 Subject: [PATCH] String config value can be optionnal AND have a choices list Integer value can be optionnal --- mopidy/backends/spotify/session_manager.py | 1 - mopidy/config/__init__.py | 4 ++-- mopidy/config/default.conf | 2 +- mopidy/config/types.py | 26 +++++++--------------- 4 files changed, 11 insertions(+), 22 deletions(-) diff --git a/mopidy/backends/spotify/session_manager.py b/mopidy/backends/spotify/session_manager.py index 130164fb..125b6ada 100644 --- a/mopidy/backends/spotify/session_manager.py +++ b/mopidy/backends/spotify/session_manager.py @@ -36,7 +36,6 @@ class SpotifySessionManager(process.BaseThread, PyspotifySessionManager): full_proxy = '' if config['proxy']['hostname']: full_proxy = config['proxy']['hostname'] - # Add proxy port and scheme only if available if config['proxy']['port']: full_proxy += ':' + str(config['proxy']['port']) if config['proxy']['scheme']: diff --git a/mopidy/config/__init__.py b/mopidy/config/__init__.py index 582a1a03..dfdae6a7 100644 --- a/mopidy/config/__init__.py +++ b/mopidy/config/__init__.py @@ -27,9 +27,9 @@ _audio_schema['output'] = String() _proxy_schema = ConfigSchema('proxy') _proxy_schema['scheme'] = String(optional=True, - choices=['', 'https', 'socks4', 'socks5']) + choices=['http', 'https', 'socks4', 'socks5']) _proxy_schema['hostname'] = Hostname(optional=True) -_proxy_schema['port'] = Port() +_proxy_schema['port'] = Port(optional=True) _proxy_schema['username'] = String(optional=True) _proxy_schema['password'] = Secret(optional=True) diff --git a/mopidy/config/default.conf b/mopidy/config/default.conf index d241323a..3899c2c5 100644 --- a/mopidy/config/default.conf +++ b/mopidy/config/default.conf @@ -15,6 +15,6 @@ output = autoaudiosink [proxy] scheme = hostname = -port = http +port = username = password = diff --git a/mopidy/config/types.py b/mopidy/config/types.py index 8715c8da..29651940 100644 --- a/mopidy/config/types.py +++ b/mopidy/config/types.py @@ -71,9 +71,9 @@ class String(ConfigValue): def deserialize(self, value): value = decode(value).strip() validators.validate_required(value, self._required) - validators.validate_choice(value, self._choices) if not value: return None + validators.validate_choice(value, self._choices) return value def serialize(self, value, display=False): @@ -111,12 +111,16 @@ class Secret(ConfigValue): class Integer(ConfigValue): """Integer value.""" - def __init__(self, minimum=None, maximum=None, choices=None): + def __init__(self, minimum=None, maximum=None, choices=None, optional=False): + self._required = not optional self._minimum = minimum self._maximum = maximum self._choices = choices def deserialize(self, value): + validators.validate_required(value, self._required) + if not value: + return None value = int(value) validators.validate_choice(value, self._choices) validators.validate_minimum(value, self._minimum) @@ -222,23 +226,9 @@ class Port(Integer): allocate a port for us. """ # TODO: consider probing if port is free or not? - def __init__(self, choices=None): + def __init__(self, choices=None, optional=False): super(Port, self).__init__( - minimum=0, maximum=2 ** 16 - 1, choices=choices) - - def deserialize(self, value): - try: - value = int(value) - except ValueError: - try: - value = socket.getservbyname(value, 'tcp') - except socket.error: - raise ValueError('must be a valid port number') - - validators.validate_choice(value, self._choices) - validators.validate_minimum(value, self._minimum) - validators.validate_maximum(value, self._maximum) - return value + minimum=0, maximum=2 ** 16 - 1, choices=choices, optional=optional) class Path(ConfigValue):