From 3c2cabcaa785af8efc6759aacf1e7a45a7c64fe6 Mon Sep 17 00:00:00 2001 From: David Glick Date: Sun, 16 Jun 2024 21:47:52 -0700 Subject: [PATCH] Avoid runtime dependency on plone.app.upgrade --- news/142.bugfix | 1 + src/plone/volto/bbb.py | 27 +++++++++++++++++++++++++++ src/plone/volto/patches.py | 2 +- 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 news/142.bugfix diff --git a/news/142.bugfix b/news/142.bugfix new file mode 100644 index 00000000..fdbf2500 --- /dev/null +++ b/news/142.bugfix @@ -0,0 +1 @@ +Remove runtime dependency on plone.app.upgrade. @davisagli diff --git a/src/plone/volto/bbb.py b/src/plone/volto/bbb.py index 906d6ded..a8b30aec 100644 --- a/src/plone/volto/bbb.py +++ b/src/plone/volto/bbb.py @@ -1,4 +1,8 @@ # flake8: noqa +from types import ModuleType + +import sys + try: from plone.base.interfaces import IPloneSiteRoot @@ -9,3 +13,26 @@ from plone.base.utils import get_installer except ImportError: from Products.CMFPlone.utils import get_installer + + +# This was copied from plone.app.upgrade +# to avoid a hard dependency on it. +def alias_module(name, target): + parts = name.split(".") + i = 0 + module = None + while i < len(parts) - 1: + i += 1 + module_name = ".".join(parts[:i]) + try: + __import__(module_name) + except ImportError: + new_module = ModuleType(module_name) + sys.modules[module_name] = new_module + if module is not None: + setattr(module, parts[i - 1], new_module) + module = sys.modules[module_name] + + setattr(module, parts[-1], target) + # also make sure sys.modules is updated + sys.modules[module_name + "." + parts[-1]] = target diff --git a/src/plone/volto/patches.py b/src/plone/volto/patches.py index 589eecd7..24cf4a9c 100644 --- a/src/plone/volto/patches.py +++ b/src/plone/volto/patches.py @@ -1,9 +1,9 @@ -from plone.app.upgrade.utils import alias_module from plone.registry.interfaces import IRegistry from plone.rest.interfaces import IAPIRequest from plone.volto import content from plone.volto import interfaces from plone.volto import logger +from plone.volto.bbb import alias_module from plone.volto.interfaces import IVoltoSettings from Products.SiteErrorLog.SiteErrorLog import _rate_restrict_burst from Products.SiteErrorLog.SiteErrorLog import _rate_restrict_period