From 2351d658449e94c409c2a71ade714a6ca19d793a Mon Sep 17 00:00:00 2001 From: Calum Lind Date: Wed, 12 Jan 2022 19:03:07 +0000 Subject: [PATCH] [Plugins] Fix and refactor get_plugin_info method A new metadata version 2.1 has optional Description that is causing an TypeError when looking up the key in plugin_info since clients are assuming values are always strings but the default is None. Fixed TypeError by ensuring that the info dict has a default empty string set for all keys. Separated the parsing of the pkg_info into static method to make it easier to test. Changed the missing plugin info to only set the Name and Version as 'not available' since all other fields are optional. Ref: https://dev.deluge-torrent.org/ticket/3476 Ref: https://packaging.python.org/en/latest/specifications/core-metadata/#description --- deluge/pluginmanagerbase.py | 37 +++++++++++++++++----------- deluge/tests/test_plugin_metadata.py | 5 ++-- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/deluge/pluginmanagerbase.py b/deluge/pluginmanagerbase.py index 0bf217264..da9590d48 100644 --- a/deluge/pluginmanagerbase.py +++ b/deluge/pluginmanagerbase.py @@ -254,28 +254,37 @@ class PluginManagerBase: def get_plugin_info(self, name): """Returns a dictionary of plugin info from the metadata""" - info = {}.fromkeys(METADATA_KEYS) - last_header = '' - cont_lines = [] - # Missing plugin info + if not self.pkg_env[name]: log.warning('Failed to retrieve info for plugin: %s', name) - for k in info: - info[k] = 'not available' + info = {}.fromkeys(METADATA_KEYS, '') + info['Name'] = info['Version'] = 'not available' return info - for line in self.pkg_env[name][0].get_metadata('PKG-INFO').splitlines(): + + pkg_info = self.pkg_env[name][0].get_metadata('PKG-INFO') + return self.parse_pkg_info(pkg_info) + + @staticmethod + def parse_pkg_info(pkg_info): + last_header = '' + cont_lines = [] + info = {}.fromkeys(METADATA_KEYS, '') + + for line in pkg_info.splitlines(): if not line: continue + if line[0] in ' \t' and ( len(line.split(':', 1)) == 1 or line.split(':', 1)[0] not in info ): # This is a continuation cont_lines.append(line.strip()) - else: - if cont_lines: - info[last_header] = '\n'.join(cont_lines).strip() - cont_lines = [] - if line.split(':', 1)[0] in info: - last_header = line.split(':', 1)[0] - info[last_header] = line.split(':', 1)[1].strip() + continue + + if cont_lines: + info[last_header] = '\n'.join(cont_lines).strip() + cont_lines = [] + if line.split(':', 1)[0] in info: + last_header = line.split(':', 1)[0] + info[last_header] = line.split(':', 1)[1].strip() return info diff --git a/deluge/tests/test_plugin_metadata.py b/deluge/tests/test_plugin_metadata.py index 341839e22..d86b245d8 100644 --- a/deluge/tests/test_plugin_metadata.py +++ b/deluge/tests/test_plugin_metadata.py @@ -20,9 +20,10 @@ class PluginManagerBaseTestCase(BaseTestCase): pm = PluginManagerBase('core.conf', 'deluge.plugin.core') for p in pm.get_available_plugins(): for key, value in pm.get_plugin_info(p).items(): - self.assertTrue(isinstance(f'{key}: {value}', ''.__class__)) + self.assertTrue(isinstance(f'{key}: {value}', str)) def test_get_plugin_info_invalid_name(self): pm = PluginManagerBase('core.conf', 'deluge.plugin.core') for key, value in pm.get_plugin_info('random').items(): - self.assertEqual(value, 'not available') + result = 'not available' if key in ('Name', 'Version') else '' + self.assertEqual(value, result)