annotate roundup/cgi/engine_chameleon.py @ 7999:310e19beba3e

fix: report filename if template file is invalid A file name like .#issue.search.html causes a crash because _find returns None not a tuple and unpacking fails. Trap the TypeError and raise ValueError with message that includes the basename of the file.
author John Rouillard <rouilj@ieee.org>
date Sun, 26 May 2024 11:14:10 -0400
parents ac0802452818
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4587
a2eb4fb3e6d8 New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff changeset
1 """Templating engine adapter for the Chameleon."""
a2eb4fb3e6d8 New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff changeset
2
a2eb4fb3e6d8 New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff changeset
3 __docformat__ = 'restructuredtext'
a2eb4fb3e6d8 New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff changeset
4
4720
fd72576e07ed API break: rename Templates to Loader for zopetal and chameleon
anatoly techtonik <techtonik@gmail.com>
parents: 4719
diff changeset
5 import chameleon
4587
a2eb4fb3e6d8 New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff changeset
6
5418
55f09ca366c4 Python 3 preparation: StringIO.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5416
diff changeset
7 from roundup.cgi.templating import context, TALLoaderBase
5416
56c9bcdea47f Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents: 4749
diff changeset
8 from roundup.anypy.strings import s2u
4587
a2eb4fb3e6d8 New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff changeset
9
6063
4d20d8251bf2 flake8 whitespace; removed unused import os.path.
John Rouillard <rouilj@ieee.org>
parents: 5418
diff changeset
10
4749
0421390b3094 templating: Move common TAL methods to TALLoaderBase class
anatoly techtonik <techtonik@gmail.com>
parents: 4740
diff changeset
11 class Loader(TALLoaderBase):
7775
b8e63e65d9a8 chore: replace use of dir with template_dir.
John Rouillard <rouilj@ieee.org>
parents: 6063
diff changeset
12 def __init__(self, template_dir):
b8e63e65d9a8 chore: replace use of dir with template_dir.
John Rouillard <rouilj@ieee.org>
parents: 6063
diff changeset
13 self.template_dir = template_dir
7790
ac0802452818 fix: typo in var name inan unused (mostly) chameleon engine.
John Rouillard <rouilj@ieee.org>
parents: 7775
diff changeset
14 self.loader = chameleon.PageTemplateLoader(template_dir)
4587
a2eb4fb3e6d8 New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff changeset
15
4740
fe9568a6cbd6 Untangle template selection logic from template loading functionality.
anatoly techtonik <techtonik@gmail.com>
parents: 4739
diff changeset
16 def load(self, tplname):
7999
310e19beba3e fix: report filename if template file is invalid
John Rouillard <rouilj@ieee.org>
parents: 7790
diff changeset
17 try:
310e19beba3e fix: report filename if template file is invalid
John Rouillard <rouilj@ieee.org>
parents: 7790
diff changeset
18 src, filename = self._find(tplname)
310e19beba3e fix: report filename if template file is invalid
John Rouillard <rouilj@ieee.org>
parents: 7790
diff changeset
19 except TypeError as e:
310e19beba3e fix: report filename if template file is invalid
John Rouillard <rouilj@ieee.org>
parents: 7790
diff changeset
20 raise ValueError("Unable to load template file basename: %s: %s" % (
310e19beba3e fix: report filename if template file is invalid
John Rouillard <rouilj@ieee.org>
parents: 7790
diff changeset
21 tplname, e))
310e19beba3e fix: report filename if template file is invalid
John Rouillard <rouilj@ieee.org>
parents: 7790
diff changeset
22
4587
a2eb4fb3e6d8 New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff changeset
23 return RoundupPageTemplate(self.loader.load(src))
a2eb4fb3e6d8 New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff changeset
24
6063
4d20d8251bf2 flake8 whitespace; removed unused import os.path.
John Rouillard <rouilj@ieee.org>
parents: 5418
diff changeset
25
4635
45ac4cd1a381 Fixes for RoundupPageTemplate in engine_chameleon.py.
Cheer Xiao <xiaqqaix@gmail.com>
parents: 4587
diff changeset
26 class RoundupPageTemplate(object):
4587
a2eb4fb3e6d8 New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff changeset
27 def __init__(self, pt):
a2eb4fb3e6d8 New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff changeset
28 self._pt = pt
a2eb4fb3e6d8 New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff changeset
29
a2eb4fb3e6d8 New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff changeset
30 def render(self, client, classname, request, **options):
a2eb4fb3e6d8 New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff changeset
31 c = context(client, self, classname, request)
a2eb4fb3e6d8 New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff changeset
32 c.update({'options': options})
a2eb4fb3e6d8 New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff changeset
33
a2eb4fb3e6d8 New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff changeset
34 def translate(msgid, domain=None, mapping=None, default=None):
a2eb4fb3e6d8 New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff changeset
35 result = client.translator.translate(domain, msgid,
6063
4d20d8251bf2 flake8 whitespace; removed unused import os.path.
John Rouillard <rouilj@ieee.org>
parents: 5418
diff changeset
36 mapping=mapping,
4d20d8251bf2 flake8 whitespace; removed unused import os.path.
John Rouillard <rouilj@ieee.org>
parents: 5418
diff changeset
37 default=default)
5416
56c9bcdea47f Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents: 4749
diff changeset
38 return s2u(result)
4587
a2eb4fb3e6d8 New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff changeset
39
4635
45ac4cd1a381 Fixes for RoundupPageTemplate in engine_chameleon.py.
Cheer Xiao <xiaqqaix@gmail.com>
parents: 4587
diff changeset
40 output = self._pt.render(None, translate, **c)
4587
a2eb4fb3e6d8 New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff changeset
41 return output.encode(client.charset)
a2eb4fb3e6d8 New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff changeset
42
a2eb4fb3e6d8 New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff changeset
43 def __getitem__(self, name):
a2eb4fb3e6d8 New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff changeset
44 return self._pt[name]
a2eb4fb3e6d8 New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff changeset
45
a2eb4fb3e6d8 New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff changeset
46 def __getattr__(self, name):
a2eb4fb3e6d8 New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff changeset
47 return getattr(self._pt, name)

Roundup Issue Tracker: http://roundup-tracker.org/