From f6a0a6819034f6c009568b538db3bc6b93146a64 Mon Sep 17 00:00:00 2001 From: Laurent Monin Date: Fri, 12 Apr 2024 12:12:44 +0200 Subject: [PATCH] Introduce DebugOpt.PLUGIN_FULLPATH and shorten plugin paths in logs --- picard/debug_opts.py | 1 + picard/log.py | 7 +++++++ test/test_log.py | 49 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+) diff --git a/picard/debug_opts.py b/picard/debug_opts.py index 607db2bc710..328be1404ff 100644 --- a/picard/debug_opts.py +++ b/picard/debug_opts.py @@ -78,3 +78,4 @@ def get_registry(cls): class DebugOpt(DebugOptEnum): WS_REPLIES = 1, N_('Web Service Replies'), N_('Log content of web service replies') + PLUGIN_FULLPATH = 2, N_('Plugin Fullpath'), N_('Log plugin full paths') diff --git a/picard/log.py b/picard/log.py index 2baf78964e7..559d7cdecd3 100644 --- a/picard/log.py +++ b/picard/log.py @@ -45,6 +45,7 @@ FROZEN_TEMP_PATH, IS_FROZEN, ) +from picard.debug_opts import DebugOpt # Get the absolute path for the picard module @@ -176,6 +177,12 @@ def name_filter(record): path = path.resolve().relative_to(picard_module_path) except ValueError: pass + else: + if not DebugOpt.PLUGIN_FULLPATH.enabled and 'plugins' in path.parts: + parts = list(reversed(path.parts)) + parts = parts[:parts.index('plugins') + 1] + path = Path(*reversed(parts)) + parts = list(path.parts) if parts[-1] == '__init__': del parts[-1] diff --git a/test/test_log.py b/test/test_log.py index e1f6d0023ee..98affbfb5a6 100644 --- a/test/test_log.py +++ b/test/test_log.py @@ -33,6 +33,7 @@ from test.picardtestcase import PicardTestCase from picard.const.sys import IS_WIN +from picard.debug_opts import DebugOpt from picard.log import ( _calculate_bounds, name_filter, @@ -161,6 +162,18 @@ def test_5(self): self.assertTrue(name_filter(record)) self.assertEqual(record.name, '__init__/module') + def test_plugin_path_long(self): + DebugOpt.PLUGIN_FULLPATH.enabled = True + record = FakeRecord(name=None, pathname='/path1/path2/plugins/path3/plugins/plugin.zip') + self.assertTrue(name_filter(record)) + self.assertEqual(record.name, 'plugins/path3/plugins/plugin') + + def test_plugin_path_short(self): + DebugOpt.PLUGIN_FULLPATH.enabled = False + record = FakeRecord(name=None, pathname='/path1/path2/plugins/path3/plugins/plugin.zip') + self.assertTrue(name_filter(record)) + self.assertEqual(record.name, 'plugins/plugin') + @unittest.skipIf(IS_WIN, "Posix test") @patch('picard.log.picard_module_path', PurePosixPath('/picard')) @@ -186,6 +199,18 @@ def test_4(self): with self.assertRaises(ValueError): name_filter(record) + def test_plugin_path_long(self): + DebugOpt.PLUGIN_FULLPATH.enabled = True + record = FakeRecord(name=None, pathname='/path1/plugins/path2/plugins/plugin.zip') + self.assertTrue(name_filter(record)) + self.assertEqual(record.name, 'path1/plugins/path2/plugins/plugin') + + def test_plugin_path_short(self): + DebugOpt.PLUGIN_FULLPATH.enabled = False + record = FakeRecord(name=None, pathname='/path1/plugins/path2/plugins/plugin.zip') + self.assertTrue(name_filter(record)) + self.assertEqual(record.name, 'path1/plugins/path2/plugins/plugin') + @unittest.skipIf(IS_WIN, "Posix test") @patch('picard.log.picard_module_path', PurePosixPath('/path1/path2/')) # incorrect, but testing anyway @@ -226,6 +251,18 @@ def test_5(self): self.assertTrue(name_filter(record)) self.assertEqual(record.name, '__init__/module') + def test_plugin_path_long(self): + DebugOpt.PLUGIN_FULLPATH.enabled = True + record = FakeRecord(name=None, pathname='C:/path1/path2/plugins/path3/plugins/plugin.zip') + self.assertTrue(name_filter(record)) + self.assertEqual(record.name, 'plugins/path3/plugins/plugin') + + def test_plugin_path_short(self): + DebugOpt.PLUGIN_FULLPATH.enabled = False + record = FakeRecord(name=None, pathname='C:/path1/path2/plugins/path3/plugins/plugin.zip') + self.assertTrue(name_filter(record)) + self.assertEqual(record.name, 'plugins/plugin') + @unittest.skipUnless(IS_WIN, "Windows test") @patch('picard.log.picard_module_path', PureWindowsPath('C:/picard')) @@ -251,6 +288,18 @@ def test_4(self): with self.assertRaises(ValueError): name_filter(record) + def test_plugin_path_long(self): + DebugOpt.PLUGIN_FULLPATH.enabled = True + record = FakeRecord(name=None, pathname='C:/path1/plugins/path2/plugins/plugin.zip') + self.assertTrue(name_filter(record)) + self.assertEqual(record.name, 'path1/plugins/path2/plugins/plugin') + + def test_plugin_path_short(self): + DebugOpt.PLUGIN_FULLPATH.enabled = False + record = FakeRecord(name=None, pathname='C:/path1/plugins/path2/plugins/plugin.zip') + self.assertTrue(name_filter(record)) + self.assertEqual(record.name, 'path1/plugins/path2/plugins/plugin') + @unittest.skipUnless(IS_WIN, "Windows test") @patch('picard.log.picard_module_path', PureWindowsPath('C:/path1/path2/')) # incorrect, but testing anyway