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 21, 2024
1 parent 8bfe292 commit 49864e1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 29 deletions.
39 changes: 19 additions & 20 deletions src/ape/api/networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,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 @@ -86,6 +83,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 @@ -103,16 +108,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 @@ -224,7 +224,6 @@ def config(self) -> PluginConfig:
Returns:
:class:`ape.api.config.PluginConfig`
"""

return self.config_manager.get_config(self.name)

@property
Expand All @@ -250,7 +249,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 @@ -264,10 +262,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 @@ -773,9 +768,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 @@ -805,6 +797,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 @@ -990,8 +991,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
6 changes: 1 addition & 5 deletions src/ape/managers/networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,7 @@ def ecosystems(self) -> dict[str, EcosystemAPI]:
@cached_property
def _plugin_ecosystems(self) -> dict[str, EcosystemAPI]:
def to_kwargs(name: str) -> dict:
return {
"name": name,
"data_folder": self.config_manager.DATA_FOLDER / name,
"request_header": self.config_manager.REQUEST_HEADER,
}
return {"name": name, "request_header": self.config_manager.REQUEST_HEADER}

# Load plugins.
plugins = self.plugin_manager.ecosystems
Expand Down

0 comments on commit 49864e1

Please sign in to comment.