コード例 #1
0
ファイル: exporter.py プロジェクト: chrstn-hntschl/advene
    def export(self, filename=None):
        ctx = self.controller.build_context(here=self.source)
        if filename is None:
            # No filename is provided. Return string.
            stream = io.BytesIO()
        elif isinstance(filename, io.TextIOBase):
            # Use an intermediary BytesIO
            stream = io.BytesIO()
        elif isinstance(filename, io.BytesIO):
            stream = filename
        else:
            try:
                stream = open(filename, 'wb')
            except Exception:
                logger.error(_("Cannot export to %(filename)s"), exc_info=True)
                return True

        if self.templateview.content.mimetype is None or self.templateview.content.mimetype.startswith(
                'text/'):
            compiler = simpleTAL.HTMLTemplateCompiler()
            compiler.parseTemplate(self.templateview.content.stream, 'utf-8')
            if self.templateview.content.mimetype == 'text/plain':
                # Convert HTML entities to their values
                output = io.BytesIO()
            else:
                output = stream
            try:
                compiler.getTemplate().expand(context=ctx,
                                              outputFile=output,
                                              outputEncoding='utf-8')
            except simpleTALES.ContextContentException:
                logger.error(_("Error when exporting text template"),
                             exc_info=True)
            if self.templateview.content.mimetype == 'text/plain':
                stream.write(output.getvalue().replace(b'&lt;', b'<').replace(
                    b'&gt;', b'>').replace(b'&amp;', b'&'))
        else:
            compiler = simpleTAL.XMLTemplateCompiler()
            compiler.parseTemplate(self.templateview.content.stream)
            try:
                compiler.getTemplate().expand(context=ctx,
                                              outputFile=stream,
                                              outputEncoding='utf-8',
                                              suppressXMLDeclaration=True)
            except simpleTALES.ContextContentException:
                logger.error(_("Error when exporting XML template"),
                             exc_info=True)
        if filename is None:
            value = stream.getvalue()
            stream.close()
            return value
        elif isinstance(filename, io.TextIOBase):
            # Enforce UTF-8
            filename.write(stream.getvalue().encode('utf-8'))
        elif isinstance(filename, io.BytesIO):
            # Nothing to do: it is the responsibility of the caller to close the stream
            pass
        else:
            stream.close()
            return _("Data exported to %s") % filename
コード例 #2
0
    def interpret(self, view_source, mimetype, stream=None):
        """
        Interpret the TAL template available through the stream view_source,
        with the mime-type mimetype, and print the result to the stream
        "stream". The stream is returned. If stream is not given or None, a
        StringIO will be created and returned.
        """
        if stream is None:
            stream = StringIO()

        if isinstance(view_source, str) or isinstance(view_source, unicode):
            view_source = StringIO(unicode(view_source))

        kw = {}
        if mimetype is None or mimetype.startswith('text/'):
            compiler = simpleTAL.HTMLTemplateCompiler()
            compiler.log = self.log
            compiler.parseTemplate(view_source, 'utf-8')
        else:
            compiler = simpleTAL.XMLTemplateCompiler()
            compiler.log = self.log
            compiler.parseTemplate(view_source)
            kw["suppressXMLDeclaration"] = 1
        compiler.getTemplate().expand(context=self,
                                      outputFile=stream,
                                      outputEncoding='utf-8',
                                      **kw)

        return stream
コード例 #3
0
ファイル: BasicTemplateTest.py プロジェクト: g2p/SimpleTAL
def NGTemplateOverhead(count):
    file = io.StringIO()
    start = time.clock()
    for attempt in range(count):
        tempFile = io.StringIO(performanceTemplate)
        compiler = simpleTAL.XMLTemplateCompiler()
        compiler.parseTemplate(tempFile)
        template = compiler.getTemplate()
        template.expand(context, file)
    end = time.clock()
    #print "Resuling file: " + file.getvalue()
    return (end - start)
コード例 #4
0
    def export(self, filename):
        ctx = self.controller.build_context(here=self.source)
        try:
            stream = open(filename, 'wb')
        except Exception:
            logger.error(_("Cannot export to %(filename)s"), exc_info=True)
            return True

        if self.templateview.content.mimetype is None or self.templateview.content.mimetype.startswith(
                'text/'):
            compiler = simpleTAL.HTMLTemplateCompiler()
            compiler.parseTemplate(self.templateview.content.stream, 'utf-8')
            if self.templateview.content.mimetype == 'text/plain':
                # Convert HTML entities to their values
                output = io.BytesIO()
            else:
                output = stream
            try:
                compiler.getTemplate().expand(context=ctx,
                                              outputFile=output,
                                              outputEncoding='utf-8')
            except simpleTALES.ContextContentException:
                logger.error(_("Error when exporting text template"),
                             exc_info=True)
            if self.templateview.content.mimetype == 'text/plain':
                stream.write(output.getvalue().replace(b'&lt;', b'<').replace(
                    b'&gt;', b'>').replace(b'&amp;', b'&'))
        else:
            compiler = simpleTAL.XMLTemplateCompiler()
            compiler.parseTemplate(self.templateview.content.stream)
            try:
                compiler.getTemplate().expand(context=ctx,
                                              outputFile=stream,
                                              outputEncoding='utf-8',
                                              suppressXMLDeclaration=True)
            except simpleTALES.ContextContentException:
                logger.error(_("Error when exporting XML template"),
                             exc_info=True)
        stream.close()
        return _("Data exported to %s") % filename