PICARD-2232: Add helper functions to get official server for submission

Use those functions in addrelease instead of custom implementation
This commit is contained in:
Philipp Wolfer
2021-06-14 21:45:44 +02:00
parent 512edd656a
commit 2e4ef20744
3 changed files with 107 additions and 7 deletions

View File

@@ -23,12 +23,11 @@ from secrets import token_bytes
from PyQt5.QtCore import QCoreApplication
from picard import log
from picard.config import get_config
from picard.const import MUSICBRAINZ_SERVERS
from picard.util import (
format_time,
htmlescape,
)
from picard.util.mbserver import get_submission_host
from picard.util.webbrowser2 import open
@@ -140,11 +139,9 @@ def _find_file(path):
def _mbserver_url(path):
config = get_config()
host = config.setting["server_host"]
if host not in MUSICBRAINZ_SERVERS:
host = MUSICBRAINZ_SERVERS[0] # Submission only works to official servers
return "https://%s%s" % (host, path)
host, port = get_submission_host()
protocol = 'https' if port == 443 else 'http'
return "%s://%s:%i%s" % (protocol, host, port, path)
def _get_cluster_form(cluster):

50
picard/util/mbserver.py Normal file
View File

@@ -0,0 +1,50 @@
# -*- coding: utf-8 -*-
#
# Picard, the next-generation MusicBrainz tagger
#
# Copyright (C) 2021 Philipp Wolfer
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
from picard.config import get_config
from picard.const import MUSICBRAINZ_SERVERS
def is_official_server(host):
"""Returns True, if host is an official MusicBrainz server for the primary database.
Args:
host: the hostname
Returns: True, if host is an official MusicBrainz server, False otherwise
"""
return host in MUSICBRAINZ_SERVERS
def get_submission_host():
"""Returns the host and port used for data submission.
Data submission usually should be done against the primary database. This function
will return the hostname configured as `server_host` if it is an official MusicBrainz
server, otherwise it will return the primary official server.
Returns: Tuple of hostname and port number, e.g. `('musicbrainz.org', 443)`
"""
config = get_config()
host = config.setting['server_host']
if is_official_server(host):
return (host, 443)
else:
return (MUSICBRAINZ_SERVERS[0], 443)

View File

@@ -0,0 +1,53 @@
# -*- coding: utf-8 -*-
#
# Picard, the next-generation MusicBrainz tagger
#
# Copyright (C) 2021 Philipp Wolfer
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
from test.picardtestcase import PicardTestCase
from picard.const import MUSICBRAINZ_SERVERS
from picard.util.mbserver import (
get_submission_host,
is_official_server,
)
class IsOfficialServerTest(PicardTestCase):
def test_official(self):
for host in MUSICBRAINZ_SERVERS:
self.assertTrue(is_official_server(host))
def test_not_official(self):
self.assertFalse(is_official_server('test.musicbrainz.org'))
self.assertFalse(is_official_server('example.com'))
self.assertFalse(is_official_server('127.0.0.1'))
self.assertFalse(is_official_server('localhost'))
class test_get_submission_host(PicardTestCase):
def test_official(self):
for host in MUSICBRAINZ_SERVERS:
self.set_config_values(setting={'server_host': host, 'server_port': 80})
self.assertEqual((host, 443), get_submission_host())
def test_unofficial(self):
self.set_config_values(setting={'server_host': 'test.musicbrainz.org', 'server_port': 80})
self.assertEqual((MUSICBRAINZ_SERVERS[0], 443), get_submission_host())