From 2094493eccb932e2b1bc20bbad6986258cfac7d6 Mon Sep 17 00:00:00 2001 From: Daniel <d.hornung@indiscale.com> Date: Wed, 26 Jun 2024 16:39:25 +0200 Subject: [PATCH] ENH: PYLINKAHEADINI instead of PYCAOSDBINI --- CHANGELOG.md | 2 ++ examples/pylinkahead.ini | 2 +- src/doc/configuration.md | 2 +- src/linkahead/__init__.py | 2 +- src/linkahead/configuration.py | 15 +++++++++------ unittests/test_configuration.py | 12 ++++++------ 6 files changed, 20 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e01686cd..7a97a2b7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed ### +* Using environment variable PYLINKAHEADINI instead of PYCAOSDBINI. + ### Deprecated ### ### Removed ### diff --git a/examples/pylinkahead.ini b/examples/pylinkahead.ini index f37e24e0..84d1eb85 100644 --- a/examples/pylinkahead.ini +++ b/examples/pylinkahead.ini @@ -1,7 +1,7 @@ # To be found be the caosdb package, the INI file must be located either in # - $CWD/pylinkahead.ini # - $HOME/.pylinkahead.ini -# - the location given in the env variable PYCAOSDBINI +# - the location given in the env variable PYLINKAHEADINI [Connection] # URL of the CaosDB server diff --git a/src/doc/configuration.md b/src/doc/configuration.md index 54ae251b..427551db 100644 --- a/src/doc/configuration.md +++ b/src/doc/configuration.md @@ -1,6 +1,6 @@ # Configuration of PyLinkAhead # The behavior of PyLinkAhead is defined via a configuration that is provided using configuration files. -PyLinkAhead tries to read from the inifile specified in the environment variable `PYCAOSDBINI` or +PyLinkAhead tries to read from the inifile specified in the environment variable `PYLINKAHEADINI` or alternatively in `~/.pylinkahead.ini` upon import. After that, the ini file `pylinkahead.ini` in the current working directory will be read additionally, if it exists. diff --git a/src/linkahead/__init__.py b/src/linkahead/__init__.py index 3a8c5ba3..cd54f8f4 100644 --- a/src/linkahead/__init__.py +++ b/src/linkahead/__init__.py @@ -24,7 +24,7 @@ """LinkAhead Python bindings. -Tries to read from the inifile specified in the environment variable `PYCAOSDBINI` or +Tries to read from the inifile specified in the environment variable `PYLINKAHEADINI` or alternatively in `~/.pylinkahead.ini` upon import. After that, the ini file `pylinkahead.ini` in the current working directory will be read additionally, if it exists. diff --git a/src/linkahead/configuration.py b/src/linkahead/configuration.py index b020467c..f3f50ae3 100644 --- a/src/linkahead/configuration.py +++ b/src/linkahead/configuration.py @@ -102,7 +102,7 @@ def validate_yaml_schema(valobj: dict[str, dict[str, Union[int, str, bool]]]): def _read_config_files() -> list[str]: """Read config files from different paths. - Read the config from either ``$PYCAOSDBINI`` or home directory (``~/.pylinkahead.ini``), and + Read the config from either ``$PYLINKAHEADINI`` or home directory (``~/.pylinkahead.ini``), and additionally adds config from a config file in the current working directory (``pylinkahead.ini``). If deprecated names are used (starting with 'pycaosdb'), those used in addition but the files @@ -131,15 +131,18 @@ def _read_config_files() -> list[str]: warnings.warn("\n\nYou have a config file with the old naming scheme (pycaosdb.ini). " f"Please use the new version and rename\n" f" {ini_cwd_caosdb}\nto\n {ini_cwd}", DeprecationWarning) + if "PYCAOSDBINI" in environ: + warnings.warn("\n\nYou have an environment variable PYCAOSDBINI. " + f"Please rename it to PYLINKAHEADINI.", DeprecationWarning) # End: LinkAhead rename block ################################################## - if "PYCAOSDBINI" in environ: - if not isfile(expanduser(environ["PYCAOSDBINI"])): + if "PYLINKAHEADINI" in environ: + if not isfile(expanduser(environ["PYLINKAHEADINI"])): raise RuntimeError( - f"No configuration file found at\n{expanduser(environ['PYCAOSDBINI'])}" - "\nwhich was given via the environment variable PYCAOSDBINI" + f"No configuration file found at\n{expanduser(environ['PYLINKAHEADINI'])}" + "\nwhich was given via the environment variable PYLINKAHEADINI" ) - return_var.extend(configure(expanduser(environ["PYCAOSDBINI"]))) + return_var.extend(configure(expanduser(environ["PYLINKAHEADINI"]))) else: if isfile(ini_user_caosdb): return_var.extend(configure(ini_user_caosdb)) diff --git a/unittests/test_configuration.py b/unittests/test_configuration.py index 40506e87..95bc906c 100644 --- a/unittests/test_configuration.py +++ b/unittests/test_configuration.py @@ -45,24 +45,24 @@ def temp_ini_files(): remove("pylinkahead.ini") if created_temp_ini_home: remove(expanduser("~/.pylinkahead.ini")) - environ["PYCAOSDBINI"] = "~/.pylinkahead.ini" + environ["PYLINKAHEADINI"] = "~/.pylinkahead.ini" def test_config_ini_via_envvar(temp_ini_files): with raises(KeyError): - environ["PYCAOSDBINI"] + environ["PYLINKAHEADINI"] - environ["PYCAOSDBINI"] = "bla bla" - assert environ["PYCAOSDBINI"] == "bla bla" + environ["PYLINKAHEADINI"] = "bla bla" + assert environ["PYLINKAHEADINI"] == "bla bla" # test wrong configuration file in envvar with pytest.raises(RuntimeError): db.configuration._read_config_files() # test good configuration file in envvar - environ["PYCAOSDBINI"] = "~/.pylinkahead.ini" + environ["PYLINKAHEADINI"] = "~/.pylinkahead.ini" assert expanduser("~/.pylinkahead.ini") in db.configuration._read_config_files() # test without envvar - environ.pop("PYCAOSDBINI") + environ.pop("PYLINKAHEADINI") assert expanduser("~/.pylinkahead.ini") in db.configuration._read_config_files() # test configuration file in cwd assert join(getcwd(), "pylinkahead.ini") in db.configuration._read_config_files() -- GitLab