Skip to content

Commit 5477a8d

Browse files
Merge pull request #86 from lsst/tickets/DM-48656
DM-48656: Fix ApdbReplica.from_uri to understand new YAML config format
2 parents cb3d4e2 + 9e46503 commit 5477a8d

File tree

9 files changed

+35
-44
lines changed

9 files changed

+35
-44
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ repos:
88
- id: end-of-file-fixer
99
- id: trailing-whitespace
1010
- repo: https://github.com/psf/black-pre-commit-mirror
11-
rev: 24.10.0
11+
rev: 25.1.0
1212
hooks:
1313
- id: black
1414
# It is recommended to specify the latest version of Python
@@ -17,7 +17,7 @@ repos:
1717
# https://pre-commit.com/#top_level-default_language_version
1818
language_version: python3.11
1919
- repo: https://github.com/pycqa/isort
20-
rev: 5.13.2
20+
rev: 6.0.0
2121
hooks:
2222
- id: isort
2323
name: isort (python)

python/lsst/dax/apdb/apdb.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
from lsst.resources import ResourcePathExpression
3333
from lsst.sphgeom import Region
3434

35-
from .apdbIndex import ApdbIndex
3635
from .apdbSchema import ApdbTables
3736
from .config import ApdbConfig
3837
from .factory import make_apdb
@@ -82,22 +81,6 @@ def from_uri(cls, uri: ResourcePathExpression) -> Apdb:
8281
Instance of `Apdb` class, the type of the returned instance is
8382
determined by configuration.
8483
"""
85-
if isinstance(uri, str) and uri.startswith("label:"):
86-
tag, _, label = uri.partition(":")
87-
index = ApdbIndex()
88-
# Try to find YAML format first, and pex_config if YAML is not
89-
# found. During transitional period we support conversion of
90-
# pex_config format to a new pydantic format.
91-
try:
92-
uri = index.get_apdb_uri(label, "yaml")
93-
except ValueError as yaml_exc:
94-
try:
95-
uri = index.get_apdb_uri(label, "pex_config")
96-
except ValueError:
97-
# If none is found then re-raise exception from yaml
98-
# attempt, but add a note that pex_config is missing too.
99-
yaml_exc.add_note(f"Legacy label {label}/pex_config is also missing.")
100-
raise yaml_exc from None
10184
config = ApdbConfig.from_uri(uri)
10285
return make_apdb(config)
10386

python/lsst/dax/apdb/apdbReplica.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,12 @@
2727
from abc import ABC, abstractmethod
2828
from collections.abc import Collection, Iterable, Sequence
2929
from dataclasses import dataclass
30-
from typing import TYPE_CHECKING, cast
30+
from typing import TYPE_CHECKING
3131

3232
import astropy.time
33-
from lsst.pex.config import Config
34-
from lsst.resources import ResourcePath, ResourcePathExpression
33+
from lsst.resources import ResourcePathExpression
3534

3635
from .apdb import ApdbConfig
37-
from .apdbIndex import ApdbIndex
3836
from .factory import make_apdb_replica
3937

4038
if TYPE_CHECKING:
@@ -146,16 +144,7 @@ def from_uri(cls, uri: ResourcePathExpression) -> ApdbReplica:
146144
Instance of `ApdbReplica` class, the type of the returned instance
147145
is determined by configuration.
148146
"""
149-
if isinstance(uri, str) and uri.startswith("label:"):
150-
tag, _, label = uri.partition(":")
151-
index = ApdbIndex()
152-
# Current format for config files is "pex_config"
153-
format = "pex_config"
154-
uri = index.get_apdb_uri(label, format)
155-
path = ResourcePath(uri)
156-
config_str = path.read().decode()
157-
# Assume that this is ApdbConfig, make_apdb will raise if not.
158-
config = cast(ApdbConfig, Config._fromPython(config_str))
147+
config = ApdbConfig.from_uri(uri)
159148
return make_apdb_replica(config)
160149

161150
@classmethod

python/lsst/dax/apdb/config.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from lsst.resources import ResourcePath, ResourcePathExpression
3333
from pydantic import BaseModel, Field
3434

35+
from .apdbIndex import ApdbIndex
3536
from .factory import config_type_for_name
3637

3738

@@ -97,6 +98,23 @@ def from_uri(cls, uri: ResourcePathExpression) -> ApdbConfig:
9798
config : `ApdbConfig`
9899
Apdb configuration object.
99100
"""
101+
if isinstance(uri, str) and uri.startswith("label:"):
102+
tag, _, label = uri.partition(":")
103+
index = ApdbIndex()
104+
# Try to find YAML format first, and pex_config if YAML is not
105+
# found. During transitional period we support conversion of
106+
# pex_config format to a new pydantic format.
107+
try:
108+
uri = index.get_apdb_uri(label, "yaml")
109+
except ValueError as yaml_exc:
110+
try:
111+
uri = index.get_apdb_uri(label, "pex_config")
112+
except ValueError:
113+
# If none is found then re-raise exception from yaml
114+
# attempt, but add a note that pex_config is missing too.
115+
yaml_exc.add_note(f"Legacy label {label}/pex_config is also missing.")
116+
raise yaml_exc from None
117+
100118
path = ResourcePath(uri)
101119
config_bytes = path.read()
102120

python/lsst/dax/apdb/sql/apdbSql.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
# You should have received a copy of the GNU General Public License
2020
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2121

22-
"""Module defining Apdb class and related methods.
23-
"""
22+
"""Module defining Apdb class and related methods."""
2423

2524
from __future__ import annotations
2625

python/lsst/dax/apdb/sql/apdbSqlReplica.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
# You should have received a copy of the GNU General Public License
2020
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2121

22-
"""Module defining Apdb class and related methods.
23-
"""
22+
"""Module defining Apdb class and related methods."""
2423

2524
from __future__ import annotations
2625

python/lsst/dax/apdb/sql/apdbSqlSchema.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
# You should have received a copy of the GNU General Public License
2020
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2121

22-
"""Module responsible for APDB schema operations.
23-
"""
22+
"""Module responsible for APDB schema operations."""
2423

2524
from __future__ import annotations
2625

tests/test_apdbSql.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
# You should have received a copy of the GNU General Public License
2020
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2121

22-
"""Unit test for Apdb class.
23-
"""
22+
"""Unit test for Apdb class."""
2423

2524
import gc
2625
import os
@@ -32,7 +31,7 @@
3231

3332
import lsst.utils.tests
3433
import sqlalchemy
35-
from lsst.dax.apdb import Apdb, ApdbConfig, ApdbTables
34+
from lsst.dax.apdb import Apdb, ApdbConfig, ApdbReplica, ApdbTables
3635
from lsst.dax.apdb.pixelization import Pixelization
3736
from lsst.dax.apdb.sql import ApdbSql, ApdbSqlConfig
3837
from lsst.dax.apdb.tests import ApdbSchemaUpdateTest, ApdbTest
@@ -264,6 +263,12 @@ def test_remove_database_file(self) -> None:
264263
with self.assertRaisesRegex(sqlalchemy.exc.OperationalError, "unable to open database file"):
265264
Apdb.from_uri(self.config_path)
266265

266+
def test_make_apdb_replica(self) -> None:
267+
"""Check that we can make ApdbReplica instance from config URI."""
268+
ApdbReplica.from_uri(self.config_path)
269+
with self.assertRaises(FileNotFoundError):
270+
Apdb.from_uri(self.bad_config_path)
271+
267272

268273
class MyMemoryTestCase(lsst.utils.tests.MemoryTestCase):
269274
"""Run file leak tests."""

tests/test_apdbSqlSchema.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
# You should have received a copy of the GNU General Public License
2020
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2121

22-
"""Unit test for ApdbSqlSchema class.
23-
"""
22+
"""Unit test for ApdbSqlSchema class."""
2423

2524
import os
2625
import unittest

0 commit comments

Comments
 (0)