LSST Applications g0603fd7c41+501e3db9f9,g0aad566f14+23d8574c86,g0dd44d6229+a1a4c8b791,g2079a07aa2+86d27d4dc4,g2305ad1205+a62672bbc1,g2bbee38e9b+047b288a59,g337abbeb29+047b288a59,g33d1c0ed96+047b288a59,g3a166c0a6a+047b288a59,g3d1719c13e+23d8574c86,g487adcacf7+cb7fd919b2,g4be5004598+23d8574c86,g50ff169b8f+96c6868917,g52b1c1532d+585e252eca,g591dd9f2cf+4a9e435310,g63cd9335cc+585e252eca,g858d7b2824+23d8574c86,g88963caddf+0cb8e002cc,g99cad8db69+43388bcaec,g9ddcbc5298+9a081db1e4,ga1e77700b3+a912195c07,gae0086650b+585e252eca,gb0e22166c9+60f28cb32d,gb2522980b2+793639e996,gb3a676b8dc+b4feba26a1,gb4b16eec92+63f8520565,gba4ed39666+c2a2e4ac27,gbb8dafda3b+a5d255a82e,gc120e1dc64+d820f8acdb,gc28159a63d+047b288a59,gc3e9b769f7+f4f1cc6b50,gcf0d15dbbd+a1a4c8b791,gdaeeff99f8+f9a426f77a,gdb0af172c8+b6d5496702,ge79ae78c31+047b288a59,w.2024.19
LSST Data Management Base Package
Loading...
Searching...
No Matches
Public Member Functions | Static Public Attributes | Protected Member Functions | Protected Attributes | Static Protected Attributes | List of all members
lsst.dax.apdb.apdbIndex.ApdbIndex Class Reference

Public Member Functions

 __init__ (self, str|None index_path=None)
 
ResourcePath get_apdb_uri (self, str label, str|None format=None)
 
Mapping[str, str] get_entries (self)
 

Static Public Attributes

ClassVar index_env_var = "DAX_APDB_INDEX_URI"
 

Protected Member Functions

Mapping[str, str] _read_index (self, str|None index_path=None)
 

Protected Attributes

 _index_path
 
 _cache
 

Static Protected Attributes

Mapping _cache = None
 

Detailed Description

Index of well-known Apdb instances.

Parameters
----------
index_path : `str`, optional
    Path to the index configuration file, if not provided then the value
    of ``DAX_APDB_INDEX_URI`` environment variable is used to locate
    configuration file.

The index is configured from a simple YAML file whose location is
determined from ``DAX_APDB_INDEX_URI`` environment variable. The content
of the index file is a mapping of the labels to URIs in YAML format, e.g.:

.. code-block:: yaml

   dev: "/path/to/config-file.yaml"
   "prod/pex_config": "s3://bucket/apdb-prod.py"
   "prod/yaml": "s3://bucket/apdb-prod.yaml"

The labels in the index file consists of the label name and an optional
format name separated from label by slash. `get_apdb_uri` method can
use its ``format`` argument to return either a format-specific
configuration or a label-only configuration if format-specific is not
defined.

Definition at line 39 of file apdbIndex.py.

Constructor & Destructor Documentation

◆ __init__()

lsst.dax.apdb.apdbIndex.ApdbIndex.__init__ ( self,
str | None index_path = None )

Definition at line 74 of file apdbIndex.py.

74 def __init__(self, index_path: str | None = None):
75 self._index_path = index_path
76

Member Function Documentation

◆ _read_index()

Mapping[str, str] lsst.dax.apdb.apdbIndex.ApdbIndex._read_index ( self,
str | None index_path = None )
protected
Return contents of the index file.

Parameters
----------
index_path : `str`, optional
    Location of the index file, if not provided then default location
    is used.

Returns
-------
entries : `~collections.abc.Mapping` [`str`, `str`]
    All known entries. Can be empty if no index can be found.

Raises
------
RuntimeError
    Raised if ``index_path`` is not provided and environment variable
    is not set.
TypeError
    Raised if content of the configuration file is incorrect.

Definition at line 77 of file apdbIndex.py.

77 def _read_index(self, index_path: str | None = None) -> Mapping[str, str]:
78 """Return contents of the index file.
79
80 Parameters
81 ----------
82 index_path : `str`, optional
83 Location of the index file, if not provided then default location
84 is used.
85
86 Returns
87 -------
88 entries : `~collections.abc.Mapping` [`str`, `str`]
89 All known entries. Can be empty if no index can be found.
90
91 Raises
92 ------
93 RuntimeError
94 Raised if ``index_path`` is not provided and environment variable
95 is not set.
96 TypeError
97 Raised if content of the configuration file is incorrect.
98 """
99 if self._cache is not None:
100 return self._cache
101 if index_path is None:
102 index_path = os.getenv(self.index_env_var)
103 if not index_path:
104 raise RuntimeError(
105 f"No repository index defined, environment variable {self.index_env_var} is not set."
106 )
107 index_uri = ResourcePath(index_path)
108 _LOG.debug("Opening YAML index file: %s", index_uri.geturl())
109 try:
110 content = index_uri.read()
111 except IsADirectoryError as exc:
112 raise FileNotFoundError(f"Index file {index_uri.geturl()} is a directory") from exc
113 stream = io.BytesIO(content)
114 if index_data := yaml.load(stream, Loader=yaml.SafeLoader):
115 try:
116 self._cache = TypeAdapter(dict[str, str]).validate_python(index_data)
117 except ValidationError as e:
118 raise TypeError(f"Repository index {index_uri.geturl()} not in expected format") from e
119 else:
120 self._cache = {}
121 return self._cache
122

