forked from metabrainz/picard
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a way to select extra debug output
Web service replies can be very long, and it makes debug output rather hard to read. A debug option mechanism is now added, it is controlled by a new class `DebugOpt`. A command-line option `--debug-opts` allows selection of extra debug output, for now the sole option is `ws_replies`. A matching menu was added in View Debug Log dialog. It is important to note those aren't saved in config file, as they are meant to be disabled by default and only enabled when needed.
- Loading branch information
Showing
5 changed files
with
213 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Picard, the next-generation MusicBrainz tagger | ||
# | ||
# Copyright (C) 2024 Laurent Monin | ||
# | ||
# 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 enum import Enum | ||
|
||
|
||
class DebugOptEnum(int, Enum): | ||
__registry__ = set() | ||
|
||
def __new__(cls, value: int, title: str, description: str) -> None: | ||
value = int(value) | ||
obj = super().__new__(cls, value) | ||
obj._value_ = value | ||
obj.title = title | ||
obj.description = description | ||
return obj | ||
|
||
@property | ||
def optname(self): | ||
return self.name.lower() | ||
|
||
@property | ||
def enabled(self): | ||
return self in self.__registry__ | ||
|
||
@enabled.setter | ||
def enabled(self, enable: bool): | ||
if enable: | ||
self.__registry__.add(self) | ||
else: | ||
self.__registry__.discard(self) | ||
|
||
@classmethod | ||
def opt_names(cls): | ||
"""Returns a comma-separated list of all possible debug options""" | ||
return ','.join(sorted(o.optname for o in cls)) | ||
|
||
@classmethod | ||
def from_string(cls, string: str): | ||
"""Parse command line argument, a string with comma-separated values, | ||
and enable corresponding debug options""" | ||
opts = {str(o).strip().lower() for o in string.split(',')} | ||
for o in cls: | ||
o.enabled = o.optname in opts | ||
|
||
@classmethod | ||
def to_string(cls): | ||
"""Returns a comma-separated list of all enabled debug options""" | ||
return ','.join(sorted(o.optname for o in cls.__registry__)) | ||
|
||
@classmethod | ||
def set_registry(cls, registry: set): | ||
"""Defines a new set to store enabled debug options""" | ||
cls.__registry__ = registry | ||
|
||
@classmethod | ||
def get_registry(cls): | ||
"""Returns current storage for enabled debug options""" | ||
return cls.__registry__ | ||
|
||
|
||
class DebugOpt(DebugOptEnum): | ||
WS_REPLIES = 1, N_('WS Replies'), N_('Log web service replies') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Picard, the next-generation MusicBrainz tagger | ||
# | ||
# Copyright (C) 2024 Laurent Monin | ||
# | ||
# 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.debug_opts import DebugOptEnum | ||
|
||
|
||
class TestDebugOpt(DebugOptEnum): | ||
A = 1, 'titleA', 'descriptionA' | ||
B = 2, 'titleB', 'descriptionB' | ||
|
||
|
||
class DebugOptTest(PicardTestCase): | ||
def setUp(self): | ||
TestDebugOpt.set_registry(set()) | ||
|
||
def test_enabled(self): | ||
self.assertFalse(TestDebugOpt.A.enabled) | ||
self.assertFalse(TestDebugOpt.B.enabled) | ||
TestDebugOpt.A.enabled = True | ||
self.assertTrue(TestDebugOpt.A.enabled) | ||
|
||
def test_optname(self): | ||
self.assertEqual(TestDebugOpt.A.optname, 'a') | ||
self.assertEqual(TestDebugOpt.B.optname, 'b') | ||
|
||
def test_title(self): | ||
self.assertEqual(TestDebugOpt.A.title, 'titleA') | ||
self.assertEqual(TestDebugOpt.B.title, 'titleB') | ||
|
||
def test_description(self): | ||
self.assertEqual(TestDebugOpt.A.description, 'descriptionA') | ||
self.assertEqual(TestDebugOpt.B.description, 'descriptionB') | ||
|
||
def test_opt_names(self): | ||
self.assertEqual(TestDebugOpt.opt_names(), 'a,b') | ||
|
||
def test_from_string_simple(self): | ||
TestDebugOpt.from_string('a') | ||
self.assertTrue(TestDebugOpt.A.enabled) | ||
self.assertFalse(TestDebugOpt.B.enabled) | ||
TestDebugOpt.from_string('a,b') | ||
self.assertTrue(TestDebugOpt.A.enabled) | ||
self.assertTrue(TestDebugOpt.B.enabled) | ||
|
||
def test_from_string_complex(self): | ||
TestDebugOpt.from_string('something, A,x,b') | ||
self.assertTrue(TestDebugOpt.A.enabled) | ||
self.assertTrue(TestDebugOpt.B.enabled) | ||
|
||
def test_from_string_remove(self): | ||
TestDebugOpt.set_registry({TestDebugOpt.B}) | ||
self.assertTrue(TestDebugOpt.B.enabled) | ||
TestDebugOpt.from_string('A') | ||
self.assertTrue(TestDebugOpt.A.enabled) | ||
self.assertFalse(TestDebugOpt.B.enabled) | ||
|
||
def test_to_string(self): | ||
self.assertEqual('', TestDebugOpt.to_string()) | ||
TestDebugOpt.A.enabled = True | ||
self.assertEqual('a', TestDebugOpt.to_string()) | ||
TestDebugOpt.B.enabled = True | ||
self.assertEqual('a,b', TestDebugOpt.to_string()) | ||
|
||
def test_set_get_registry(self): | ||
old_set = TestDebugOpt.get_registry() | ||
TestDebugOpt.A.enabled = True | ||
self.assertTrue(TestDebugOpt.A.enabled) | ||
new_set = set() | ||
TestDebugOpt.set_registry(new_set) | ||
self.assertFalse(TestDebugOpt.A.enabled) | ||
TestDebugOpt.B.enabled = True | ||
self.assertFalse(TestDebugOpt.A.enabled) | ||
self.assertTrue(TestDebugOpt.B.enabled) | ||
TestDebugOpt.set_registry(old_set) | ||
self.assertTrue(TestDebugOpt.A.enabled) | ||
self.assertFalse(TestDebugOpt.B.enabled) |