/loggerhead/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/loggerhead/trunk

« back to all changes in this revision

Viewing changes to loggerhead/zptsupport.py

  • Committer: Ubuntu One Auto Copilot
  • Author(s): Jelmer Vernooij
  • Date: 2022-08-30 10:28:23 UTC
  • mfrom: (533.1.1 trunk)
  • Revision ID: otto-copilot@canonical.com-20220830102823-u3w6efosxw5s086s
Cope with moved errors NoSuchFile and FileExists in newer versions of Breezy.

Merged from https://code.launchpad.net/~jelmer/loggerhead/moved-errors/+merge/429073

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
# along with this program; if not, write to the Free Software
14
14
# Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335  USA
15
15
#
16
 
"""Support for Zope Page Templates using the simpletal library."""
 
16
"""Support for Zope Page Templates using the Chameleon library."""
17
17
 
18
 
import logging
19
18
import os
20
19
import pkg_resources
21
20
import re
22
 
import StringIO
23
21
 
24
 
from simpletal import simpleTAL, simpleTALES
 
22
from chameleon import PageTemplate
25
23
 
26
24
_zpt_cache = {}
27
25
 
30
28
    tinstance = _zpt_cache.get(tfile)
31
29
    stat = os.stat(tfile)
32
30
    if tinstance is None or tinstance.stat != stat:
33
 
        text = open(tfile).read()
 
31
        with open(tfile) as tf:
 
32
            text = tf.read()
34
33
        text = re.sub(r'\s*\n\s*', '\n', text)
35
34
        text = re.sub(r'[ \t]+', ' ', text)
36
35
        tinstance = _zpt_cache[tfile] = TemplateWrapper(
37
 
            simpleTAL.compileXMLTemplate(text), tfile, stat)
 
36
            PageTemplate(text), tfile, stat)
38
37
    return tinstance
39
38
 
40
39
 
46
45
        self.stat = stat
47
46
 
48
47
    def expand(self, **info):
49
 
        context = simpleTALES.Context(allowPythonPath=1)
50
 
        for k, v in info.iteritems():
51
 
            context.addGlobal(k, v)
52
 
        s = StringIO.StringIO()
53
 
        self.template.expandInline(context, s)
54
 
        return s.getvalue()
 
48
        return self.template(**info)
55
49
 
56
50
    def expand_into(self, f, **info):
57
 
        context = simpleTALES.Context(allowPythonPath=1)
58
 
        for k, v in info.iteritems():
59
 
            context.addGlobal(k, v)
60
 
        self.template.expand(context, f, 'utf-8')
 
51
        f.write(self.template(**info).encode('UTF-8'))
61
52
 
62
53
    @property
63
54
    def macros(self):