|
1 | 1 | # SPDX-License-Identifier: BSD-3-Clause |
| 2 | +# Copyright 2025 MoDaCor Authors |
| 3 | +# |
| 4 | +# Redistribution and use in source and binary forms, with or without modification, |
| 5 | +# are permitted provided that the following conditions are met: |
| 6 | +# 1. Redistributions of source code must retain the above copyright notice, this |
| 7 | +# list of conditions and the following disclaimer. |
| 8 | +# 2. Redistributions in binary form must reproduce the above copyright notice, |
| 9 | +# this list of conditions and the following disclaimer in the documentation |
| 10 | +# and/or other materials provided with the distribution. |
| 11 | +# 3. Neither the name of the copyright holder nor the names of its contributors |
| 12 | +# may be used to endorse or promote products derived from this software without |
| 13 | +# specific prior written permission. |
| 14 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND |
| 15 | +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 16 | +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 17 | +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
| 18 | +# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 19 | +# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 20 | +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
| 21 | +# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 23 | +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
2 | 24 |
|
3 | | - |
4 | | -__all__ = ["IoSources"] |
5 | 25 | __license__ = "BSD-3-Clause" |
| 26 | +__copyright__ = "Copyright 2025 MoDaCor Authors" |
| 27 | +__status__ = "Alpha" |
| 28 | +__all__ = ["IoSources"] |
| 29 | + |
| 30 | + |
| 31 | +from typing import Any |
| 32 | + |
| 33 | +import numpy as np |
| 34 | +from attrs import define, field |
| 35 | + |
| 36 | +from modacor.io.io_source import IoSource |
| 37 | + |
| 38 | + |
| 39 | +@define |
| 40 | +class IoSources: |
| 41 | + |
| 42 | + defined_sources: dict[str, IoSource] = field(factory=dict) |
| 43 | + |
| 44 | + def register_source(self, source_reference: str, source: IoSource): |
| 45 | + """ |
| 46 | + Register a new source class with the given name. |
| 47 | +
|
| 48 | + Parameters |
| 49 | + ---------- |
| 50 | + source_reference : str |
| 51 | + The reference name of the source to register. |
| 52 | + source : IoSource |
| 53 | + The class of the source to register. |
| 54 | + """ |
| 55 | + if not isinstance(source_reference, str): |
| 56 | + raise TypeError("source_name must be a string") |
| 57 | + if not isinstance(source, IoSource): |
| 58 | + raise TypeError("source_class must be a subclass of IoSource") |
| 59 | + self.defined_sources[source_reference] = source |
| 60 | + |
| 61 | + def get_source(self, source_reference: str) -> IoSource: |
| 62 | + """ |
| 63 | + Get the source class associated with the given name. |
| 64 | +
|
| 65 | + Parameters |
| 66 | + ---------- |
| 67 | + source_reference : str |
| 68 | + The reference name of the source to access. |
| 69 | +
|
| 70 | + Returns |
| 71 | + ------- |
| 72 | + IoSource : |
| 73 | + The source class associated with the provided name. |
| 74 | + """ |
| 75 | + if source_reference not in self.defined_sources: |
| 76 | + raise ValueError(f"Source {source_reference} not registered.") |
| 77 | + return self.defined_sources[source_reference] |
| 78 | + |
| 79 | + def get_data(self, data_reference: str, index: int) -> np.ndarray: |
| 80 | + """ |
| 81 | + Get data from the specified source using the provided data key. |
| 82 | +
|
| 83 | + The data_reference is composed of the source reference and the internal |
| 84 | + data reference, separated by "::". |
| 85 | +
|
| 86 | + Parameters |
| 87 | + ---------- |
| 88 | + data_reference : str |
| 89 | + The reference name of the source to access. |
| 90 | + index : int |
| 91 | + The index to access the data. |
| 92 | +
|
| 93 | + Returns |
| 94 | + ------- |
| 95 | + Any : |
| 96 | + The data associated with the provided key. |
| 97 | + """ |
| 98 | + _source_ref, _data_key = data_reference.split("::", 1) |
| 99 | + _source = self.get_source(_source_ref) |
| 100 | + return _source.get_data(index, _data_key) |
| 101 | + |
| 102 | + def get_static_metadata(self, data_reference: str) -> Any: |
| 103 | + """ |
| 104 | + Get static metadata from the specified source using the provided data key. |
6 | 105 |
|
| 106 | + The data_reference is composed of the source reference and the internal |
| 107 | + data reference, separated by "::". |
7 | 108 |
|
| 109 | + Parameters |
| 110 | + ---------- |
| 111 | + data_reference : str |
| 112 | + The reference name of the source to access. |
8 | 113 |
|
9 | | -class IoSources: ... |
| 114 | + Returns |
| 115 | + ------- |
| 116 | + Any : |
| 117 | + The static metadata associated with the provided key. |
| 118 | + """ |
| 119 | + _source_ref, _data_key = data_reference.split("::", 1) |
| 120 | + _source = self.get_source(_source_ref) |
| 121 | + return _source.get_static_metadata(_data_key) |
0 commit comments