Coverage for /home/martinb/.local/share/virtualenvs/camcops/lib/python3.6/site-packages/deform/template.py : 64%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""Template."""
2# Standard Library
3import os.path
5from chameleon.zpt.loader import TemplateLoader
6from pkg_resources import resource_filename
7from translationstring import ChameleonTranslate
9from .exception import TemplateError
12BOOLEAN_HTML_ATTRS = frozenset(
13 [
14 # List of Boolean attributes in HTML that should be rendered in
15 # minimized form (e.g. <img ismap> rather than <img ismap="">)
16 # From http://www.w3.org/TR/xhtml1/#guidelines (C.10)
17 "compact",
18 "nowrap",
19 "ismap",
20 "declare",
21 "noshade",
22 "checked",
23 "disabled",
24 "readonly",
25 "multiple",
26 "selected",
27 "noresize",
28 "defer",
29 ]
30)
33class ZPTTemplateLoader(TemplateLoader):
34 def __init__(self, *args, **kwargs):
35 kwargs.setdefault("encoding", "utf-8")
36 kwargs.setdefault("boolean_attributes", BOOLEAN_HTML_ATTRS)
37 super(ZPTTemplateLoader, self).__init__(*args, **kwargs)
39 def load(self, filename, *args, **kwargs):
40 if ":" in filename:
41 pkg_name, fn = filename.split(":", 1)
42 filename = resource_filename(pkg_name, fn)
43 else:
44 path, ext = os.path.splitext(filename)
45 if not ext:
46 filename = filename + ".pt"
47 try:
48 return super(ZPTTemplateLoader, self).load(
49 filename, *args, **kwargs
50 )
51 except ValueError:
52 raise TemplateError(filename)
55class ZPTRendererFactory(object):
56 """
57 Construct a custom Chameleon ZPT :term:`renderer` for Deform.
59 If the template name is an asset spec (has a colon in it, e.g.
60 ``mypackage:subdir/subdir2/mytemplate.pt``), use
61 ``pkg_resources.resource_filename`` to resolve it.
62 Otherwise, fall back to search-path-based machinery to resolve it.
64 Allowing an asset spec allows users to specify templates without the
65 trouble of needing to add search paths to the deform rendering machinery.
67 **Arguments**
69 search_path
70 A sequence of strings representing fully qualified filesystem
71 directories containing Deform Chameleon template sources. The
72 order in which the directories are listed within ``search_path``
73 is the order in which they are checked for the template provided
74 to the renderer. If every resource is an asset spec, however,
75 the search path is never used.
77 auto_reload
78 If true, automatically reload templates when they change (slows
79 rendering). Default: ``True``.
81 debug
82 If true, show nicer tracebacks during Chameleon template rendering
83 errors (slows rendering). Default: ``True``.
85 encoding
86 The encoding that the on-disk representation of the templates
87 and all non-ASCII values passed to the template should be
88 expected to adhere to. Default: ``utf-8``.
90 translator
91 A translation function used for internationalization when the
92 ``i18n:translate`` attribute syntax is used in the Chameleon
93 template is active or a
94 :class:`translationstring.TranslationString` is encountered
95 during output. It must accept a translation string and return
96 an interpolated translation. Default: ``None`` (no translation
97 performed).
98 """
100 def __init__(
101 self,
102 search_path,
103 auto_reload=True,
104 debug=False,
105 encoding="utf-8",
106 translator=None,
107 ):
108 self.translate = translator
109 loader = ZPTTemplateLoader(
110 search_path=search_path,
111 auto_reload=auto_reload,
112 debug=debug,
113 encoding=encoding,
114 translate=ChameleonTranslate(translator),
115 )
116 self.loader = loader
118 def __call__(self, template_name, **kw):
119 return self.load(template_name)(**kw)
121 def load(self, template_name):
122 return self.loader.load(template_name)
125default_dir = resource_filename("deform", "templates/")
126default_renderer = ZPTRendererFactory((default_dir,))