Skip to content

Commit

Permalink
refactor!: use name instead of data_folder for accounts (#2025)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Apr 29, 2024
1 parent d2f730f commit 61a3f89
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
30 changes: 28 additions & 2 deletions src/ape/api/accounts.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
from pathlib import Path
from typing import TYPE_CHECKING, Any, Iterator, Optional, Union

Expand Down Expand Up @@ -402,9 +403,11 @@ class AccountContainerAPI(BaseInterfaceModel):
"""

"""
The path to the account's data.
The name of the account container.
For example, the ``ape-ledger`` plugin
uses ``"ledger"`` as its name.
"""
data_folder: Path
name: str

"""
The type of account in this container.
Expand Down Expand Up @@ -432,6 +435,16 @@ def accounts(self) -> Iterator[AccountAPI]:
Iterator[:class:`~ape.api.accounts.AccountAPI`]
"""

@property
def data_folder(self) -> Path:
"""
The path to the account data files.
Defaults to ``$HOME/.ape/<plugin_name>`` unless overriden.
"""
path = self.config_manager.DATA_FOLDER / self.name
path.mkdir(parents=True, exist_ok=True)
return path

@abstractmethod
def __len__(self) -> int:
"""
Expand Down Expand Up @@ -552,6 +565,19 @@ class TestAccountContainerAPI(AccountContainerAPI):
``AccountContainerAPI`` directly. Then, they show up in the ``accounts`` test fixture.
"""

@property
def data_folder(self) -> Path:
"""
**NOTE**: Test account containers do not touch
persistant data. By default and unless overriden,
this property returns the path to ``/dev/null`` and
it is not used for anything.
"""
if os.name == "posix":
return Path("/dev/null")

return Path("NUL")

@abstractmethod
def generate_account(self) -> "TestAccountAPI":
"""
Expand Down
9 changes: 2 additions & 7 deletions src/ape/managers/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ def containers(self) -> dict[str, TestAccountContainerAPI]:
t for t in self.plugin_manager.account_types if issubclass(t[1][1], TestAccountAPI)
]
for plugin_name, (container_type, account_type) in account_types:
# Pydantic validation won't allow passing None for data_folder/required attr
containers[plugin_name] = container_type(data_folder="", account_type=account_type)
containers[plugin_name] = container_type(name=plugin_name, account_type=account_type)

return containers

Expand Down Expand Up @@ -174,11 +173,7 @@ def containers(self) -> dict[str, AccountContainerAPI]:
if issubclass(account_type, TestAccountAPI):
continue

accounts_folder = data_folder / plugin_name
accounts_folder.mkdir(exist_ok=True)
containers[plugin_name] = container_type(
data_folder=accounts_folder, account_type=account_type
)
containers[plugin_name] = container_type(name=plugin_name, account_type=account_type)

return containers

Expand Down

0 comments on commit 61a3f89

Please sign in to comment.