Skip to content

Commit

Permalink
Load config from protean-specific section in pyproject.toml
Browse files Browse the repository at this point in the history
Protean related settings will be nested under [tool.protean.*] sections.
  • Loading branch information
subhashb committed May 28, 2024
1 parent 907d826 commit 2274033
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/protean/domain/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
7 changes: 7 additions & 0 deletions tests/config/test_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
3 changes: 2 additions & 1 deletion tests/support/test_domains/test16/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[tool.protean]
debug = true
testing = true

[custom]
[tool.protean.custom]
FOO = "quux"
3 changes: 2 additions & 1 deletion tests/support/test_domains/test17/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[tool.protean]
debug = true
testing = true

[custom]
[tool.protean.custom]
FOO = "quux"
2 changes: 2 additions & 0 deletions tests/support/test_domains/test24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This folder contains a realistic pyproject.toml file with all protean settings
nested under `tool.protean.*` keys.
4 changes: 4 additions & 0 deletions tests/support/test_domains/test24/domain24.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from protean import Domain


domain = Domain(__file__, "TEST24")
28 changes: 28 additions & 0 deletions tests/support/test_domains/test24/pyproject.toml
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 2274033

Please sign in to comment.