Esempio n. 1
0
    def render(self, **kwargs):
        """Doc..."""

        # ADD KWARGS TO TEMPLATE RENDER PROPERTIES
        if kwargs:
            data = DictUtils.merge(self._data, kwargs)
        else:
            data = self._data

        td = [self._rootDir] if StringUtils.isStringType(self._rootDir) else self._rootDir

        lookup = TemplateLookup(
            directories=td, input_encoding="utf-8", output_encoding="utf-8", encoding_errors="replace"
        )

        template = self._template
        if template:
            if not template.startswith("/"):
                template = "/" + template

            try:
                target = lookup.get_template(template)
            except Exception as err:
                self._result = None
                self._error = err
                self._errorMsg = "Failed to get template (%s):\n%s" % (
                    template,
                    exceptions.text_error_template().render().replace("%", "%%"),
                )
                self._log.writeError(self._errorMsg, self._error)
                return self.dom
        else:
            target = Template(self._source if self._source else "", lookup=lookup)

        mr = MakoDataTransporter(data=data, logger=self._log)
        try:
            self._result = target.render_unicode(mr=mr).replace("\r", "")
        except Exception:
            d = []
            if data:
                for n, v in data.items():
                    d.append(StringUtils.toUnicode(n) + ": " + StringUtils.toUnicode(v))

            try:
                stack = exceptions.text_error_template().render().replace("%", "%%")
            except Exception as err2:
                stack = ""
                self._log.writeError("Unable to build mako exception stack", err2)

            traces = mr.getTraces()
            self._errorMsg = "Failed to render (%s):\n%s\n%sDATA:\n\t%s" % (
                str(template),
                str(stack),
                ("TRACES:\n\t" + "\n\t".join(traces) if traces else ""),
                "\n\t".join(d) if d else "",
            )

            self._log.write(self._errorMsg)

        if self._minify:
            return self.minifyResult()

        return self.dom
Esempio n. 2
0
    def render(self, **kwargs):
        """Doc..."""

        # ADD KWARGS TO TEMPLATE RENDER PROPERTIES
        if kwargs:
            data = DictUtils.merge(self._data, kwargs)
        else:
            data = self._data

        td = [self._rootDir] if StringUtils.isStringType(self._rootDir) else self._rootDir

        lookup = TemplateLookup(
            directories=td,
            input_encoding='utf-8',
            output_encoding='utf-8',
            encoding_errors='replace')

        template = self._template
        if template:
            if not template.startswith('/'):
                template = '/' + template

            try:
                target = lookup.get_template(template)
            except Exception as err:
                self._result   = None
                self._error    = err
                self._errorMsg = 'Failed to get template (%s):\n%s' % (
                    template,
                    exceptions.text_error_template().render().replace('%','%%') )
                self._log.writeError(self._errorMsg, self._error)
                return self.dom
        else:
            target = Template(self._source if self._source else '', lookup=lookup)

        mr = MakoDataTransporter(data=data, logger=self._log)
        try:
            self._result = target.render_unicode(mr=mr).replace('\r', '')
        except Exception:
            d = []
            if data:
                for n,v in data.items():
                    d.append(StringUtils.toUnicode(n) + ': ' + StringUtils.toUnicode(v))

            try:
                stack = exceptions.text_error_template().render().replace('%','%%')
            except Exception as err2:
                stack = ''
                self._log.writeError('Unable to build mako exception stack', err2)

            traces = mr.getTraces()
            self._errorMsg = 'Failed to render (%s):\n%s\n%sDATA:\n\t%s' % (
                str(template),
                str(stack),
                ('TRACES:\n\t' + '\n\t'.join(traces) if traces else ''),
                '\n\t'.join(d) if d else '')

            self._log.write(self._errorMsg)

        if self._minify:
            return self.minifyResult()

        return self.dom
Esempio n. 3
0
                template = '/' + template

            try:
                target = lookup.get_template(template)
            except Exception, err:
                self._result   = None
                self._error    = err
                self._errorMsg = 'Failed to get template (%s):\n%s' % (
                    template,
                    exceptions.text_error_template().render().replace('%','%%') )
                self._log.writeError(self._errorMsg, self._error)
                return self.dom
        else:
            target = Template(self._source if self._source else u'', lookup=lookup)

        mr = MakoDataTransporter(data=data, logger=self._log)
        try:
            self._result = target.render_unicode(mr=mr).replace('\r', '')
        except Exception, err:
            d = []
            if data:
                for n,v in data.iteritems():
                    d.append(unicode(n) + u': ' + unicode(v))

            try:
                stack = exceptions.text_error_template().render().replace('%','%%')
            except Exception, err2:
                stack = ''
                self._log.writeError('Unable to build mako exception stack', err2)

            traces = mr.getTraces()