Skip to content

Commit

Permalink
feat: hide home dir in project.path and all paths in ape console (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey committed May 14, 2024
1 parent f47a256 commit 88eda18
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/ape/managers/project/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from ape.managers.project.types import ApeProject, BrownieProject
from ape.utils import get_relative_path, log_instead_of_fail
from ape.utils.basemodel import _assert_not_ipython_check, only_raise_attribute_error
from ape.utils.os import get_full_extension
from ape.utils.os import clean_path, get_full_extension


class ProjectManager(BaseManager):
Expand Down Expand Up @@ -63,11 +63,11 @@ def __init__(
self.path = self.path.parent

def __str__(self) -> str:
return f'Project("{self.path}")'
return f'Project("{clean_path(self.path)}")'

@log_instead_of_fail(default="<ProjectManager>")
def __repr__(self) -> str:
path = f" {self.path}" if self.path else ""
path = f" {clean_path(self.path)}" if self.path else ""
return f"<ProjectManager{path}>"

@property
Expand Down
10 changes: 9 additions & 1 deletion src/ape/utils/os.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ def run_in_tempdir(
Args:
fn (Callable): A function that takes a path. It gets called
with the resolved path to the temporary directory.
name (str): Optionally name the temporary directory.
name (Optional[str]): Optionally name the temporary directory.
Returns:
Any: The result of the function call.
Expand Down Expand Up @@ -250,3 +250,11 @@ def path_match(path: Union[str, Path], *exclusions: str) -> bool:
return True

return False


def clean_path(path: Path) -> str:
home = Path.home()
if path.is_relative_to(home):
return f"$HOME{os.path.sep}{path.relative_to(home)}"

return f"{path}"
9 changes: 9 additions & 0 deletions src/ape_console/plugin.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import shlex
from pathlib import Path

import click
from click.testing import CliRunner
from eth_utils import is_hex
from IPython import get_ipython
from IPython.core.magic import Magics, line_magic, magics_class
from rich import print as rich_print

import ape
from ape._cli import cli
Expand All @@ -13,6 +15,7 @@
from ape.managers import ProjectManager
from ape.types import AddressType
from ape.utils import ManagerAccessMixin, cached_property
from ape.utils.os import clean_path


@magics_class
Expand Down Expand Up @@ -91,3 +94,9 @@ def custom_exception_handler(self, etype, value, tb, tb_offset=None):
def load_ipython_extension(ipython):
ipython.register_magics(ApeConsoleMagics)
ipython.set_custom_exc((ApeException,), custom_exception_handler)

# This prevents displaying a user's home directory
# ever when using `ape console`.
ipython.display_formatter.formatters["text/plain"].for_type(
Path, lambda x, *args, **kwargs: rich_print(clean_path(x))
)
7 changes: 7 additions & 0 deletions tests/functional/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -722,3 +722,10 @@ def test_add_compiler_data(project_with_dependency_config):
compiler_5 = Compiler(name="test456", version="9.0.0", contractTypes=["bar"])
with pytest.raises(ProjectError, match=r".*'bar' collision across compilers.*"):
proj.add_compiler_data([compiler_4, compiler_5])


def test_repr(project):
actual = repr(project)
# NOTE: tmp path is NOT relative to home.
expected = f"<ProjectManager {project.path}>"
assert actual == expected
16 changes: 16 additions & 0 deletions tests/functional/utils/test_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from ape.utils.misc import SOURCE_EXCLUDE_PATTERNS
from ape.utils.os import (
clean_path,
create_tempdir,
get_all_files_in_directory,
get_full_extension,
Expand Down Expand Up @@ -209,3 +210,18 @@ def test_path_match_recurse_dir(path):
"""
excl = "exclude_dir/**"
assert path_match(path, excl)


def test_clean_path_relative_to_home():
name = "__canary_ape_test__"
path = Path.home() / name
actual = clean_path(path)
expected = f"$HOME/{name}"
assert actual == expected


def test_clean_path_not_relative_to_home():
name = "__canary_ape_test__"
path = Path(name)
actual = clean_path(path)
assert actual == name

0 comments on commit 88eda18

Please sign in to comment.