mirror of
https://git.deluge-torrent.org/deluge
synced 2025-08-08 09:28:41 +00:00
[Plugins] Fix missing description with metadata 2.1
Changes to the metadata specs in v2.1 meant that Description field might appear in the body of the message instead of as a header key. Replaced custom parser with email parser (as outlined in the document using compat32 policy) to simplify extracting the message header and body. Ref: https://dev.deluge-torrent.org/ticket/3476 Ref: https://packaging.python.org/en/latest/specifications/core-metadata/#description
This commit is contained in:
parent
2351d65844
commit
c3cd7f5e5c
2 changed files with 28 additions and 19 deletions
|
@ -8,6 +8,7 @@
|
||||||
|
|
||||||
|
|
||||||
"""PluginManagerBase"""
|
"""PluginManagerBase"""
|
||||||
|
import email
|
||||||
import logging
|
import logging
|
||||||
import os.path
|
import os.path
|
||||||
|
|
||||||
|
@ -266,25 +267,13 @@ class PluginManagerBase:
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def parse_pkg_info(pkg_info):
|
def parse_pkg_info(pkg_info):
|
||||||
last_header = ''
|
metadata_msg = email.message_from_string(pkg_info)
|
||||||
cont_lines = []
|
metadata_ver = metadata_msg.get('Metadata-Version')
|
||||||
info = {}.fromkeys(METADATA_KEYS, '')
|
|
||||||
|
|
||||||
for line in pkg_info.splitlines():
|
info = {key: metadata_msg.get(key, '') for key in METADATA_KEYS}
|
||||||
if not line:
|
|
||||||
continue
|
|
||||||
|
|
||||||
if line[0] in ' \t' and (
|
# Optional Description field in body (Metadata spec >=2.1)
|
||||||
len(line.split(':', 1)) == 1 or line.split(':', 1)[0] not in info
|
if not info['Description'] and metadata_ver.startswith('2'):
|
||||||
):
|
info['Description'] = metadata_msg.get_payload().strip()
|
||||||
# This is a continuation
|
|
||||||
cont_lines.append(line.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
|
return info
|
||||||
|
|
|
@ -20,10 +20,30 @@ class PluginManagerBaseTestCase(BaseTestCase):
|
||||||
pm = PluginManagerBase('core.conf', 'deluge.plugin.core')
|
pm = PluginManagerBase('core.conf', 'deluge.plugin.core')
|
||||||
for p in pm.get_available_plugins():
|
for p in pm.get_available_plugins():
|
||||||
for key, value in pm.get_plugin_info(p).items():
|
for key, value in pm.get_plugin_info(p).items():
|
||||||
self.assertTrue(isinstance(f'{key}: {value}', str))
|
self.assertIsInstance(key, str)
|
||||||
|
self.assertIsInstance(value, str)
|
||||||
|
|
||||||
def test_get_plugin_info_invalid_name(self):
|
def test_get_plugin_info_invalid_name(self):
|
||||||
pm = PluginManagerBase('core.conf', 'deluge.plugin.core')
|
pm = PluginManagerBase('core.conf', 'deluge.plugin.core')
|
||||||
for key, value in pm.get_plugin_info('random').items():
|
for key, value in pm.get_plugin_info('random').items():
|
||||||
result = 'not available' if key in ('Name', 'Version') else ''
|
result = 'not available' if key in ('Name', 'Version') else ''
|
||||||
self.assertEqual(value, result)
|
self.assertEqual(value, result)
|
||||||
|
|
||||||
|
def test_parse_pkg_info_metadata_2_1(self):
|
||||||
|
pkg_info = """Metadata-Version: 2.1
|
||||||
|
Name: AutoAdd
|
||||||
|
Version: 1.8
|
||||||
|
Summary: Monitors folders for .torrent files.
|
||||||
|
Home-page: http://dev.deluge-torrent.org/wiki/Plugins/AutoAdd
|
||||||
|
Author: Chase Sterling, Pedro Algarvio
|
||||||
|
Author-email: chase.sterling@gmail.com, pedro@algarvio.me
|
||||||
|
License: GPLv3
|
||||||
|
Platform: UNKNOWN
|
||||||
|
|
||||||
|
Monitors folders for .torrent files.
|
||||||
|
"""
|
||||||
|
plugin_info = PluginManagerBase.parse_pkg_info(pkg_info)
|
||||||
|
for value in plugin_info.values():
|
||||||
|
self.assertNotEqual(value, '')
|
||||||
|
result = 'Monitors folders for .torrent files.'
|
||||||
|
self.assertEqual(plugin_info['Description'], result)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue