Skip to content

Commit d048b0f

Browse files
committed
enhance template_registry.py documentation for clarity and detail
1 parent a55bb63 commit d048b0f

File tree

1 file changed

+65
-19
lines changed

1 file changed

+65
-19
lines changed

src/mdio/builder/template_registry.py

Lines changed: 65 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,22 @@
1-
"""Template registry for MDIO v1 dataset templates."""
1+
"""Template registry for MDIO dataset templates.
2+
3+
This module provides a tiny, thread-safe singleton registry to discover and fetch predefined dataset
4+
templates used by the MDIO builder.
5+
6+
Key points
7+
- Global, thread-safe singleton (safe to use across threads)
8+
- Fetching a template returns a deep-copied instance you can modify freely
9+
- Comes pre-populated with common seismic templates
10+
11+
Use the top-level helpers for convenience: ``get_template``, ``list_templates``, ``register_template``,
12+
``is_template_registered``, ``get_template_registry``.
13+
"""
14+
15+
from __future__ import annotations
216

317
import copy
418
import threading
5-
from typing import Optional
19+
from typing import TYPE_CHECKING
620

721
from mdio.builder.templates.abstract_dataset_template import AbstractDatasetTemplate
822
from mdio.builder.templates.seismic_2d_poststack import Seismic2DPostStackTemplate
@@ -13,15 +27,42 @@
1327
from mdio.builder.templates.seismic_3d_prestack_coca import Seismic3DPreStackCocaTemplate
1428
from mdio.builder.templates.seismic_3d_prestack_shot import Seismic3DPreStackShotTemplate
1529

30+
if TYPE_CHECKING:
31+
from mdio.builder.templates.abstract_dataset_template import AbstractDatasetTemplate
32+
33+
34+
__all__ = [
35+
"TemplateRegistry",
36+
"get_template_registry",
37+
"register_template",
38+
"get_template",
39+
"is_template_registered",
40+
"list_templates",
41+
]
42+
1643

1744
class TemplateRegistry:
18-
"""A thread-safe singleton registry for dataset templates."""
45+
"""Thread-safe singleton registry for dataset templates.
1946
20-
_instance: Optional["TemplateRegistry"] = None
21-
_lock = threading.RLock()
22-
_initialized = False
47+
The registry stores template instances by their public name and returns a deep-copied instance on
48+
every retrieval, so callers can safely mutate the returned object without affecting the registry
49+
or other callers.
2350
24-
def __new__(cls) -> "TemplateRegistry":
51+
Thread-safety
52+
- Creation uses double-checked locking to guarantee a single instance.
53+
- Registry operations are protected by an internal re-entrant lock.
54+
55+
Typical usage
56+
- Use the module helpers: ``get_template``, ``list_templates``, ``register_template``,
57+
and ``is_template_registered``.
58+
- Alternatively, use ``TemplateRegistry.get_instance()`` for direct access.
59+
"""
60+
61+
_instance: TemplateRegistry | None = None
62+
_lock: threading.RLock = threading.RLock()
63+
_initialized: bool = False
64+
65+
def __new__(cls) -> TemplateRegistry:
2566
"""Create or return the singleton instance.
2667
2768
Uses double-checked locking pattern to ensure thread safety.
@@ -41,21 +82,24 @@ def __init__(self) -> None:
4182
with self._lock:
4283
if not self._initialized:
4384
self._templates: dict[str, AbstractDatasetTemplate] = {}
44-
self._registry_lock = threading.RLock()
85+
self._registry_lock: threading.RLock = threading.RLock()
4586
self._register_default_templates()
4687
TemplateRegistry._initialized = True
4788

4889
def register(self, instance: AbstractDatasetTemplate) -> str:
49-
"""Register a template instance by its name.
90+
"""Register a template instance under its public name.
91+
92+
A deep copy of the provided instance is stored internally to avoid accidental side effects
93+
from later external mutations.
5094
5195
Args:
52-
instance: An instance of template to register.
96+
instance: Template instance to register.
5397
5498
Returns:
55-
The name of the registered template.
99+
The public name of the registered template.
56100
57101
Raises:
58-
ValueError: If the template name is already registered.
102+
ValueError: If a template with the same name is already registered.
59103
"""
60104
with self._registry_lock:
61105
name = instance.name
@@ -66,9 +110,9 @@ def register(self, instance: AbstractDatasetTemplate) -> str:
66110
return name
67111

68112
def _register_default_templates(self) -> None:
69-
"""Register default templates if needed.
113+
"""Register built-in seismic templates.
70114
71-
Subclasses can override this method to register default templates.
115+
Subclasses may override this hook to extend or change the defaults.
72116
"""
73117
# Post-Stack Data
74118
self.register(Seismic2DPostStackTemplate("time"))
@@ -92,8 +136,8 @@ def _register_default_templates(self) -> None:
92136
def get(self, template_name: str) -> AbstractDatasetTemplate:
93137
"""Get an instance of a template from the registry by its name.
94138
95-
Each call returns a fresh, independent copy of the template that can be
96-
modified without affecting the original template or other copies.
139+
Each call returns a fresh, independent copy of the template that can be modified without affecting
140+
the original template or other copies.
97141
98142
Args:
99143
template_name: The name of the template to retrieve.
@@ -152,7 +196,7 @@ def clear(self) -> None:
152196
self._templates.clear()
153197

154198
@classmethod
155-
def get_instance(cls) -> "TemplateRegistry":
199+
def get_instance(cls) -> TemplateRegistry:
156200
"""Get the singleton instance (alternative to constructor).
157201
158202
Returns:
@@ -193,8 +237,8 @@ def register_template(template: AbstractDatasetTemplate) -> str:
193237
def get_template(name: str) -> AbstractDatasetTemplate:
194238
"""Get an instance of a template from the global registry.
195239
196-
Each call returns a fresh, independent instance of the template that can be
197-
modified without affecting the original template or other copies.
240+
Each call returns a fresh, independent instance of the template that can be modified without affecting
241+
the original template or other copies.
198242
199243
Args:
200244
name: The name of the template to retrieve.
@@ -220,6 +264,8 @@ def is_template_registered(name: str) -> bool:
220264
def list_templates() -> list[str]:
221265
"""List all registered template names.
222266
267+
The order is implementation-defined and may change; do not rely on it for stable sorting.
268+
223269
Returns:
224270
A list of all registered template names.
225271
"""

0 commit comments

Comments
 (0)