diff --git a/mopidy/backends/spotify/session_manager.py b/mopidy/backends/spotify/session_manager.py index 3e8f1b67..dff29fad 100644 --- a/mopidy/backends/spotify/session_manager.py +++ b/mopidy/backends/spotify/session_manager.py @@ -33,10 +33,12 @@ class SpotifySessionManager(process.BaseThread, PyspotifySessionManager): self.cache_location = config['spotify']['cache_dir'] self.settings_location = config['spotify']['cache_dir'] - # Add proxy port only if available - full_proxy = config['proxy']['hostname'] - if 'port' in config['proxy']: - full_proxy = full_proxy + ':' + str(config['proxy']['port']) + full_proxy = '' + if config['proxy']['hostname']: + full_proxy = config['proxy']['hostname'] + # Add proxy port only if available + if config['proxy']['port']: + full_proxy = full_proxy + ':' + str(config['proxy']['port']) PyspotifySessionManager.__init__( self, config['spotify']['username'], config['spotify']['password'], diff --git a/mopidy/config/__init__.py b/mopidy/config/__init__.py index 7ccaa3f3..35a1d8e0 100644 --- a/mopidy/config/__init__.py +++ b/mopidy/config/__init__.py @@ -29,7 +29,7 @@ _proxy_schema = ConfigSchema('proxy') _proxy_schema['hostname'] = Hostname(optional=True) _proxy_schema['username'] = String(optional=True) _proxy_schema['password'] = Secret(optional=True) -_proxy_schema['port'] = Port(optional=True) +_proxy_schema['port'] = Port() # NOTE: if multiple outputs ever comes something like LogLevelConfigSchema #_outputs_schema = config.AudioOutputConfigSchema() diff --git a/mopidy/config/default.conf b/mopidy/config/default.conf index 204c789b..4a259c49 100644 --- a/mopidy/config/default.conf +++ b/mopidy/config/default.conf @@ -16,4 +16,4 @@ output = autoaudiosink hostname = username = password = -port = \ No newline at end of file +port = diff --git a/mopidy/config/types.py b/mopidy/config/types.py index 3dff0908..8309a591 100644 --- a/mopidy/config/types.py +++ b/mopidy/config/types.py @@ -117,10 +117,7 @@ class Integer(ConfigValue): self._choices = choices def deserialize(self, value): - try: - value = int(value) - except: - value = 0 + value = int(value) validators.validate_choice(value, self._choices) validators.validate_minimum(value, self._minimum) validators.validate_maximum(value, self._maximum) @@ -225,11 +222,29 @@ class Port(Integer): allocate a port for us. """ # TODO: consider probing if port is free or not? - def __init__(self, choices=None, optional=False): - self._required = not optional + def __init__(self, choices=None): super(Port, self).__init__( minimum=0, maximum=2 ** 16 - 1, choices=choices) + def deserialize(self, value): + # in case of no value is given, just return nothing + if not len(value): + return value + # now we can try to convert + try: + value = int(value) + except ValueError: + try: + value = socket.getservbyname(value, 'tcp') + except socket.error: + raise ValueError('must be a valid port number') + else: + validators.validate_choice(value, self._choices) + validators.validate_minimum(value, self._minimum) + validators.validate_maximum(value, self._maximum) + return value + + class Path(ConfigValue): """File system path