diff --git a/README.md b/README.md index 8c7db111..c9248ff3 100644 --- a/README.md +++ b/README.md @@ -166,6 +166,9 @@ The possible sections are: - Main section: `[main]` - `path_filter_fat`: whether to filter path components for FAT file systems - `path_filter_special`: whether to filter path components for special characters + +- MusicBrainz section: `[musicbrainz]` + - `server`: the MusicBrainz server to connect to, in `host:[port]` format. Defaults to `musicbrainz.org`. - Drive section: `[drive:IDENTIFIER]`, one for each configured drive. All these values are probed by whipper and should not be edited by hand. - `defeats_cache`: whether this drive can defeat the audio cache diff --git a/whipper/command/main.py b/whipper/command/main.py index 6e025478..04530c30 100644 --- a/whipper/command/main.py +++ b/whipper/command/main.py @@ -10,7 +10,7 @@ from whipper.command import cd, offset, drive, image, accurip, debug from whipper.command.basecommand import BaseCommand -from whipper.common import common, directory +from whipper.common import common, directory, config from whipper.extern.task import task from whipper.program.utils import eject_device @@ -22,6 +22,14 @@ def main(): # set user agent musicbrainzngs.set_useragent("whipper", whipper.__version__, "https://github.com/JoeLametta/whipper") + + try: + server = config.Config().get_musicbrainz_server() + except KeyError, e: + sys.stderr.write('whipper: %s\n' % e.message) + sys.exit() + + musicbrainzngs.set_hostname(server) # register plugins with pkg_resources distributions, _ = pkg_resources.working_set.find_plugins( pkg_resources.Environment([directory.data_path('plugins')]) diff --git a/whipper/common/config.py b/whipper/common/config.py index 30cdbead..2b7f7cbc 100644 --- a/whipper/common/config.py +++ b/whipper/common/config.py @@ -24,6 +24,7 @@ import shutil import tempfile import urllib +from urlparse import urlparse from whipper.common import directory @@ -72,6 +73,15 @@ def get(self, section, option): def getboolean(self, section, option): return self._getter('boolean', section, option) + # musicbrainz section + + def get_musicbrainz_server(self): + server = self.get('musicbrainz', 'server') or 'musicbrainz.org' + server_url = urlparse('//' + server) + if server_url.scheme != '' or server_url.path != '': + raise KeyError('Invalid MusicBrainz server: %s' % server) + return server + # drive sections def setReadOffset(self, vendor, model, release, offset): diff --git a/whipper/image/table.py b/whipper/image/table.py index 28dd04eb..77617812 100644 --- a/whipper/image/table.py +++ b/whipper/image/table.py @@ -28,7 +28,7 @@ import whipper -from whipper.common import common +from whipper.common import common, config import logging logger = logging.getLogger(__name__) @@ -390,7 +390,7 @@ def getMusicBrainzDiscId(self): return result def getMusicBrainzSubmitURL(self): - host = 'musicbrainz.org' + host = config.Config().get_musicbrainz_server() discid = self.getMusicBrainzDiscId() values = self._getMusicBrainzValues() diff --git a/whipper/test/test_common_config.py b/whipper/test/test_common_config.py index 26035fb4..cdfdbf28 100644 --- a/whipper/test/test_common_config.py +++ b/whipper/test/test_common_config.py @@ -66,3 +66,29 @@ def testDefeatsCache(self): defeats = self._config.getDefeatsCache( 'PLEXTOR ', 'DVDR PX-L890SA', '1.05') self.assertEquals(defeats, True) + + def test_get_musicbrainz_server(self): + self.assertEquals(self._config.get_musicbrainz_server(), + 'musicbrainz.org', + msg='Default value is correct') + + self._config._parser.add_section('musicbrainz') + + self._config._parser.set('musicbrainz', 'server', + '192.168.2.141:5000') + self._config.write() + self.assertEquals(self._config.get_musicbrainz_server(), + '192.168.2.141:5000', + msg='Correctly returns user-set value') + + self._config._parser.set('musicbrainz', 'server', + '192.168.2.141:5000/hello/world') + self._config.write() + self.assertRaises(KeyError, self._config.get_musicbrainz_server) + + self._config._parser.set('musicbrainz', 'server', + 'http://192.168.2.141:5000') + self._config.write() + self.assertRaises(KeyError, self._config.get_musicbrainz_server) + + self._config._parser.remove_section('musicbrainz')