From 316379a7932437d0b1eec467c6e2e16efec6903c Mon Sep 17 00:00:00 2001 From: antazoey Date: Sun, 12 Jan 2025 20:15:22 -0600 Subject: [PATCH] feat: config can load project name from `pyproject.toml` project setting (#2461) Co-authored-by: antazoey --- docs/userguides/config.md | 54 +++++++++++++++++++++++---------- src/ape/utils/misc.py | 9 +++++- tests/functional/test_config.py | 10 ++++++ 3 files changed, 56 insertions(+), 17 deletions(-) diff --git a/docs/userguides/config.md b/docs/userguides/config.md index 0eb62f5897..919e070e6d 100644 --- a/docs/userguides/config.md +++ b/docs/userguides/config.md @@ -170,33 +170,28 @@ contract = project.MyContract.deployments[0] Ape does not add or edit deployments in your `ape-config.yaml` file. ``` -## Node +## Name -When using the `node` provider, you can customize its settings. -For example, to change the URI for an Ethereum network, do: +Configure the name of the project: ```toml -[tool.ape.node.ethereum.mainnet] -uri = "http://localhost:5030" +[tool.ape] +name = "ape-project" ``` -Or the equivalent YAML: +If the name is not specified in `tool.ape` but is in `project`, Ape will use that as the project name: -```yaml -node: - ethereum: - mainnet: - uri: http://localhost:5030 +```toml +[project] +name = "ape-project" ``` -Now, the `ape-node` core plugin will use the URL `http://localhost:5030` to connect and make requests. +To configure this name using an `ape-config.yaml` file, do: -```{warning} -Instead of using `ape-node` to connect to an Infura or Alchemy node, use the [ape-infura](https://github.com/ApeWorX/ape-infura) or [ape-alchemy](https://github.com/ApeWorX/ape-alchemy) provider plugins instead, which have their own way of managing API keys via environment variables. +```yaml +name: ape-project ``` -For more information on networking as a whole, see [this guide](./networks.html). - ## Networks Set default network and network providers: @@ -246,6 +241,33 @@ ethereum: For the local network configuration, the default is `"max"`. Otherwise, it is `"auto"`. +## Node + +When using the `node` provider, you can customize its settings. +For example, to change the URI for an Ethereum network, do: + +```toml +[tool.ape.node.ethereum.mainnet] +uri = "http://localhost:5030" +``` + +Or the equivalent YAML: + +```yaml +node: + ethereum: + mainnet: + uri: http://localhost:5030 +``` + +Now, the `ape-node` core plugin will use the URL `http://localhost:5030` to connect and make requests. + +```{warning} +Instead of using `ape-node` to connect to an Infura or Alchemy node, use the [ape-infura](https://github.com/ApeWorX/ape-infura) or [ape-alchemy](https://github.com/ApeWorX/ape-alchemy) provider plugins instead, which have their own way of managing API keys via environment variables. +``` + +For more information on networking as a whole, see [this guide](./networks.html). + ## Plugins Set which `ape` plugins you want to always use. diff --git a/src/ape/utils/misc.py b/src/ape/utils/misc.py index b369f7b1c5..695ce513a5 100644 --- a/src/ape/utils/misc.py +++ b/src/ape/utils/misc.py @@ -218,7 +218,14 @@ def load_config(path: Path, expand_envars=True, must_exist=False) -> dict: contents = expand_environment_variables(contents) if path.name == "pyproject.toml": - config = tomllib.loads(contents).get("tool", {}).get("ape", {}) + pyproject_toml = tomllib.loads(contents) + config = pyproject_toml.get("tool", {}).get("ape", {}) + + # Utilize [project] for some settings. + if project_settings := pyproject_toml.get("project"): + if "name" not in config and "name" in project_settings: + config["name"] = project_settings["name"] + elif path.suffix in (".json",): config = json.loads(contents) elif path.suffix in (".yml", ".yaml"): diff --git a/tests/functional/test_config.py b/tests/functional/test_config.py index ec49a34c0f..6d82b36598 100644 --- a/tests/functional/test_config.py +++ b/tests/functional/test_config.py @@ -196,6 +196,16 @@ def test_validate_file_shows_linenos_handles_lists(): assert "-->4" in str(err.value) +def test_validate_file_uses_project_name(): + name = "apexampledapp" + with create_tempdir() as temp_dir: + file = temp_dir / "pyproject.toml" + content = f'[project]\nname = "{name}"\n' + file.write_text(content) + cfg = ApeConfig.validate_file(file) + assert cfg.name == name + + def test_deployments(networks_connected_to_tester, owner, vyper_contract_container, project): _ = networks_connected_to_tester # Connection needs to lookup config.