◆ get_apdb_uri()

ResourcePath lsst.dax.apdb.apdbIndex.ApdbIndex.get_apdb_uri ( self,
str label,
str | None format = None )
Return URI for APDB configuration file given its label.

Parameters
----------
label : `str`
    Label of APDB instance.
format : `str`
    Format of the APDB configuration file, arbitrary string. This can
    be used to support an expected migration from pex_config to YAML
    configuration for APDB, code that uses pex_config could provide
    "pex_config" for ``format``. The actual key in the index is
    either a slash-separated label and format, or, if that is missing,
    just a label.

Returns
-------
uri : `~lsst.resources.ResourcePath`
    URI for the configuration file for APDB instance.

Raises
------
FileNotFoundError
    Raised if an index is defined in the environment but it
    can not be found.
ValueError
    Raised if the label is not found in the index.
RuntimeError
    Raised if ``index_path`` is not provided and environment variable
    is not set.
TypeError
    Raised if the format of the index file is incorrect.

Definition at line 123 of file apdbIndex.py.

123 def get_apdb_uri(self, label: str, format: str | None = None) -> ResourcePath:
124 """Return URI for APDB configuration file given its label.
125
126 Parameters
127 ----------
128 label : `str`
129 Label of APDB instance.
130 format : `str`
131 Format of the APDB configuration file, arbitrary string. This can
132 be used to support an expected migration from pex_config to YAML
133 configuration for APDB, code that uses pex_config could provide
134 "pex_config" for ``format``. The actual key in the index is
135 either a slash-separated label and format, or, if that is missing,
136 just a label.
137
138 Returns
139 -------
140 uri : `~lsst.resources.ResourcePath`
141 URI for the configuration file for APDB instance.
142
143 Raises
144 ------
145 FileNotFoundError
146 Raised if an index is defined in the environment but it
147 can not be found.
148 ValueError
149 Raised if the label is not found in the index.
150 RuntimeError
151 Raised if ``index_path`` is not provided and environment variable
152 is not set.
153 TypeError
154 Raised if the format of the index file is incorrect.
155 """
156 index = self._read_index(self._index_path)
157 labels: list[str] = [label]
158 if format:
159 labels.insert(0, f"{label}/{format}")
160 for label in labels:
161 if (uri_str := index.get(label)) is not None:
162 return ResourcePath(uri_str)
163 if len(labels) == 1:
164 message = f"Label {labels[0]} is not defined in index file"
165 else:
166 labels_str = ", ".join(labels)
167 message = f"None of labels {labels_str} is defined in index file"
168 all_labels = set(index)
169 raise ValueError(f"{message}, labels known to index: {all_labels}")
170
daf::base::PropertySet * set
Definition fits.cc:931

◆ get_entries()

Mapping[str, str] lsst.dax.apdb.apdbIndex.ApdbIndex.get_entries ( self)
Retrieve all entries defined in index.

Returns
-------
entries : `~collections.abc.Mapping` [`str`, `str`]
    All known index entries.

Raises
------
RuntimeError
    Raised if ``index_path`` is not provided and environment variable
    is not set.
TypeError
    Raised if content of the configuration file is incorrect.

Definition at line 171 of file apdbIndex.py.

171 def get_entries(self) -> Mapping[str, str]:
172 """Retrieve all entries defined in index.
173
174 Returns
175 -------
176 entries : `~collections.abc.Mapping` [`str`, `str`]
177 All known index entries.
178
179 Raises
180 ------
181 RuntimeError
182 Raised if ``index_path`` is not provided and environment variable
183 is not set.
184 TypeError
185 Raised if content of the configuration file is incorrect.
186 """
187 return self._read_index(self._index_path)

Member Data Documentation

◆ _cache [1/2]

Mapping lsst.dax.apdb.apdbIndex.ApdbIndex._cache = None
staticprotected

Definition at line 71 of file apdbIndex.py.

◆ _cache [2/2]

lsst.dax.apdb.apdbIndex.ApdbIndex._cache
protected

Definition at line 116 of file apdbIndex.py.

◆ _index_path

lsst.dax.apdb.apdbIndex.ApdbIndex._index_path
protected

Definition at line 75 of file apdbIndex.py.

◆ index_env_var

ClassVar lsst.dax.apdb.apdbIndex.ApdbIndex.index_env_var = "DAX_APDB_INDEX_URI"
static

Definition at line 66 of file apdbIndex.py.


The documentation for this class was generated from the following file: