Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/rook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,4 @@

from .__version__ import __author__, __email__, __version__ # noqa: F401

from .config import CONFIG # noqa: F401

from .wsgi import application # noqa
4 changes: 2 additions & 2 deletions src/rook/catalog/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from clisops.exceptions import InvalidCollection

from rook import CONFIG
from rook import config

from .db import DBCatalog


def get_catalog(project):
if CONFIG[f"project:{project}"].get("use_catalog"):
if config.get_project_config(project).get("use_catalog"):
try:
catalog = DBCatalog(project)
return catalog
Expand Down
4 changes: 2 additions & 2 deletions src/rook/catalog/intake.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import fsspec
import intake

from rook import CONFIG
from rook import config

from .base import Catalog
from .util import MAX_DATETIME, MIN_DATETIME, parse_time
Expand All @@ -23,7 +23,7 @@ def __init__(self, project, url=None):
super().__init__(project)
self.url = (
url
or CONFIG.get("catalog", {}).get("intake_catalog_url")
or config.get_config().get("catalog", {}).get("intake_catalog_url")
or DEFAULT_INTAKE_CATALOG_URL
)
self._cat = None
Expand Down
18 changes: 5 additions & 13 deletions src/rook/config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Central access to Rook configuration."""

import json
import sys
from pathlib import Path
from typing import Any

Expand All @@ -10,27 +9,20 @@


_PACKAGE_FILE = Path(__file__)
CONFIG = _get_clisops_config(_PACKAGE_FILE)
_CONFIG = _get_clisops_config(_PACKAGE_FILE)


def get_config() -> dict[str, Any]:
"""Return the current Rook configuration."""
return CONFIG
return _CONFIG


def reload_config() -> dict[str, Any]:
"""Reload Rook configuration from the standard clisops sources."""
global CONFIG
global _CONFIG

CONFIG = _reload_clisops_config(_PACKAGE_FILE)

# Keep the public compatibility alias current without making config depend
# on importing the rest of Rook.
rook_module = sys.modules.get("rook")
if rook_module is not None:
rook_module.CONFIG = CONFIG

return CONFIG
_CONFIG = _reload_clisops_config(_PACKAGE_FILE)
return _CONFIG


def get_project_config(project: str) -> dict[str, Any]:
Expand Down
6 changes: 3 additions & 3 deletions src/rook/director/director.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from clisops.project_utils import get_project_name
from clisops.utils.file_utils import FileMapper

from rook import CONFIG
from rook import config
from clisops.exceptions import InvalidCollection
from rook.catalog import get_catalog

Expand Down Expand Up @@ -35,7 +35,7 @@ def __init__(self, coll, inputs):
self.output_uris = None
self.search_result = None

if CONFIG[f"project:{self.project}"].get("use_catalog"):
if config.get_project_config(self.project).get("use_catalog"):
try:
self.catalog = get_catalog(self.project)
except Exception:
Expand All @@ -48,7 +48,7 @@ def __init__(self, coll, inputs):
def use_fixes(self):
# TODO: don't use fixes
return False
# return CONFIG[f"project:{self.project}"].get("use_fixes", False)
# return config.get_project_config(self.project).get("use_fixes", False)

def _check_apply_fixes(self):
if (
Expand Down
7 changes: 0 additions & 7 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,12 @@ def write_roocs_cfg(stratus):
# TODO: reload configs in clisops
# workaround ... fix code in new clisops.
import clisops
import rook.catalog
import rook.catalog.intake
import rook.director.director
from rook.config import reload_config

cfg = reload_config()
clisops.CONFIG = cfg
clisops.project_utils.CONFIG = cfg
rook.director.director.CONFIG = cfg
rook.catalog.CONFIG = cfg
rook.catalog.intake.CONFIG = cfg
# print("clisops.config", clisops.CONFIG["project:cmip5"]["base_dir"])
# print("rook.config", rook.CONFIG["project:cmip6"]["base_dir"])


@pytest.fixture
Expand Down
8 changes: 4 additions & 4 deletions tests/test_catalog_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
def test_result_files_uses_project_base_dir(monkeypatch):
monkeypatch.setattr(
config,
"CONFIG",
"_CONFIG",
{"project:c3s-cmip6": {"base_dir": "/data/CMIP6"}},
)

Expand All @@ -26,7 +26,7 @@ def test_result_files_uses_project_base_dir(monkeypatch):
def test_result_files_uses_global_s3_base_dir(monkeypatch):
monkeypatch.setattr(
config,
"CONFIG",
"_CONFIG",
{
"project:c3s-cmip6": {"base_dir": "/data/CMIP6"},
"s3": {"base_dir": "s3://example-bucket/data/CMIP6"},
Expand All @@ -45,7 +45,7 @@ def test_result_files_uses_global_s3_base_dir(monkeypatch):
def test_result_files_uses_project_s3_base_dir_override(monkeypatch):
monkeypatch.setattr(
config,
"CONFIG",
"_CONFIG",
{
"project:c3s-cmip6": {
"base_dir": "/data/CMIP6",
Expand All @@ -67,7 +67,7 @@ def test_result_files_uses_project_s3_base_dir_override(monkeypatch):
def test_result_download_urls_keep_data_node_root(monkeypatch):
monkeypatch.setattr(
config,
"CONFIG",
"_CONFIG",
{
"project:c3s-cmip6": {
"base_dir": "/data/CMIP6",
Expand Down
18 changes: 10 additions & 8 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
from rook import config


def test_rook_has_no_config_compatibility_alias():
assert not hasattr(rook, "CONFIG")


def test_get_project_config(monkeypatch):
monkeypatch.setattr(
config,
"CONFIG",
"_CONFIG",
{"project:demo": {"base_dir": "/data/demo"}},
)

Expand All @@ -16,7 +20,7 @@ def test_get_project_config(monkeypatch):
def test_get_storage_base_prefers_project_s3_override(monkeypatch):
monkeypatch.setattr(
config,
"CONFIG",
"_CONFIG",
{
"project:demo": {
"base_dir": "/data/demo",
Expand All @@ -32,7 +36,7 @@ def test_get_storage_base_prefers_project_s3_override(monkeypatch):
def test_get_storage_base_falls_back_to_local_project_root(monkeypatch):
monkeypatch.setattr(
config,
"CONFIG",
"_CONFIG",
{"project:demo": {"base_dir": "/data/demo"}},
)

Expand All @@ -42,19 +46,17 @@ def test_get_storage_base_falls_back_to_local_project_root(monkeypatch):
def test_s3_options_ignore_malformed_optional_json(monkeypatch):
monkeypatch.setattr(
config,
"CONFIG",
"_CONFIG",
{"s3": {"storage_options_json": "not-json", "anon": "false"}},
)

assert config.get_s3_storage_options() == {"anon": False}


def test_reload_config_updates_compatibility_alias(monkeypatch):
def test_reload_config_updates_current_config(monkeypatch):
reloaded = {"project:demo": {"base_dir": "/new/data"}}
monkeypatch.setattr(config, "CONFIG", config.CONFIG)
monkeypatch.setattr(rook, "CONFIG", rook.CONFIG)
monkeypatch.setattr(config, "_CONFIG", config.get_config())
monkeypatch.setattr(config, "_reload_clisops_config", lambda _path: reloaded)

assert config.reload_config() is reloaded
assert config.get_config() is reloaded
assert rook.CONFIG is reloaded
2 changes: 1 addition & 1 deletion tests/test_ops_consolidate.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def search(self, collection, time):
monkeypatch.setattr(consolidate, "get_catalog", lambda _project: DummyCatalog())
monkeypatch.setattr(
config,
"CONFIG",
"_CONFIG",
{
"project:c3s-cmip6": {"base_dir": "/data/CMIP6"},
"s3": {"base_dir": "s3://example-bucket/data/CMIP6"},
Expand Down
12 changes: 6 additions & 6 deletions tests/test_ops_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ def fake_open_zarr(store, **kwargs):
monkeypatch.setattr(helpers.xr, "open_zarr", fake_open_zarr)
monkeypatch.setattr(
config,
"CONFIG",
"_CONFIG",
{"s3": {"anon": "true", "endpoint_url": "https://s3.example.org"}},
)

Expand Down Expand Up @@ -267,7 +267,7 @@ def fail_apply_fixes(_ds_id, _ds):
def test_get_storage_options_for_s3_source(monkeypatch):
monkeypatch.setattr(
config,
"CONFIG",
"_CONFIG",
{"s3": {"anon": "true", "endpoint_url": "https://s3.example.org"}},
)

Expand All @@ -280,7 +280,7 @@ def test_get_storage_options_for_s3_source(monkeypatch):


def test_get_storage_options_without_s3_config(monkeypatch):
monkeypatch.setattr(config, "CONFIG", {})
monkeypatch.setattr(config, "_CONFIG", {})

options = helpers.get_storage_options(source(None, "s3://bucket/file.nc"))

Expand All @@ -290,7 +290,7 @@ def test_get_storage_options_without_s3_config(monkeypatch):
def test_get_storage_options_does_not_depend_on_format(monkeypatch):
monkeypatch.setattr(
config,
"CONFIG",
"_CONFIG",
{"s3": {"anon": "true", "endpoint_url": "https://s3.example.org"}},
)

Expand All @@ -308,7 +308,7 @@ def fake_open(path, **kwargs):
return "DATASET"

monkeypatch.setattr(helpers, "open_xr_dataset", fake_open)
monkeypatch.setattr(config, "CONFIG", {"s3": {"anon": "true"}})
monkeypatch.setattr(config, "_CONFIG", {"s3": {"anon": "true"}})

result = helpers.open_dataset(
source(None, "s3://bucket/reference.json"), apply_fixes=False
Expand Down Expand Up @@ -339,7 +339,7 @@ def fake_open(file_paths, **kwargs):
monkeypatch.setattr(helpers, "apply_dataset_fixes", lambda _ds_id, ds: ds)
monkeypatch.setattr(
config,
"CONFIG",
"_CONFIG",
{"s3": {"anon": "true", "endpoint_url": "https://s3.example.org"}},
)

Expand Down