コード例 #1
0
ファイル: __init__.py プロジェクト: Ismatov/gsl-en
    def generate(self, encoding=None,
            fragment=False, output=None, format=None):
        """
        Execute template and generate serialized output incrementally.

        This method returns an iterator that yields an encoded string
        for each iteration. The iteration ends when the template is done
        executing.

        encoding
          The output encoding. Default: utf-8.
        fragment
          Controls whether prologue information (such as <?xml?>
          declaration and DOCTYPE should be written). Set to True
          when generating fragments meant to be inserted into
          existing XML documents.
        output
          A string specifying an output method ('xml', 'html',
          'xhtml') or a Serializer object.
        """
        serializer = self._get_serializer(output)
        try:
            return serializer.generate(self, encoding, fragment, format)
        except Exception:
            raise_template_error(module=self.__module__)
コード例 #2
0
def _create_module(code, name, filename, store=True, ns={}, exec_module=None):
    for recompiled in range(2):
        name = get_template_name(name, filename)
        mod = new.module(name)
        mod.__file__ = filename
        mod.__ctime__ = time.time()
        mod.__dict__.update(ns)
        try:
            if exec_module:
                exec_module(mod, code)
            else:
                exec code in mod.__dict__
        except Exception:
            if store:
                sys.modules[name] = mod
            raise_template_error(module=name, filename=filename)
        if getattr(mod, 'kid_version', None) == __version__:
            break
        # the module has been compiled against an old Kid version,
        # recompile to ensure compatibility and best performance
        if recompiled:  # already tried recompiling, to no avail
            raise TemplateImportError('Cannot recompile template file'
                                      ' %r for Kid version %s' %
                                      (filename, __version__))
        template = KidFile(filename)
        template.stale = True
        template._python = template._code = None
        code = template.compile(dump_source=environ.get('KID_OUTPUT_PY'))
    if store:
        sys.modules[name] = mod
    return mod
    def generate(self,
                 encoding=None,
                 fragment=False,
                 output=None,
                 format=None):
        """
        Execute template and generate serialized output incrementally.

        This method returns an iterator that yields an encoded string
        for each iteration. The iteration ends when the template is done
        executing.

        encoding
          The output encoding. Default: utf-8.
        fragment
          Controls whether prologue information (such as <?xml?>
          declaration and DOCTYPE should be written). Set to True
          when generating fragments meant to be inserted into
          existing XML documents.
        output
          A string specifying an output method ('xml', 'html',
          'xhtml') or a Serializer object.
        """
        serializer = self._get_serializer(output)
        try:
            return serializer.generate(self, encoding, fragment, format)
        except Exception:
            raise_template_error(module=self.__module__)
コード例 #4
0
ファイル: __init__.py プロジェクト: Ismatov/gsl-en
    def serialize(self, encoding=None,
            fragment=False, output=None, format=None):
        """
        Execute a template and return a single string.

        encoding
          The output encoding. Default: utf-8.
        fragment
          Controls whether prologue information (such as <?xml?>
          declaration and DOCTYPE should be written). Set to True
          when generating fragments meant to be inserted into
          existing XML documents.
        output
          A string specifying an output method ('xml', 'html',
          'xhtml') or a Serializer object.

        This is a convienence method, roughly equivalent to::

          ''.join([x for x in obj.generate(encoding, fragment, output)]

        """
        serializer = self._get_serializer(output)
        try:
            return serializer.serialize(self, encoding, fragment, format)
        except Exception:
            raise_template_error(module=self.__module__)
    def serialize(self,
                  encoding=None,
                  fragment=False,
                  output=None,
                  format=None):
        """
        Execute a template and return a single string.

        encoding
          The output encoding. Default: utf-8.
        fragment
          Controls whether prologue information (such as <?xml?>
          declaration and DOCTYPE should be written). Set to True
          when generating fragments meant to be inserted into
          existing XML documents.
        output
          A string specifying an output method ('xml', 'html',
          'xhtml') or a Serializer object.

        This is a convienence method, roughly equivalent to::

          ''.join([x for x in obj.generate(encoding, fragment, output)]

        """
        serializer = self._get_serializer(output)
        try:
            return serializer.serialize(self, encoding, fragment, format)
        except Exception:
            raise_template_error(module=self.__module__)
    def write(self,
              file,
              encoding=None,
              fragment=False,
              output=None,
              format=None):
        """
        Execute template and write output to file.

        file:file
          A filename or a file like object (must support write()).
        encoding:string
          The output encoding. Default: utf-8.
        fragment:bool
          Controls whether prologue information (such as <?xml?>
          declaration and DOCTYPE should be written). Set to True
          when generating fragments meant to be inserted into
          existing XML documents.
        output:string,`Serializer`
          A string specifying an output method ('xml', 'html',
          'xhtml') or a Serializer object.
        """
        serializer = self._get_serializer(output)
        try:
            return serializer.write(self, file, encoding, fragment, format)
        except Exception:
            raise_template_error(module=self.__module__)
