[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:
Calum Lind 2022-01-12 20:03:09 +00:00
parent 2351d65844
commit c3cd7f5e5c
No known key found for this signature in database
GPG Key ID: 90597A687B836BA3
2 changed files with 28 additions and 19 deletions

View File

@ -8,6 +8,7 @@
"""PluginManagerBase"""
import email
import logging
import os.path
@ -266,25 +267,13 @@ class PluginManagerBase:
@staticmethod
def parse_pkg_info(pkg_info):
last_header = ''
cont_lines = []
info = {}.fromkeys(METADATA_KEYS, '')
metadata_msg = email.message_from_string(pkg_info)
metadata_ver = metadata_msg.get('Metadata-Version')
for line in pkg_info.splitlines():
if not line:
continue
info = {key: metadata_msg.get(key, '') for key in METADATA_KEYS}
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())
continue
# Optional Description field in body (Metadata spec >=2.1)
if not info['Description'] and metadata_ver.startswith('2'):
info['Description'] = metadata_msg.get_payload().strip()
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

View File

@ -20,10 +20,30 @@ 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}', str))
self.assertIsInstance(key, str)
self.assertIsInstance(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():
result = 'not available' if key in ('Name', 'Version') else ''
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)