diff --git a/CHANGELOG.md b/CHANGELOG.md index c0375bcdcf035e17784755ebcb993e109c4c6058..1c8a144dc939f3a58d1f9346929ad5e32d62d213 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 `caosdb.State` class for more information. * `etag` property for the `caosdb.Query` class. The etag allows to debug the caching and to decide whether the server has changed between queries. +* function `_read_config_files` to read `pycaosdb.ini` files from different paths. ### Changed ### @@ -21,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Removed ### ### Fixed ### +* #45 - test_config_ini_via_envvar ### Security ### diff --git a/src/caosdb/__init__.py b/src/caosdb/__init__.py index b320b8295e2981f99788f141c1a5e8c8952aabce..79e24c699c7285d0a7e000472794de247168030c 100644 --- a/src/caosdb/__init__.py +++ b/src/caosdb/__init__.py @@ -46,15 +46,9 @@ from caosdb.common.models import (ACL, ALL, FIX, NONE, OBLIGATORY, RECOMMENDED, Query, QueryTemplate, Record, RecordType, delete, execute_query, get_global_acl, get_known_permissions, raise_errors) -from caosdb.configuration import configure, get_config +from caosdb.configuration import _read_config_files, configure, get_config from caosdb.connection.connection import configure_connection, get_connection -from caosdb.version import version as __version__ from caosdb.exceptions import * +from caosdb.version import version as __version__ -# read configuration these files - -if "PYCAOSDBINI" in environ: - configure(expanduser(environ["PYCAOSDBINI"])) -else: - configure(expanduser('~/.pycaosdb.ini')) -configure(join(getcwd(), "pycaosdb.ini")) +_read_config_files() diff --git a/src/caosdb/configuration.py b/src/caosdb/configuration.py index 4d0797844182b8465ef5f97a869e31ee4fcaf47d..6e8a9c6ff2083b0c30324722003fb3c08a592191 100644 --- a/src/caosdb/configuration.py +++ b/src/caosdb/configuration.py @@ -28,6 +28,9 @@ except ImportError: # python3 from configparser import ConfigParser +from os import environ, getcwd +from os.path import expanduser, join, isfile + def _reset_config(): global _pycaosdbconf @@ -49,3 +52,20 @@ def configure(inifile): def get_config(): return _pycaosdbconf + + +def _read_config_files(): + """Function to read config files from different paths. Checks for path in $PYCAOSDBINI or home directory (.pycaosdb.ini) and in the current working directory (pycaosdb.ini). + + Returns: + [list]: list with successfully parsed ini-files. Order: env_var or home directory, cwd. Used for testing the function. + """ + return_var = [] + if "PYCAOSDBINI" in environ: + return_var.extend(configure(expanduser(environ["PYCAOSDBINI"]))) + else: + return_var.extend(configure(expanduser('~/.pycaosdb.ini'))) + + if isfile(join(getcwd(), "pycaosdb.ini")): + return_var.extend(configure(join(getcwd(), "pycaosdb.ini"))) + return return_var diff --git a/src/caosdb/connection/connection.py b/src/caosdb/connection/connection.py index fc699ab5db1db36bc1ee63034b6828eec4d16bc1..bcad5d7c9288a79867da864e007e2f3b813fd562 100644 --- a/src/caosdb/connection/connection.py +++ b/src/caosdb/connection/connection.py @@ -611,3 +611,12 @@ class _Connection(object): # pylint: disable=useless-object-inheritance _handle_response_status(http_response) return http_response + + def get_username(self): + """ + Return the username of the current connection. + + Shortcut for: get_connection()._authenticator._credentials_provider.username + """ + return self._authenticator._credentials_provider.username + diff --git a/unittests/test_configuration.py b/unittests/test_configuration.py index 76445b6f262120d6a29c73527a9bf042f85f8a05..b135e7cd65b11be7cb6c4ef2237a41a6639ccbb7 100644 --- a/unittests/test_configuration.py +++ b/unittests/test_configuration.py @@ -22,19 +22,45 @@ # ** end header # +import pytest import caosdb as db +from os import environ, getcwd, remove +from os.path import expanduser, isfile, join from pytest import raises -def test_config_ini_via_envvar(): - from os import environ - from os.path import expanduser +@pytest.fixture +def temp_ini_files(): + created_temp_ini_cwd = False + created_temp_ini_home = False + if not isfile(join(getcwd(), "pycaosdb.ini")): + open("pycaosdb.ini", 'a').close() # create temporary ini file + created_temp_ini_cwd = True + if not isfile(expanduser("~/.pycaosdb.ini")): + open(expanduser("~/.pycaosdb.ini"), 'a').close() # create temporary ini file in home directory + created_temp_ini_home = True + yield 0 + if created_temp_ini_cwd: + remove("pycaosdb.ini") + if created_temp_ini_home: + remove(expanduser("~/.pycaosdb.ini")) + environ["PYCAOSDBINI"] = "~/.pycaosdb.ini" + + +def test_config_ini_via_envvar(temp_ini_files): with raises(KeyError): environ["PYCAOSDBINI"] environ["PYCAOSDBINI"] = "bla bla" assert environ["PYCAOSDBINI"] == "bla bla" - assert db.configuration.configure(environ["PYCAOSDBINI"]) == [] + # test wrong configuration file in envvar + assert not expanduser(environ["PYCAOSDBINI"]) in db.configuration._read_config_files() + # test good configuration file in envvar environ["PYCAOSDBINI"] = "~/.pycaosdb.ini" - assert db.configuration.configure(expanduser(environ["PYCAOSDBINI"])) == [expanduser("~/.pycaosdb.ini")] + assert expanduser("~/.pycaosdb.ini") in db.configuration._read_config_files() + # test without envvar + environ.pop("PYCAOSDBINI") + assert expanduser("~/.pycaosdb.ini") in db.configuration._read_config_files() + # test configuration file in cwd + assert join(getcwd(), "pycaosdb.ini") in db.configuration._read_config_files()