/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:
11
11
#
12
12
# You should have received a copy of the GNU General Public License
13
13
# along with this program; if not, write to the Free Software
14
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
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
 
import StringIO
22
 
 
23
 
from simpletal import simpleTAL, simpleTALES
24
 
 
25
 
logging.getLogger("simpleTAL").setLevel(logging.INFO)
26
 
logging.getLogger("simpleTALES").setLevel(logging.INFO)
27
 
 
 
20
import re
 
21
 
 
22
from chameleon import PageTemplate
28
23
 
29
24
_zpt_cache = {}
 
25
 
 
26
 
30
27
def zpt(tfile):
31
28
    tinstance = _zpt_cache.get(tfile)
32
29
    stat = os.stat(tfile)
33
30
    if tinstance is None or tinstance.stat != stat:
 
31
        with open(tfile) as tf:
 
32
            text = tf.read()
 
33
        text = re.sub(r'\s*\n\s*', '\n', text)
 
34
        text = re.sub(r'[ \t]+', ' ', text)
34
35
        tinstance = _zpt_cache[tfile] = TemplateWrapper(
35
 
            simpleTAL.compileXMLTemplate(open(tfile)), tfile, stat)
 
36
            PageTemplate(text), tfile, stat)
36
37
    return tinstance
37
38
 
38
39
 
44
45
        self.stat = stat
45
46
 
46
47
    def expand(self, **info):
47
 
        context = simpleTALES.Context(allowPythonPath=1)
48
 
        for k, v in info.iteritems():
49
 
            context.addGlobal(k, v)
50
 
        s = StringIO.StringIO()
51
 
        self.template.expandInline(context, s)
52
 
        return s.getvalue()
 
48
        return self.template(**info)
53
49
 
54
50
    def expand_into(self, f, **info):
55
 
        context = simpleTALES.Context(allowPythonPath=1)
56
 
        for k, v in info.iteritems():
57
 
            context.addGlobal(k, v)
58
 
        self.template.expand(context, f, 'utf-8')
 
51
        f.write(self.template(**info).encode('UTF-8'))
59
52
 
60
53
    @property
61
54
    def macros(self):
74
67
        package = classname[0:divider]
75
68
        basename = classname[divider+1:]
76
69
    else:
77
 
        raise ValueError, "All templates must be in a package"
 
70
        raise ValueError("All templates must be in a package")
78
71
 
79
72
    tfile = pkg_resources.resource_filename(
80
73
        package, "%s.%s" % (basename, "pt"))