Skip to content

Commit

Permalink
[Plugins] Fix missing description with metadata 2.1
Browse files Browse the repository at this point in the history
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
  • Loading branch information
cas-- committed Jan 12, 2022
1 parent 2351d65 commit c3cd7f5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
25 changes: 7 additions & 18 deletions deluge/pluginmanagerbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@


"""PluginManagerBase"""
import email
import logging
import os.path

Expand Down Expand Up @@ -266,25 +267,13 @@ def get_plugin_info(self, name):

@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
22 changes: 21 additions & 1 deletion deluge/tests/test_plugin_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,30 @@ def test_get_plugin_info(self):
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)

0 comments on commit c3cd7f5

Please sign in to comment.