tuskar-ui/horizon/loaders.py
Gabriel Hurley 1721ba9c4a Adds dash/panel app templates, mgmt commands, template loader.
Implements blueprint scaffolding.

Using custom management commands you can now create the majority
of the boilerplate code for a new dashboard or panel from a
set of basic templates with a single command. See the docs
for more info.

Additionally, in support of the new commands (and inherent
codified directory structure) there's a new template loader
included which can load templates from "templates" directories
in any registered panel.

Change-Id: I1df5eb152cb18694dc89d562799c8d3e8950ca6f
2012-05-01 14:41:20 -07:00

47 lines
1.4 KiB
Python

"""
Wrapper for loading templates from "templates" directories in panel modules.
"""
import os
from django.conf import settings
from django.template.base import TemplateDoesNotExist
from django.template.loader import BaseLoader
from django.utils._os import safe_join
# Set up a cache of the panel directories to search.
panel_template_dirs = {}
class TemplateLoader(BaseLoader):
is_usable = True
def get_template_sources(self, template_name):
dash_name, panel_name, remainder = template_name.split(os.path.sep, 2)
key = os.path.join(dash_name, panel_name)
if key in panel_template_dirs:
template_dir = panel_template_dirs[key]
try:
yield safe_join(template_dir, panel_name, remainder)
except UnicodeDecodeError:
# The template dir name wasn't valid UTF-8.
raise
except ValueError:
# The joined path was located outside of template_dir.
pass
def load_template_source(self, template_name, template_dirs=None):
for path in self.get_template_sources(template_name):
try:
file = open(path)
try:
return (file.read().decode(settings.FILE_CHARSET), path)
finally:
file.close()
except IOError:
pass
raise TemplateDoesNotExist(template_name)
_loader = TemplateLoader()