From 2274033c1c8a27c6f6723e4574a245dcac7576db Mon Sep 17 00:00:00 2001 From: Subhash Bhushan Date: Mon, 27 May 2024 15:33:07 -0700 Subject: [PATCH] Load config from protean-specific section in pyproject.toml Protean related settings will be nested under [tool.protean.*] sections. --- src/protean/domain/config.py | 5 ++++ tests/config/test_load.py | 7 +++++ .../test_domains/test16/pyproject.toml | 3 +- .../test_domains/test17/pyproject.toml | 3 +- tests/support/test_domains/test24/README.md | 2 ++ tests/support/test_domains/test24/domain24.py | 4 +++ .../test_domains/test24/pyproject.toml | 28 +++++++++++++++++++ 7 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 tests/support/test_domains/test24/README.md create mode 100644 tests/support/test_domains/test24/domain24.py create mode 100644 tests/support/test_domains/test24/pyproject.toml diff --git a/src/protean/domain/config.py b/src/protean/domain/config.py index 3d6c659d..1a5f9940 100644 --- a/src/protean/domain/config.py +++ b/src/protean/domain/config.py @@ -61,6 +61,11 @@ def find_config_file(directory: str): with open(config_file_name, "rb") as f: config = tomllib.load(f) + # If pyproject.toml, extract protean configuration + # from the 'tool.protean' section + if config_file_name.endswith("pyproject.toml"): + config = config.get("tool", {}).get("protean", {}) + # Merge with defaults config = cls._deep_merge(defaults, config) diff --git a/tests/config/test_load.py b/tests/config/test_load.py index 47b60e80..732b691f 100644 --- a/tests/config/test_load.py +++ b/tests/config/test_load.py @@ -147,3 +147,10 @@ def test_that_config_is_loaded_from_2nd_parent_folder_of_path(): domain = derive_domain("src/publishing/domain23") assert domain.config["custom"]["foo"] == "grault" + + +def test_that_config_is_loaded_from_a_sub_context_in_pyproject_toml(): + change_working_directory_to("test24") + + domain = derive_domain("src/publishing/domain24") + assert domain.config["custom"]["foo"] == "garply" diff --git a/tests/support/test_domains/test16/pyproject.toml b/tests/support/test_domains/test16/pyproject.toml index 5d43ac73..9c390b74 100644 --- a/tests/support/test_domains/test16/pyproject.toml +++ b/tests/support/test_domains/test16/pyproject.toml @@ -1,5 +1,6 @@ +[tool.protean] debug = true testing = true -[custom] +[tool.protean.custom] FOO = "quux" \ No newline at end of file diff --git a/tests/support/test_domains/test17/pyproject.toml b/tests/support/test_domains/test17/pyproject.toml index 5d43ac73..9c390b74 100644 --- a/tests/support/test_domains/test17/pyproject.toml +++ b/tests/support/test_domains/test17/pyproject.toml @@ -1,5 +1,6 @@ +[tool.protean] debug = true testing = true -[custom] +[tool.protean.custom] FOO = "quux" \ No newline at end of file diff --git a/tests/support/test_domains/test24/README.md b/tests/support/test_domains/test24/README.md new file mode 100644 index 00000000..32ff7fba --- /dev/null +++ b/tests/support/test_domains/test24/README.md @@ -0,0 +1,2 @@ +This folder contains a realistic pyproject.toml file with all protean settings +nested under `tool.protean.*` keys. \ No newline at end of file diff --git a/tests/support/test_domains/test24/domain24.py b/tests/support/test_domains/test24/domain24.py new file mode 100644 index 00000000..699ff904 --- /dev/null +++ b/tests/support/test_domains/test24/domain24.py @@ -0,0 +1,4 @@ +from protean import Domain + + +domain = Domain(__file__, "TEST24") diff --git a/tests/support/test_domains/test24/pyproject.toml b/tests/support/test_domains/test24/pyproject.toml new file mode 100644 index 00000000..780adae0 --- /dev/null +++ b/tests/support/test_domains/test24/pyproject.toml @@ -0,0 +1,28 @@ +[tool.protean] +debug = true +testing = true +secret_key = "tvTpk3PAfkGr5x9!2sFU%XpW7bR8cwKA" +identity_strategy = "uuid" +identity_type = "string" +event_processing = "sync" +command_processing = "sync" + +[tool.protean.databases.default] +provider = "memory" + +[tool.protean.databases.memory] +provider = "memory" + +[tool.protean.databases.sqlite] +provider = "sqlalchemy" +database = "sqlite" +database_uri = "sqlite:///test.db" + +[tool.protean.brokers.default] +provider = "inline" + +[tool.protean.caches.default] +provider = "memory" + +[tool.protean.custom] +foo = "garply" \ No newline at end of file