ophelia / ophelia /

# Copyright (c) 2007-2008 Thomas Lotze
# See also LICENSE.txt

import zope.pagetemplate.pagetemplate

class PageTemplateTracebackSupplement(object):

    def __init__(self, template):
        self.template = template
        self.source_url = template.file_path
        self.warnings = template._v_errors

    def getInfo(self):
        if len(self.template._v_errors) != 2:
        fragments = self.template._v_errors[1].rsplit(',')[-2:]
        if len(fragments) != 2:
            line = int(fragments[0].split()[-1])
        except ValueError:
            column = int(fragments[1].split()[-1])
        except ValueError:
        if line >= 5:
            lower = line - 5
            err_line = 5
            lower = 0
            err_line = line

        line_offset, row_offset = self.template.offset

        text_lines = self.template._text.rstrip().splitlines()[lower:line + 5]
        if lower == 0:
            text_lines[0] = '.' * row_offset + text_lines[0]
            if line == 1:
                column += row_offset

        text_lines = ["%4d: %s" % (lower + i + 1 + line_offset, line)
                      for i, line in enumerate(text_lines)]
        text_lines.insert(err_line, "   -> %s^" % (' ' * column))
        return '\n'.join(text_lines)

class PageTemplate(zope.pagetemplate.pagetemplate.PageTemplate):
    """Page templates with supplemented tracebacks and source tracking.

    Call parameters: the namespace of file context variables

    file_path = None

    def __init__(self, text, file_path=None, offset=(0, 0)):
        super(PageTemplate, self).__init__()
        self.file_path = file_path
        self.offset = offset

    def _cook(self):
        __traceback_supplement__ = (PageTemplateTracebackSupplement, self)
        super(PageTemplate, self)._cook()
        if self._v_errors:
            raise ValueError("There were errors in the page template text.")

    def pt_getContext(self, namespaces, names):
        context = {"None": None,
        for namespace in namespaces:
        return context

    def pt_source_file(self):
        return self.file_path
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.