annotate roundup/cgi/engine_chameleon.py @ 5418:55f09ca366c4

Python 3 preparation: StringIO. This generally arranges for StringIO and cStringIO references to use io.StringIO for Python 3 but io.BytesIO for Python 2, consistent with the string representations generally used in Roundup. A special FasterStringIO in the TAL code, which referenced internals of the old Python 2 StringIO module, is cut down so it doesn't actually do anything beyond the StringIO class it inherits from (it would also be reasonable to remove FasterStringIO completely). One place in roundup_server.py clearly needing binary I/O is made to use io.BytesIO unconditionally.
author Joseph Myers <jsm@polyomino.org.uk>
date Wed, 25 Jul 2018 09:08:29 +0000
parents 56c9bcdea47f
children 4d20d8251bf2
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
a2eb4fb3e6d8 New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff changeset
5 import os.path
4720
fd72576e07ed API break: rename Templates to Loader for zopetal and chameleon
anatoly techtonik <techtonik@gmail.com>
parents: 4719
diff changeset
6 import chameleon
4587
a2eb4fb3e6d8 New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff changeset
7
5418
55f09ca366c4 Python 3 preparation: StringIO.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5416
diff changeset
8 from roundup.cgi.templating import context, TALLoaderBase
5416
56c9bcdea47f Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents: 4749
diff changeset
9 from roundup.anypy.strings import s2u
4587
a2eb4fb3e6d8 New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
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):
4587
a2eb4fb3e6d8 New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff changeset
12 def __init__(self, dir):
a2eb4fb3e6d8 New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff changeset
13 self.dir = dir
4720
fd72576e07ed API break: rename Templates to Loader for zopetal and chameleon
anatoly techtonik <techtonik@gmail.com>
parents: 4719
diff changeset
14 self.loader = chameleon.PageTemplateLoader(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):
4749
0421390b3094 templating: Move common TAL methods to TALLoaderBase class
anatoly techtonik <techtonik@gmail.com>
parents: 4740
diff changeset
17 src, filename = self._find(tplname)
4587
a2eb4fb3e6d8 New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff changeset
18 return RoundupPageTemplate(self.loader.load(src))
a2eb4fb3e6d8 New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff changeset
19
4635
45ac4cd1a381 Fixes for RoundupPageTemplate in engine_chameleon.py.
Cheer Xiao <xiaqqaix@gmail.com>
parents: 4587
diff changeset
20 class RoundupPageTemplate(object):
4587
a2eb4fb3e6d8 New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff changeset
21 def __init__(self, pt):
a2eb4fb3e6d8 New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff changeset
22 self._pt = pt
a2eb4fb3e6d8 New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff changeset
23
a2eb4fb3e6d8 New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff changeset
24 def render(self, client, classname, request, **options):
a2eb4fb3e6d8 New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff changeset
25 c = context(client, self, classname, request)
a2eb4fb3e6d8 New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff changeset
26 c.update({'options': options})
a2eb4fb3e6d8 New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff changeset
27
a2eb4fb3e6d8 New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff changeset
28 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
29 result = client.translator.translate(domain, msgid,
a2eb4fb3e6d8 New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff changeset
30 mapping=mapping, default=default)
5416
56c9bcdea47f Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents: 4749
diff changeset
31 return s2u(result)
4587
a2eb4fb3e6d8 New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff changeset
32
4635
45ac4cd1a381 Fixes for RoundupPageTemplate in engine_chameleon.py.
Cheer Xiao <xiaqqaix@gmail.com>
parents: 4587
diff changeset
33 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
34 return output.encode(client.charset)
a2eb4fb3e6d8 New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff changeset
35
a2eb4fb3e6d8 New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff changeset
36 def __getitem__(self, name):
a2eb4fb3e6d8 New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff changeset
37 return self._pt[name]
a2eb4fb3e6d8 New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff changeset
38
a2eb4fb3e6d8 New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff changeset
39 def __getattr__(self, name):
a2eb4fb3e6d8 New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff changeset
40 return getattr(self._pt, name)
a2eb4fb3e6d8 New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff changeset
41

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