🐛 Add prefixes for plugin template resolution
Templates provided by plugins are now internally registered under "plugin_<identifier>/<template>". Automatically generated includes via the template subsystem have been adjusted to accomodate this change. For plugins still relying on the old single namespace imports, a fallback loader has been put in place that insures includes without prefix will continue to function for now but generated a deprecation message in the log. Fixes #4260
This commit is contained in:
1 parent
9990e432dd
commit
0ae536a895
4 files changed
+70
-9
No files matched your search
@@ -400,6 +400,10 @@ class TemplatePlugin(OctoPrintPlugin, ReloadNeedingPlugin):
|
||||
``TemplatePlugin`` is a :class:`~octoprint.plugin.core.ReloadNeedingPlugin`.
|
||||
"""
|
||||
|
||||
@property
|
||||
def template_folder_key(self):
|
||||
return "plugin_{}".format(self._identifier)
|
||||
|
||||
def get_template_configs(self):
|
||||
"""
|
||||
Allows configuration of injected navbar, sidebar, tab and settings templates (and also additional templates of
|
||||
|
||||
@@ -258,7 +258,8 @@ class Server(object):
|
||||
|
||||
self._lifecycle_callbacks = defaultdict(list)
|
||||
|
||||
self._template_searchpaths = []
|
||||
self._plugin_template_loader = None
|
||||
self._plugin_template_fallback_loader = None
|
||||
|
||||
self._intermediary_server = None
|
||||
|
||||
@@ -1543,12 +1544,14 @@ class Server(object):
|
||||
|
||||
import octoprint.util.jinja
|
||||
|
||||
filesystem_loader = octoprint.util.jinja.FilteredFileSystemLoader(
|
||||
[], path_filter=lambda x: not octoprint.util.is_hidden_path(x)
|
||||
self._plugin_template_loader = jinja2.PrefixLoader({})
|
||||
self._plugin_template_fallback_loader = (
|
||||
octoprint.util.jinja.FilteredFileSystemLoader(
|
||||
[], path_filter=lambda x: not octoprint.util.is_hidden_path(x)
|
||||
)
|
||||
)
|
||||
filesystem_loader.searchpath = self._template_searchpaths
|
||||
|
||||
loaders = [app.jinja_loader, filesystem_loader]
|
||||
loaders = [app.jinja_loader, self._plugin_template_loader]
|
||||
if octoprint.util.is_running_from_source():
|
||||
from markdown import markdown
|
||||
|
||||
@@ -1563,6 +1566,14 @@ class Server(object):
|
||||
)
|
||||
)
|
||||
|
||||
warning_message = "DeprecationWarning: Used relative path '{template}' to load/import plugin template '{filename}'. This is deprecated. You should switch to using paths with plugin prefix, e.g. 'plugin_example/{template}'"
|
||||
loaders.append(
|
||||
octoprint.util.jinja.WarningLoader(
|
||||
self._plugin_template_fallback_loader,
|
||||
warning_message,
|
||||
)
|
||||
)
|
||||
|
||||
jinja_loader = jinja2.ChoiceLoader(loaders)
|
||||
app.jinja_loader = jinja_loader
|
||||
|
||||
@@ -1737,14 +1748,32 @@ class Server(object):
|
||||
)
|
||||
|
||||
def _register_additional_template_plugin(self, plugin):
|
||||
import octoprint.util.jinja
|
||||
|
||||
folder = plugin.get_template_folder()
|
||||
if folder is not None and folder not in self._template_searchpaths:
|
||||
self._template_searchpaths.append(folder)
|
||||
if (
|
||||
folder is not None
|
||||
and plugin.template_folder_key not in self._plugin_template_loader.mapping
|
||||
):
|
||||
loader = octoprint.util.jinja.FilteredFileSystemLoader(
|
||||
[plugin.get_template_folder()],
|
||||
path_filter=lambda x: not octoprint.util.is_hidden_path(x),
|
||||
)
|
||||
|
||||
self._plugin_template_loader.mapping[plugin.template_folder_key] = loader
|
||||
self._plugin_template_fallback_loader.searchpath.append(folder)
|
||||
|
||||
def _unregister_additional_template_plugin(self, plugin):
|
||||
folder = plugin.get_template_folder()
|
||||
if folder is not None and folder in self._template_searchpaths:
|
||||
self._template_searchpaths.remove(folder)
|
||||
if (
|
||||
folder is not None
|
||||
and plugin.template_folder_key in self._plugin_template_loader.mapping
|
||||
):
|
||||
del self._plugin_template_loader.mapping[plugin.template_folder_key]
|
||||
try:
|
||||
self._plugin_template_fallback_loader.searchpath.remove(folder)
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
def _setup_blueprints(self):
|
||||
# do not remove or the index view won't be found
|
||||
|
||||
@@ -1493,6 +1493,12 @@ def _process_template_config(name, implementation, rule, config=None, counter=1)
|
||||
|
||||
if "template" not in data:
|
||||
data["template"] = rule["template"](name)
|
||||
data["template"] = implementation.template_folder_key + "/" + data["template"]
|
||||
|
||||
if "template_header" in data:
|
||||
data["template_header"] = (
|
||||
implementation.template_folder_key + "/" + data["template_header"]
|
||||
)
|
||||
|
||||
if "name" not in data:
|
||||
data["name"] = implementation._plugin_name
|
||||
|
||||
@@ -117,6 +117,28 @@ class SelectedFilesWithConversionLoader(SelectedFilesLoader):
|
||||
return contents
|
||||
|
||||
|
||||
class WarningLoader(BaseLoader):
|
||||
"""
|
||||
Logs a warning if the loader is used to successfully load a template.
|
||||
"""
|
||||
|
||||
def __init__(self, loader, warning_message):
|
||||
self.loader = loader
|
||||
self.warning_message = warning_message
|
||||
|
||||
def get_source(self, environment, template):
|
||||
import logging
|
||||
|
||||
try:
|
||||
contents, filename, uptodate = self.loader.get_source(environment, template)
|
||||
logging.getLogger(__name__).warning(
|
||||
self.warning_message.format(template=template, filename=filename)
|
||||
)
|
||||
return contents, filename, uptodate
|
||||
except TemplateNotFound:
|
||||
raise
|
||||
|
||||
|
||||
def get_all_template_paths(loader):
|
||||
def walk_folder(folder):
|
||||
files = []
|
||||
|
||||
Reference in new issue
Block a user