Export data

This Python module supports multiple export formats. However, once exported, data cannot be loaded with colander_data_converter.

Mermaid

The Mermaid exporter generates text files that can be used to create visual representations of entity relationships and hierarchies.

For more details, check the documentation of MermaidExporter.

_images/mermaid.png
import json

from colander_data_converter.base.models import ColanderFeed
from colander_data_converter.exporters.mermaid import MermaidExporter

# Load the feed
with open("path/to/colander_feed.json", "r") as f:
    raw = json.load(f)
colander_feed = ColanderFeed.load(raw)

# Export the feed as a graph
exporter = MermaidExporter(colander_feed)
with open("path/to/colander_feed.txt", "w") as f:
    exporter.export(f)

Graphviz

The Graphviz exporter generates DOT format files that can be used to create visual representations of entity relationships and hierarchies. It uses customizable themes to control the visual styling of the generated graphs.

For more details, check the documentation of GraphvizExporter.

_images/graphviz.png
import json

from colander_data_converter.base.models import ColanderFeed
from colander_data_converter.exporters.graphviz import GraphvizExporter

# Load the feed
with open("path/to/colander_feed.json", "r") as f:
    raw = json.load(f)
colander_feed = ColanderFeed.load(raw)

# Export the feed as a graph
exporter = GraphvizExporter(colander_feed)
with open("path/to/colander_feed.dot", "w") as f:
    exporter.export(f)

Template

The template exporter uses Jinja2 templates to generate custom output formats. It supports both file-based templates and pre-compiled Template objects, with automatic sandboxing for security when using file-based templates.

For more details, check the documentation of TemplateExporter.

_images/template.png
import json

from colander_data_converter.base.models import ColanderFeed
from colander_data_converter.exporters.template import TemplateExporter

templates_folder = "path/to/templates"
template_name = "example.jinja2"

# Load the feed
with open("path/to/colander_feed.json", "r") as f:
    raw = json.load(f)
colander_feed = ColanderFeed.load(raw)

# Render the template
exporter = TemplateExporter(colander_feed, templates_folder, template_name)
with open("path/to/colander_feed.txt", "w") as f:
    exporter.export(f, title="Example")

# Using pre-compiled template (WARNING: No sandbox environment)
from jinja2 import Template

template = Template("Report: {{ title }}\nEntities: {{ feed.entities | length }}")
exporter = TemplateExporter(
    colander_feed,
    "",
    "",
    template=template
)
with open("path/to/report.txt", "w") as f:
    exporter.export(f, title="Security Report")

Danger

When using the template exporter, be aware of the following security considerations:

  • File-based templates: templates loaded from files are executed in a sandboxed Jinja2 environment that restricts access to potentially dangerous operations.

  • Pre-compiled templates: When providing a pre-compiled template object, it will NOT be executed in a sandboxed environment and may have access to all Python built-ins. Only use trusted templates in this case.

For production environments, always prefer file-based templates over pre-compiled template objects unless you have full control over the template source.

CSV

The CSV exporter generates comma-separated values files containing entity data. It supports exporting specific entity types and allows customization of CSV formatting options.

For more details, check the documentation of CsvExporter.

_images/csv.png
import json

from colander_data_converter.base.models import ColanderFeed, Observable
from colander_data_converter.exporters.csv import CsvExporter

# Load the feed
with open("path/to/colander_feed.json", "r") as f:
    raw = json.load(f)
colander_feed = ColanderFeed.load(raw)

# Export all observables in CSV
exporter = CsvExporter(colander_feed, Observable)
with open("path/to/colander_feed.csv", "w") as f:
    exporter.export(f)

# Custom CSV formatting
exporter = CsvExporter(colander_feed, Observable)
with open("path/to/custom_format.csv", "w") as f:
    exporter.export(f, delimiter=";", quoting=csv.QUOTE_ALL)