Skip to content

Commit

Permalink
refactor: remove data_folder from networkapis (#2038)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey committed May 1, 2024
1 parent bae1161 commit 422b481
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 25 deletions.
39 changes: 19 additions & 20 deletions src/ape/api/networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,6 @@ class EcosystemAPI(ExtraAttributesMixin, BaseInterfaceModel):
The name of the ecosystem. This should be set the same name as the plugin.
"""

data_folder: Path
"""The path to the ``.ape`` directory."""

request_header: dict
"""A shareable HTTP header for network requests."""

Expand All @@ -85,6 +82,14 @@ class EcosystemAPI(ExtraAttributesMixin, BaseInterfaceModel):
def __repr__(self) -> str:
return f"<{self.name}>"

@property
def data_folder(self) -> Path:
"""
The path to the ecosystem's data folder,
e.g. ``$HOME/.ape/{self.name}`` unless overridden.
"""
return self.config_manager.DATA_FOLDER / self.name

@cached_property
def custom_network(self) -> "NetworkAPI":
"""
Expand All @@ -102,16 +107,11 @@ def custom_network(self) -> "NetworkAPI":
raise NetworkError("Core Ethereum plugin missing.")

request_header = self.config_manager.REQUEST_HEADER
init_kwargs = {
"name": "ethereum",
"data_folder": self.data_folder,
"request_header": request_header,
}
init_kwargs = {"name": "ethereum", "request_header": request_header}
ethereum = ethereum_class(**init_kwargs) # type: ignore
return NetworkAPI(
name="custom",
ecosystem=ethereum,
data_folder=self.data_folder / "custom",
request_header=request_header,
_default_provider="node",
_is_custom=True,
Expand Down Expand Up @@ -223,7 +223,6 @@ def config(self) -> PluginConfig:
Returns:
:class:`ape.api.config.PluginConfig`
"""

return self.config_manager.get_config(self.name)

@property
Expand All @@ -249,7 +248,6 @@ def networks(self) -> dict[str, "NetworkAPI"]:
)

network_data = custom_net.model_dump(by_alias=True, exclude=("default_provider",))
network_data["data_folder"] = self.data_folder / custom_net.name
network_data["ecosystem"] = self
network_type = create_network_type(custom_net.chain_id, custom_net.chain_id)
network_api = network_type.model_validate(network_data)
Expand All @@ -263,10 +261,7 @@ def networks(self) -> dict[str, "NetworkAPI"]:
def _networks_from_plugins(self) -> dict[str, "NetworkAPI"]:
return {
network_name: network_class(
name=network_name,
ecosystem=self,
data_folder=self.data_folder / network_name,
request_header=self.request_header,
name=network_name, ecosystem=self, request_header=self.request_header
)
for _, (ecosystem_name, network_name, network_class) in self.plugin_manager.networks
if ecosystem_name == self.name
Expand Down Expand Up @@ -751,9 +746,6 @@ class NetworkAPI(BaseInterfaceModel):
ecosystem: EcosystemAPI
"""The ecosystem of the network."""

data_folder: Path # For caching any data that might need caching
"""The path to the ``.ape`` directory."""

request_header: dict
"""A shareable network HTTP header."""

Expand Down Expand Up @@ -783,6 +775,15 @@ def __repr__(self) -> str:

return f"<{name}>" if name else f"{type(self)}"

@property
def data_folder(self) -> Path:
"""
The path to the network's data folder,
e.g. ``$HOME/.ape/{self.ecosystem_name}/{self.name}`` unless
overridden.
"""
return self.ecosystem.data_folder / self.name

@property
def ecosystem_config(self) -> PluginConfig:
"""
Expand Down Expand Up @@ -969,8 +970,6 @@ def providers(self): # -> dict[str, Partial[ProviderAPI]]
provider_class,
name=provider_name,
network=self,
# NOTE: No need to have separate folder, caching should be interoperable
data_folder=self.data_folder,
request_header=self.request_header,
)

Expand Down
13 changes: 9 additions & 4 deletions src/ape/api/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,6 @@ class ProviderAPI(BaseInterfaceModel):
provider_settings: dict = {}
"""The settings for the provider, as overrides to the configuration."""

data_folder: Path
"""The path to the ``.ape`` directory."""

request_header: dict
"""A header to set on HTTP/RPC requests."""

Expand All @@ -186,6 +183,14 @@ class ProviderAPI(BaseInterfaceModel):
How many parallel threads to use when fetching logs.
"""

@property
def data_folder(self) -> Path:
"""
The path to the provider's data,
e.g. ``$HOME/.api/{self.name}`` unless overridden.
"""
return self.config_manager.DATA_FOLDER / self.name

@property
@abstractmethod
def is_connected(self) -> bool:
Expand Down Expand Up @@ -266,7 +271,7 @@ def update_settings(self, new_settings: dict):
May require a reconnect.
Args:
new_settings (Dict): The new provider settings.
new_settings (dict): The new provider settings.
"""

@property
Expand Down
3 changes: 2 additions & 1 deletion src/ape_ethereum/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,12 @@ def send_tx_wrapper(self, txn: TransactionAPI) -> ReceiptAPI:
setattr(cls, "connect", post_connect_hook(cls.connect))
return super().__new__(cls) # pydantic v2 doesn't want args

_call_trace_approach: Optional[TraceApproach] = None
"""
Is ``None`` until known.
NOTE: This gets set in `ape_ethereum.trace.Trace`.
"""
_call_trace_approach: Optional[TraceApproach] = None

_supports_debug_trace_call: Optional[bool] = None

def __init__(self, *args, **kwargs):
Expand Down

0 comments on commit 422b481

Please sign in to comment.