✨ PMGR: Support version ranges for notices
This commit is contained in:
1 parent
9f289a0e9c
commit
ef8903902c
3 files changed
+108
-7
No files matched your search
@@ -37,6 +37,7 @@ import threading
|
||||
import tempfile
|
||||
import shutil
|
||||
import filetype
|
||||
import pkg_resources
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
@@ -1358,18 +1359,12 @@ class PluginManagerPlugin(octoprint.plugin.SimpleApiPlugin,
|
||||
octoprint_version = get_octoprint_version(base=True)
|
||||
plugin_notifications = self._notices.get(key, [])
|
||||
|
||||
def filter_relevant(notification):
|
||||
return "text" in notification and "date" in notification and \
|
||||
("versions" not in notification or plugin.version in notification["versions"]) and \
|
||||
("octoversions" not in notification or is_octoprint_compatible(*notification["octoversions"],
|
||||
octoprint_version=octoprint_version))
|
||||
|
||||
def map_notification(notification):
|
||||
return self._to_external_notification(key, notification)
|
||||
|
||||
return list(filter(lambda x: x is not None,
|
||||
map(map_notification,
|
||||
filter(filter_relevant,
|
||||
filter(lambda n: _filter_relevant_notification(n, plugin.version, octoprint_version),
|
||||
plugin_notifications))))
|
||||
|
||||
def _to_external_notification(self, key, notification):
|
||||
@@ -1381,6 +1376,30 @@ class PluginManagerPlugin(octoprint.plugin.SimpleApiPlugin,
|
||||
important=notification.get("important", False))
|
||||
|
||||
|
||||
def _filter_relevant_notification(notification, plugin_version, octoprint_version):
|
||||
if "pluginversions" in notification:
|
||||
pluginversions = notification["pluginversions"]
|
||||
|
||||
is_range = lambda x: "=" in x or ">" in x or "<" in x
|
||||
version_ranges = list(map(lambda x: pkg_resources.Requirement.parse(notification["plugin"] + x),
|
||||
filter(is_range,
|
||||
pluginversions)))
|
||||
versions = list(filter(lambda x: not is_range(x),
|
||||
pluginversions))
|
||||
elif "versions" in notification:
|
||||
version_ranges = []
|
||||
versions = notification["versions"]
|
||||
else:
|
||||
version_ranges = versions = None
|
||||
|
||||
return "text" in notification and "date" in notification and \
|
||||
((version_ranges is None and versions is None) or
|
||||
(version_ranges and (any(map(lambda v: plugin_version in v, version_ranges)))) or
|
||||
(versions and plugin_version in versions)) and \
|
||||
("octoversions" not in notification or is_octoprint_compatible(*notification["octoversions"],
|
||||
octoprint_version=octoprint_version))
|
||||
|
||||
|
||||
def _register_custom_events(*args, **kwargs):
|
||||
return ["install_plugin", "uninstall_plugin", "enable_plugin", "disable_plugin"]
|
||||
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||
@@ -0,0 +1,80 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||
|
||||
import unittest
|
||||
import ddt
|
||||
|
||||
@ddt.ddt
|
||||
class NotificationFilterTest(unittest.TestCase):
|
||||
|
||||
@ddt.data(
|
||||
(dict(plugin="foo",
|
||||
text="text",
|
||||
date="2020-09-15 00:00:00Z"),
|
||||
"1.0.0",
|
||||
"1.5.0",
|
||||
True),
|
||||
|
||||
(dict(plugin="foo",
|
||||
text="text",
|
||||
date="2020-09-15 00:00:00Z",
|
||||
pluginversions=[">=1.0.0"]),
|
||||
"1.0.0",
|
||||
"1.5.0",
|
||||
True),
|
||||
|
||||
(dict(plugin="foo",
|
||||
text="text",
|
||||
date="2020-09-15 00:00:00Z",
|
||||
pluginversions=[">=2.0.0", "0.9.0", "0.9.1", "1.0.0"]),
|
||||
"1.0.0",
|
||||
"1.5.0",
|
||||
True),
|
||||
|
||||
(dict(plugin="foo",
|
||||
text="text",
|
||||
date="2020-09-15 00:00:00Z",
|
||||
pluginversions=[">=2.0.0", "0.9.0", "0.9.1", "1.0.0"]),
|
||||
"1.1.0",
|
||||
"1.5.0",
|
||||
False),
|
||||
|
||||
(dict(plugin="foo",
|
||||
text="text",
|
||||
date="2020-09-15 00:00:00Z",
|
||||
versions=["0.9.0", "0.9.1", "1.0.0", "1.0.1"]),
|
||||
"1.0.0",
|
||||
"1.5.0",
|
||||
True),
|
||||
|
||||
(dict(plugin="foo",
|
||||
text="text",
|
||||
date="2020-09-15 00:00:00Z",
|
||||
versions=["0.9.0", "0.9.1", "1.0.0", "1.0.1"]),
|
||||
"1.1.0",
|
||||
"1.5.0",
|
||||
False),
|
||||
|
||||
(dict(plugin="foo",
|
||||
text="text",
|
||||
date="2020-09-15 00:00:00Z",
|
||||
octoversions=["1.5.0"]),
|
||||
"1.0.0",
|
||||
"1.5.0",
|
||||
True),
|
||||
|
||||
(dict(plugin="foo",
|
||||
text="text",
|
||||
date="2020-09-15 00:00:00Z",
|
||||
octoversions=["==1.4.2"]),
|
||||
"1.0.0",
|
||||
"1.5.0",
|
||||
False),
|
||||
)
|
||||
@ddt.unpack
|
||||
def test_notification_filter(self, notification, plugin_version, octoprint_version, expected):
|
||||
from octoprint.plugins.pluginmanager import _filter_relevant_notification
|
||||
|
||||
result = _filter_relevant_notification(notification, plugin_version, octoprint_version)
|
||||
|
||||
self.assertEqual(expected, result)
|
||||
Reference in new issue
Block a user