colander_data_converter.exporters.template

class colander_data_converter.exporters.template.TemplateExporter(feed, template_search_path, template_name, template_source=None, **loader_options)[source]

Bases: BaseExporter

Template-based exporter using Jinja2 templating engine.

This exporter allows for flexible data export by using Jinja2 templates to format the output. It supports both file-based templates loaded from the filesystem and pre-compiled Template object. The implementation uses a sandboxed environment for security when processing templates.

The exporter streams the template output, making it memory-efficient for large datasets by processing data in chunks rather than loading everything into memory.

__init__(feed, template_search_path, template_name, template_source=None, **loader_options)[source]

Initialize the TemplateExporter with feed data and template configuration.

This constructor sets up the Jinja2 templating environment and loads the specified template. If a pre-compiled Template object is provided, it will be used directly. Otherwise, the template will be loaded from the filesystem using the provided search path and template name.

Parameters:
  • feed (ColanderFeed) – The data feed containing entities to be exported. This feed will be passed to the template as the feed variable.

  • template_search_path (str | PathLike[str] | Sequence[str | PathLike[str]]) – Path or sequence of paths where template files are located. Can be a single path string, PathLike object, or sequence of paths for multiple search locations.

  • template_name (str) – The name of the template file to load from the search path. Should include the file extension (e.g., “template.j2”, “export.html”).

  • template_source (str) – The source code of the Jinja2 template. If provided, template_search_path and template_name are ignored. Defaults to None.

  • **loader_options – Additional keyword arguments passed to the FileSystemLoader.

Note

The exporter uses a SandboxedEnvironment for security, which restricts access to potentially dangerous operations in templates. Auto-reload is enabled by default for development convenience.

export(output, **kwargs)[source]

Export data by rendering the template and writing output to the provided stream.

This method uses Jinja2’s streaming to render the template in chunks, making it memory-efficient for large datasets. The feed data is passed to the template as the ‘feed’ variable, and any additional keyword arguments are also made available as template variables.

Parameters:
  • output (TextIO) – A text-based output stream where the rendered template will be written. This can be a file object, StringIO, or any object implementing the TextIO interface.

  • **kwargs – Additional keyword arguments that will be passed as variables to the template context. These can be used within the template to customize the output or provide additional data.

Raises:
  • TemplateError – If there are errors in template syntax or rendering

  • TemplateNotFound – If the specified template file cannot be found

  • IOError – If there are issues writing to the output stream