Пример #1
0
    def transform(self, stream=None, filters=[]):
        """
        Execute the template and apply any match transformations.

        If stream is specified, it must be one of the following:

        Element
          A kid.Element.
        ElementStream
          An `pull.ElementStream` instance or other iterator that yields
          stream events.
        string
          A file or URL unless the string starts with
          '<' in which case it is considered an XML document
          and processed as if it had been an Element.

        By default, the `pull` method is called to obtain the stream.
        """
        if stream is None:
            stream = self.pull()
        elif isinstance(stream, basestring):
            if xml_sniff(stream):
                stream = XML(stream, fragment=False)
            else:
                stream = document(stream)
        elif hasattr(stream, 'tag'):
            stream = ElementStream(stream)
        else:
            stream = ElementStream.ensure(stream)
        for f in filters + self._filters:
            stream = f(stream, self)
        return stream
    def transform(self, stream=None, filters=[]):
        """
        Execute the template and apply any match transformations.

        If stream is specified, it must be one of the following:

        Element
          A kid.Element.
        ElementStream
          An `pull.ElementStream` instance or other iterator that yields
          stream events.
        string
          A file or URL unless the string starts with
          '<' in which case it is considered an XML document
          and processed as if it had been an Element.

        By default, the `pull` method is called to obtain the stream.
        """
        if stream is None:
            stream = self.pull()
        elif isinstance(stream, basestring):
            if xml_sniff(stream):
                stream = XML(stream, fragment=False)
            else:
                stream = document(stream)
        elif hasattr(stream, 'tag'):
            stream = ElementStream(stream)
        else:
            stream = ElementStream.ensure(stream)
        for f in filters + self._filters:
            stream = f(stream, self)
        return stream
Пример #3
0
def load_template(file, name='', cache=True, encoding=None, ns={},
        entity_map=None, exec_module=None):
    """Bypass import machinery and load a template module directly.

    This can be used as an alternative to accessing templates using the
    native python import mechanisms.

    file
      Can be a filename, a kid template string, or an open file object.
    name
      Optionally specifies the module name to use for this template. This
      is a hack to enable relative imports in templates.
    cache
      Whether to look for a byte-compiled version of the template. If
      no byte-compiled version is found, an attempt is made to dump a
      byte-compiled version after compiling. This argument is ignored if
      file is not a filename.
    entity_map
      Entity map to be used when parsing the template.
    exec_module
      If you want full control over how the template module is executed,
      you can provide this callable that will be called with the template
      module and the code to be executed as parameters, after the code has
      been compiled and the module has been created.

    """
    if isinstance(file, basestring):
        if xml_sniff(file):
            fo = QuickTextReader(file)
            filename = '<string>'
        else:
            fo = None
            filename = file
    else:
        fo = file
        filename = '<string>'
    import kid.importer as importer
    if filename != '<string>':
        abs_filename = path.find(filename)
        if not abs_filename:
            raise template_util.TemplateNotFound(
                "%s (in %s)" % (filename, ', '.join(path.paths)))
        filename = abs_filename
        name = importer.get_template_name(name, filename)
        if sys.modules.has_key(name):
            return sys.modules.get(name)
    import kid.compiler as compiler
    if filename == '<string>':
        code = compiler.compile(fo, filename, encoding, entity_map)
    else:
        template = compiler.KidFile(filename, force=False,
            encoding=encoding, entity_map=entity_map)
        code = template.compile(dump_code=cache,
            dump_source=os.environ.get('KID_OUTPUT_PY'))
    mod = importer._create_module(code, name, filename,
        store=cache, ns=ns, exec_module=exec_module)
    return mod
def load_template(file,
                  name='',
                  cache=True,
                  encoding=None,
                  ns={},
                  entity_map=None,
                  exec_module=None):
    """Bypass import machinery and load a template module directly.

    This can be used as an alternative to accessing templates using the
    native python import mechanisms.

    file
      Can be a filename, a kid template string, or an open file object.
    name
      Optionally specifies the module name to use for this template. This
      is a hack to enable relative imports in templates.
    cache
      Whether to look for a byte-compiled version of the template. If
      no byte-compiled version is found, an attempt is made to dump a
      byte-compiled version after compiling. This argument is ignored if
      file is not a filename.
    entity_map
      Entity map to be used when parsing the template.
    exec_module
      If you want full control over how the template module is executed,
      you can provide this callable that will be called with the template
      module and the code to be executed as parameters, after the code has
      been compiled and the module has been created.

    """
    if isinstance(file, basestring):
        if xml_sniff(file):
            fo = QuickTextReader(file)
            filename = '<string>'
        else:
            fo = None
            filename = file
    else:
        fo = file
        filename = '<string>'
    import kid.importer as importer
    if filename != '<string>':
        abs_filename = path.find(filename)
        if not abs_filename:
            raise template_util.TemplateNotFound(
                "%s (in %s)" % (filename, ', '.join(path.paths)))
        filename = abs_filename
        name = importer.get_template_name(name, filename)
        if sys.modules.has_key(name):
            return sys.modules.get(name)
    import kid.compiler as compiler
    if filename == '<string>':
        code = compiler.compile(fo, filename, encoding, entity_map)
    else:
        template = compiler.KidFile(filename,
                                    force=False,
                                    encoding=encoding,
                                    entity_map=entity_map)
        code = template.compile(dump_code=cache,
                                dump_source=os.environ.get('KID_OUTPUT_PY'))
    mod = importer._create_module(code,
                                  name,
                                  filename,
                                  store=cache,
                                  ns=ns,
                                  exec_module=exec_module)
    return mod