-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
intorduce amend_config context manager
Signed-off-by: Jose Borreguero <[email protected]>
- Loading branch information
Showing
5 changed files
with
118 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# standard imports | ||
from contextlib import contextmanager | ||
from copy import deepcopy | ||
from typing import Union | ||
|
||
# third-party libraries | ||
from mantid.kernel import ConfigService | ||
|
||
|
||
@contextmanager | ||
def amend_config( | ||
new_config: dict = None, data_dir: Union[str, list] = None, data_dir_insert_mode: str = "prepend" | ||
) -> None: | ||
r""" | ||
Context manager to safely modify Mantid Configuration Service while | ||
the function is executed. | ||
Parameters | ||
---------- | ||
new_config | ||
(key, value) pairs to substitute in the configuration service | ||
data_dir | ||
prepend one (when passing a string) or more (when passing a list) | ||
directories to the list of data search directories. Alternatively, replace instead of prepend. | ||
data_dir_insert_mode | ||
How to insert the data directories. Options are: "prepend" (default) and "replace". | ||
""" | ||
modified_keys = list() | ||
backup = dict() | ||
config = ConfigService.Instance() | ||
if new_config is not None: | ||
SEARCH_ARCHIVE = "datasearch.searcharchive" | ||
if SEARCH_ARCHIVE not in new_config: | ||
new_config[SEARCH_ARCHIVE] = "hfir, sns" | ||
DEFAULT_FACILITY = "default.facility" | ||
if DEFAULT_FACILITY not in new_config: | ||
new_config[DEFAULT_FACILITY] = "SNS" | ||
for key, val in new_config.items(): | ||
backup[key] = config[key] | ||
config[key] = val # config does not have an 'update' method | ||
modified_keys.append(key) | ||
if data_dir is not None: | ||
data_dirs = ( | ||
[ | ||
data_dir, | ||
] | ||
if isinstance(data_dir, str) | ||
else data_dir | ||
) | ||
key = "datasearch.directories" | ||
backup[key] = deepcopy(config[key]) | ||
# prepend or replace our custom data directories to the list of data search directories | ||
if data_dir_insert_mode == "prepend": | ||
config.setDataSearchDirs(data_dirs + list(config.getDataSearchDirs())) | ||
elif data_dir_insert_mode == "replace": | ||
config.setDataSearchDirs(data_dirs) | ||
else: | ||
raise ValueError(f"Invalid data_dir_insert_mode: {data_dir_insert_mode}") | ||
try: | ||
yield | ||
finally: | ||
for key in modified_keys: | ||
config[key] = backup[key] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,11 @@ | ||
# standard imports | ||
import logging | ||
from pathlib import Path | ||
import os | ||
|
||
# third-party imports | ||
import pytest | ||
|
||
def pytest_sessionstart(session): | ||
r"""invoked by pytest at the very beginning""" | ||
logger = logging.getLogger() | ||
# Insert data directory in Mantid config file | ||
mantid_config_dir = Path(os.environ["HOME"]) / ".mantid" | ||
os.makedirs(str(mantid_config_dir), exist_ok=True) # create directory if it doesn't exists | ||
mantid_config_file = mantid_config_dir / "Mantid.user.properties" | ||
write_mode = "a" if mantid_config_file.exists() else "w" # append or create-then-write | ||
with open(mantid_config_file, write_mode) as file_handle: | ||
data_path = str(Path(__file__).parent.parent / "tests/data/liquidsreflectometer-data/nexus") | ||
file_handle.write(f"datasearch.directories={data_path}") | ||
logger.info(f"Appending data directory {data_path} to mantid config file {str(mantid_config_file)}") | ||
|
||
@pytest.fixture(scope="session") | ||
def nexus_dir() -> str: | ||
r"""Absolute path to the event nexus files""" | ||
return str(Path(__file__).parent.parent / "tests/data/liquidsreflectometer-data/nexus") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters