Skip to content

Commit

Permalink
perf: networks fix and run perf
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey committed Sep 28, 2023
1 parent 100faec commit 82d3b31
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 35 deletions.
61 changes: 52 additions & 9 deletions src/ape/cli/choices.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import re
from enum import Enum
from functools import lru_cache
from typing import Any, Iterator, List, Optional, Type, Union

import click
Expand Down Expand Up @@ -243,6 +244,53 @@ def fail_from_invalid_choice(self, param):
return self.fail("Invalid choice. Type the number or the alias.", param=param)


_NETWORK_FILTER = Optional[Union[List[str], str]]


def get_networks(
ecosystem: _NETWORK_FILTER = None,
network: _NETWORK_FILTER = None,
provider: _NETWORK_FILTER = None,
) -> LazySequence:
# NOTE: Use str-keys and a lru_cache.
return _get_networks_sequence_from_cache(
_network_filter_to_key(ecosystem),
_network_filter_to_key(network),
_network_filter_to_key(provider),
)


@lru_cache(maxsize=None)
def _get_networks_sequence_from_cache(ecosystem_key: str, network_key: str, provider_key: str):
return LazySequence(
networks.get_network_choices(
ecosystem_filter=_key_to_network_filter(ecosystem_key),
network_filter=_key_to_network_filter(network_key),
provider_filter=_key_to_network_filter(provider_key),
)
)


def _network_filter_to_key(filter_: _NETWORK_FILTER) -> str:
if filter_ is None:
return "__none__"

elif isinstance(filter_, list):
return ",".join(filter_)

return filter_


def _key_to_network_filter(key: str) -> _NETWORK_FILTER:
if key == "__none__":
return None

elif "," in key:
return [n.strip() for n in key.split(",")]

return key


class NetworkChoice(click.Choice):
"""
A ``click.Choice`` to provide network choice defaults for the active project.
Expand All @@ -256,17 +304,12 @@ class NetworkChoice(click.Choice):
def __init__(
self,
case_sensitive=True,
ecosystem: Optional[Union[List[str], str]] = None,
network: Optional[Union[List[str], str]] = None,
provider: Optional[Union[List[str], str]] = None,
ecosystem: _NETWORK_FILTER = None,
network: _NETWORK_FILTER = None,
provider: _NETWORK_FILTER = None,
):
super().__init__(
LazySequence(
networks.get_network_choices(
ecosystem_filter=ecosystem, network_filter=network, provider_filter=provider
)
),
case_sensitive,
get_networks(ecosystem=ecosystem, network=network, provider=provider), case_sensitive
)

def get_metavar(self, param):
Expand Down
12 changes: 11 additions & 1 deletion src/ape/types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,9 +448,19 @@ def __len__(self):

return len(self.cache)

def __iter__(self) -> Any:
yield from self.cache
for value in self.generator:
yield value
self.cache.append(value)

@property
def generator(self) -> Iterator:
return self.generator() if callable(self.generator) else self._generator
if callable(self._generator):
self._generator = self._generator()

assert isinstance(self._generator, Iterator) # For type-checking.
yield from self._generator


__all__ = [
Expand Down
30 changes: 6 additions & 24 deletions src/ape_run/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
import click
from click import Command, Context, Option

from ape import project
from ape.cli import NetworkBoundCommand, network_option, verbosity_option
from ape.cli.options import _VERBOSITY_VALUES, _create_verbosity_kwargs
from ape.exceptions import ApeException, handle_ape_exception
from ape.logging import logger
from ape.managers.project import ProjectManager
from ape.utils import get_relative_path, use_temp_sys_path
from ape_console._cli import console

Expand Down Expand Up @@ -88,7 +88,7 @@ def invoke(self, ctx: Context) -> Any:
raise

def _get_command(self, filepath: Path) -> Union[click.Command, click.Group, None]:
relative_filepath = get_relative_path(filepath, self._project.path)
relative_filepath = get_relative_path(filepath, project.path)

# First load the code module by compiling it
# NOTE: This does not execute the module
Expand Down Expand Up @@ -167,28 +167,12 @@ def call(network):

return call

@property
def _project(self) -> ProjectManager:
"""
A class representing the project that is active at runtime.
(This is the same object as from ``from ape import project``).
Returns:
:class:`~ape.managers.project.ProjectManager`
"""

from ape import project

project.config_manager.load()

return project

@property
def commands(self) -> Dict[str, Union[click.Command, click.Group]]:
if not self._project.scripts_folder.is_dir():
if not project.scripts_folder.is_dir():
return {}

return self._get_cli_commands(self._project.scripts_folder)
return self._get_cli_commands(project.scripts_folder)

def _get_cli_commands(self, base_path: Path) -> Dict:
commands: Dict[str, Command] = {}
Expand Down Expand Up @@ -234,9 +218,7 @@ def result_callback(self, result, interactive):

def _launch_console(self):
trace = inspect.trace()
trace_frames = [
x for x in trace if x.filename.startswith(str(self._project.scripts_folder))
]
trace_frames = [x for x in trace if x.filename.startswith(str(project.scripts_folder))]
if not trace_frames:
# Error from Ape internals; avoid launching console.
sys.exit(1)
Expand All @@ -257,7 +239,7 @@ def _launch_console(self):
if frame:
del frame

return console(project=self._project, extra_locals=extra_locals, embed=True)
return console(project=project, extra_locals=extra_locals, embed=True)


@click.command(
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/cli/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def test_run(ape_cli, runner, project):

scripts = [s for s in project.scripts_folder.glob("*.py") if not s.name.startswith("error")]
for script_file in scripts:
result = runner.invoke(ape_cli, ["run", script_file.stem])
result = runner.invoke(ape_cli, ["run", script_file.stem], catch_exceptions=False)
assert (
result.exit_code == 0
), f"Unexpected exit code for '{script_file.name}'\n{result.output}"
Expand Down

0 comments on commit 82d3b31

Please sign in to comment.