Skip to content

Commit

Permalink
Refactor: alternative local config directory
Browse files Browse the repository at this point in the history
Refinements
  • Loading branch information
cfleur committed Oct 17, 2024
1 parent 4505d32 commit abe1e44
Show file tree
Hide file tree
Showing 13 changed files with 41 additions and 51 deletions.
12 changes: 6 additions & 6 deletions cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

def _check_config_validity() -> None:
import src
from src.utils.envutils import get_config_path, config_dir_key
from src import utils
try:
config_path = get_config_path(config_dir_key())
config_path = utils.environment.get_config_path()
src.types.Config.load(config_path)
click.echo(click.style("Config is valid", fg="green", bold=True))
except pydantic.ValidationError as e:
Expand Down Expand Up @@ -126,8 +126,8 @@ def request_ginput_status() -> None:
_check_config_validity()

import src # import here so that the CLI is more reactive
from src.utils.envutils import get_config_path, config_dir_key
config_path = get_config_path(config_dir_key())
from src import utils
config_path = utils.environment.get_config_path()
config = src.types.Config.load(config_path)
assert config.profiles is not None, "No profiles config found"
with ftplib.FTP(
Expand Down Expand Up @@ -158,11 +158,11 @@ def print_data_report() -> None:

import rich.console
import src # import here so that the CLI is more reactive
from src.utils.envutils import get_config_path, config_dir_key
from src import utils

console = rich.console.Console()
console.print("Loading config")
config_path = get_config_path(config_dir_key())
config_path = utils.environment.get_config_path()
config = src.types.Config.load(config_path)

# load metadata interface
Expand Down
2 changes: 1 addition & 1 deletion docs/pages/guides/metadata.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ To configure the pipeline with the metadata, you have two options: Save them loc

For this option, you can save the three files `locations.json`, `sensors.json`, and `campaigns.json` to the `config/` directory of the pipeline directory. Be sure to include all the files - even if you don't have any campaigns, save an empty list to `campaigns.json`.

Alternatively, you can store these files locally in a location outside of this repository. This is achieved by setting the environment variable `ERP_CONFIG_DIR` to the full path of the alternate location (ERP <- EM27 Retrieval Pipeline). Note that this should be the same directory that contains the `config.json` file. If this environment variable is not set, the default location the pipeline will look for these files is `./config/`. When setting an alternate config directory, leave `./config/config.template.json` for tests (this file is version-controlled).
Alternatively, you can store these files locally in a location outside of this repository. This is achieved by setting the environment variable `ERP_CONFIG_DIR` to the full path of the alternate location (ERP is short for EM27 Retrieval Pipeline). Note that this should be the same directory that contains the `config.json` file. If this environment variable is not set, the default location the pipeline will look for these files is `config/`. When setting an alternate config directory, it is not necessary to touch the file `config/config.template.json` (this file is version-controlled).

For example, directly setting the environment variable:
```
Expand Down
3 changes: 1 addition & 2 deletions src/bundle/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

sys.path.append(tum_esm_utils.files.rel_to_abs_path("../.."))
from src import types, utils
from src.utils.envutils import get_config_path, config_dir_key
from .load_results import load_results_directory


Expand All @@ -20,7 +19,7 @@ def run(
) -> None:
if config is None:
print("Loading configuration")
config_path = get_config_path(config_dir_key())
config_path = utils.environment.get_config_path()
config = types.Config.load(config_path)

assert config.bundles is not None, "no bundle targets found"
Expand Down
5 changes: 2 additions & 3 deletions src/profiles/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
import tum_esm_utils

sys.path.append(tum_esm_utils.files.rel_to_abs_path("../.."))
from src import types, profiles
from src.utils.envutils import get_config_path, config_dir_key
from src import types, profiles, utils


def run() -> None:
config_path = get_config_path(config_dir_key())
config_path = utils.environment.get_config_path()
config = types.Config.load(config_path)
assert config.profiles is not None, "No profiles config found"

Expand Down
3 changes: 1 addition & 2 deletions src/retrieval/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

sys.path.append(tum_esm_utils.files.rel_to_abs_path("../.."))
from src import types, utils, retrieval
from src.utils.envutils import get_config_path, config_dir_key


def run() -> None:
Expand All @@ -21,7 +20,7 @@ def run() -> None:

# load config
try:
config_path = get_config_path(config_dir_key())
config_path = utils.environment.get_config_path()
config = types.Config.load(config_path)
main_logger.info("Config is valid")
except Exception as e:
Expand Down
2 changes: 1 addition & 1 deletion src/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from . import functions, metadata, report, semaphores, text
from . import environment, functions, metadata, report, semaphores, text
19 changes: 19 additions & 0 deletions src/utils/environment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import os

import tum_esm_utils


CONFIG_DIR_KEY = "ERP_CONFIG_DIR"


def get_config_dir() -> str:
default_config_dir = tum_esm_utils.files.rel_to_abs_path("../../config")
return os.getenv(CONFIG_DIR_KEY, default_config_dir)


def get_config_path() -> str:
default_config_dir = get_config_dir()
return os.path.join(
os.getenv(CONFIG_DIR_KEY, default_config_dir),
"config.json"
)
24 changes: 0 additions & 24 deletions src/utils/envutils.py

This file was deleted.

4 changes: 2 additions & 2 deletions src/utils/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

import em27_metadata

from src.utils.envutils import get_config_dir, config_dir_key
from src import utils


CONFIG_DIR = get_config_dir(config_dir_key())
CONFIG_DIR = utils.environment.get_config_dir()

def load_local_em27_metadata_interface() -> Optional[em27_metadata.EM27MetadataInterface]:
assert os.path.isdir(CONFIG_DIR)
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/test_config.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import pytest

import src
from src.utils.envutils import get_config_path, config_dir_key
from src import utils

@pytest.mark.order(2)
@pytest.mark.integration
def test_config() -> None:
config_path = get_config_path(config_dir_key())
config_path = utils.environment.get_config_path()
src.types.Config.load(config_path)
5 changes: 2 additions & 3 deletions tests/integration/test_metadata_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
import em27_metadata

import src
from src.utils.envutils import get_config_path, config_dir_key

from src import utils

@pytest.mark.order(3)
@pytest.mark.integration
def test_metadata_connection() -> None:
config_path = get_config_path(config_dir_key())
config_path = utils.environment.get_config_path()
config = src.types.Config.load(config_path)
if config.general.metadata is None:
pytest.skip("Remote metadata not configured")
Expand Down
5 changes: 2 additions & 3 deletions tests/integration/test_profiles_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
import ftplib

import src
from src.utils.envutils import get_config_path, config_dir_key

from src import utils

@pytest.mark.order(3)
@pytest.mark.integration
def test_profiles_connection() -> None:
config_path = get_config_path(config_dir_key())
config_path = utils.environment.get_config_path()
config = src.types.Config.load(config_path)
if config.profiles is None:
return
Expand Down
4 changes: 2 additions & 2 deletions tests/repository/test_local_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import pytest

import src
from src.utils.envutils import get_config_dir, config_dir_key
from src import utils


CONFIG_DIR = get_config_dir(config_dir_key())
CONFIG_DIR = utils.environment.get_config_dir()
LOCATIONS_PATH = os.path.join(CONFIG_DIR, "locations.json")
SENSORS_PATH = os.path.join(CONFIG_DIR, "sensors.json")
CAMPAIGNS_PATH = os.path.join(CONFIG_DIR, "campaigns.json")
Expand Down

0 comments on commit abe1e44

Please sign in to comment.