コード例 #7
0
ファイル: importer.py プロジェクト: eginhard/gsl-en
def _create_module(code, name, filename, store=True, ns={}, exec_module=None):
    for recompiled in range(2):
        name = get_template_name(name, filename)
        mod = new.module(name)
        mod.__file__ = filename
        mod.__ctime__ = time.time()
        mod.__dict__.update(ns)
        try:
            if exec_module:
                exec_module(mod, code)
            else:
                exec code in mod.__dict__
        except Exception:
            if store:
                sys.modules[name] = mod
            raise_template_error(module=name, filename=filename)
        if getattr(mod, "kid_version", None) == __version__:
            break
        # the module has been compiled against an old Kid version,
        # recompile to ensure compatibility and best performance
        if recompiled:  # already tried recompiling, to no avail
            raise TemplateImportError(
                "Cannot recompile template file" " %r for Kid version %s" % (filename, __version__)
            )
        template = KidFile(filename)
        template.stale = True
        template._python = template._code = None
        code = template.compile(dump_source=environ.get("KID_OUTPUT_PY"))
    if store:
        sys.modules[name] = mod
    return mod
コード例 #8
0
 def code(self):
     """Get the compiled Python code for the template."""
     if self._code is None:
         if self.stale:
             pyfile = self.py_file
             if self.strip_dest_dir and \
                self.py_file.startswith(self.strip_dest_dir):
                 pyfile = os.path.normpath(
                     self.py_file[len(self.strip_dest_dir):])
             try:
                 self._code = py_compile(self.python, pyfile)
             except Exception:
                 raise_template_error(filename=self.kid_file,
                                      encoding=self.encoding)
         else:
             self._code = marshal.load(self._pyc_fp)
     return self._code
コード例 #9
0
ファイル: compiler.py プロジェクト: thraxil/gtreed
 def code(self):
     """Get the compiled Python code for the template."""
     if self._code is None:
         if self.stale:
             pyfile = self.py_file
             if self.strip_dest_dir and \
                self.py_file.startswith(self.strip_dest_dir):
                 pyfile = os.path.normpath(
                     self.py_file[len(self.strip_dest_dir):])
             try:
                 self._code = py_compile(self.python, pyfile)
             except Exception:
                 raise_template_error(filename=self.kid_file,
                     encoding=self.encoding)
         else:
             self._code = marshal.load(self._pyc_fp)
     return self._code
コード例 #10
0
ファイル: __init__.py プロジェクト: Ismatov/gsl-en
    def write(self, file, encoding=None,
            fragment=False, output=None, format=None):
        """
        Execute template and write output to file.

        file:file
          A filename or a file like object (must support write()).
        encoding:string
          The output encoding. Default: utf-8.
        fragment:bool
          Controls whether prologue information (such as <?xml?>
          declaration and DOCTYPE should be written). Set to True
          when generating fragments meant to be inserted into
          existing XML documents.
        output:string,`Serializer`
          A string specifying an output method ('xml', 'html',
          'xhtml') or a Serializer object.
        """
        serializer = self._get_serializer(output)
        try:
            return serializer.write(self, file, encoding, fragment, format)
        except Exception:
            raise_template_error(module=self.__module__)