Template Registry

A simple, thread-safe place to discover and fetch dataset templates for MDIO.

Why use it

  • One place to find all available templates

  • Safe to use across threads and the whole app (singleton)

  • Every fetch gives you your own editable copy (no side effects)

  • Comes preloaded with common seismic templates

Note

Fetching a template with get_template() returns a deep copy. Editing it will not change the registry or anyone else’s copy.

Quick start

 1from mdio.builder.template_registry import get_template, list_templates
 2
 3# See what's available
 4print(list_templates())
 5# e.g. ["Seismic2DPostStackTime", "Seismic3DPostStackDepth", ...]
 6
 7# Grab a template by name
 8template = get_template("Seismic3DPostStackTime")
 9
10# Customize your copy (safe)
11template.add_units({"amplitude": "unitless"})

Common tasks

Fetch a template you can edit

1from mdio.builder.template_registry import get_template
2
3template = get_template("Seismic2DPostStackDepth")
4# Use/modify template freely — it’s your copy

List available templates

1from mdio.builder.template_registry import list_templates
2
3names = list_templates()
4for name in names:
5    print(name)

Check if a template exists

1from mdio.builder.template_registry import is_template_registered
2
3if is_template_registered("Seismic3DPostStackTime"):
4    ...  # safe to fetch

Register your own template (optional)

If you have a custom template class, register an instance so others can fetch it by name:

 1from typing import Any
 2from mdio.builder.template_registry import register_template
 3from mdio.builder.templates.base import AbstractDatasetTemplate
 4from mdio.builder.templates.types import SeismicDataDomain
 5
 6
 7class MyTemplate(AbstractDatasetTemplate):
 8    def __init__(self, domain: SeismicDataDomain = "time"):
 9        super().__init__(domain)
10
11    @property
12    def _name(self) -> str:
13        # The public name becomes something like "MyTemplateTime"
14        return f"MyTemplate{self._data_domain.capitalize()}"
15
16    def _load_dataset_attributes(self) -> dict[str, Any]:
17        return {"surveyType": "2D", "gatherType": "custom"}
18
19
20# Make it available globally
21registered_name = register_template(MyTemplate("time"))
22print(registered_name)  # "MyTemplateTime"

Tip

Use list_templates() to discover the exact names to pass to get_template().

Troubleshooting

  • KeyError: “Template ‘XYZ’ is not registered.”

    • The name is wrong or not registered yet.

    • Call list_templates() to see valid names, or is_template_registered(name) to check first.

FAQ

  • Do I need to create a TemplateRegistry instance? No. Use the global helpers: get_template, list_templates, register_template, and is_template_registered.

  • Are templates shared between callers or threads? No. Each get_template() call returns a deep-copied instance that is safe to modify independently.

API reference

Template registry for MDIO dataset templates.

This module provides a tiny, thread-safe singleton registry to discover and fetch predefined dataset templates used by the MDIO builder.

Key points

  • Global, thread-safe singleton (safe to use across threads)

  • Fetching a template returns a deep-copied instance you can modify freely

  • Comes pre-populated with common seismic templates

Use the top-level helpers for convenience: get_template, list_templates, register_template, is_template_registered, get_template_registry.

class mdio.builder.template_registry.TemplateRegistry

Thread-safe singleton registry for dataset templates.

The registry stores template instances by their public name and returns a deep-copied instance on every retrieval, so callers can safely mutate the returned object without affecting the registry or other callers.

Thread-safety

  • Creation uses double-checked locking to guarantee a single instance.

  • Registry operations are protected by an internal re-entrant lock.

Typical usage

  • Use the module helpers: get_template, list_templates, register_template, and is_template_registered.

  • Alternatively, use TemplateRegistry.get_instance() for direct access.

Return type:

TemplateRegistry

classmethod get_instance()

Get the singleton instance (alternative to constructor).

Returns:

The singleton instance of TemplateRegistry.

Return type:

TemplateRegistry

clear()

Clear all registered templates (useful for testing).

Return type:

None

get(template_name)

Get an instance of a template from the registry by its name.

Each call returns a fresh, independent copy of the template that can be modified without affecting the original template or other copies.

Parameters:

template_name (str) – The name of the template to retrieve.

Returns:

An instance of the template if found.

Raises:

KeyError – If the template is not registered.

Return type:

AbstractDatasetTemplate

is_registered(template_name)

Check if a template is registered in the registry.

Parameters:

template_name (str) – The name of the template to check.

Returns:

True if the template is registered, False otherwise.

Return type:

bool

list_all_templates()

Get all registered template names.

Returns:

A list of all registered template names.

Return type:

list[str]

register(instance)

Register a template instance under its public name.

A deep copy of the provided instance is stored internally to avoid accidental side effects from later external mutations.

Parameters:

instance (AbstractDatasetTemplate) – Template instance to register.

Returns:

The public name of the registered template.

Raises:

ValueError – If a template with the same name is already registered.

Return type:

str

unregister(template_name)

Unregister a template from the registry.

Parameters:

template_name (str) – The name of the template to unregister.

Raises:

KeyError – If the template is not registered.

Return type:

None

mdio.builder.template_registry.get_template(name)

Get an instance of a template from the global registry.

Each call returns a fresh, independent instance of the template that can be modified without affecting the original template or other copies.

Parameters:

name (str) – The name of the template to retrieve.

Returns:

An instance of the template if found.

Return type:

AbstractDatasetTemplate

mdio.builder.template_registry.get_template_registry()

Get the global template registry instance.

Returns:

The singleton instance of TemplateRegistry.

Return type:

TemplateRegistry

mdio.builder.template_registry.is_template_registered(name)

Check if a template is registered in the global registry.

Parameters:

name (str) – The name of the template to check.

Returns:

True if the template is registered, False otherwise.

Return type:

bool

mdio.builder.template_registry.list_templates()

List all registered template names.

The order is implementation-defined and may change; do not rely on it for stable sorting.

Returns:

A list of all registered template names.

Return type:

list[str]

mdio.builder.template_registry.register_template(template)

Register a template in the global registry.

Parameters:

template (AbstractDatasetTemplate) – An instance of AbstractDatasetTemplate to register.

Returns:

The name of the registered template.

Return type:

str