Skip to content

Commit

Permalink
feat: config can load project name from pyproject.toml project sett…
Browse files Browse the repository at this point in the history
…ing (#2461)

Co-authored-by: antazoey <antyzoa@gmail.com>
  • Loading branch information
antazoey and antazoey authored Jan 13, 2025
1 parent 298d690 commit 316379a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 17 deletions.
54 changes: 38 additions & 16 deletions docs/userguides/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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.
Expand Down
9 changes: 8 additions & 1 deletion src/ape/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand Down
10 changes: 10 additions & 0 deletions tests/functional/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down

0 comments on commit 316379a

Please sign in to comment.