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 7, 2024
1 parent eb9c0e6 commit 08cea7b
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 5 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
11 changes: 9 additions & 2 deletions src/ape/utils/os.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,18 @@ 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 (Optional[str]): Optionally provide a name for the temporary
directory.
name (Optional[str]): Optionally name the temporary directory.
Returns:
Any: The result of the function call.
"""
with create_tempdir(name=name) as temp_dir:
return fn(temp_dir)


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,17 +1,20 @@
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
from ape.exceptions import Abort, ApeException, handle_ape_exception
from ape.logging import logger
from ape.types import AddressType
from ape.utils import cached_property
from ape.utils.os import clean_path


@magics_class
Expand Down Expand Up @@ -82,3 +85,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 @@ -680,3 +680,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 @@ -3,6 +3,7 @@
import pytest

from ape.utils.os import (
clean_path,
create_tempdir,
get_all_files_in_directory,
get_full_extension,
Expand Down Expand Up @@ -117,3 +118,18 @@ def fn(p):
assert arguments[0].resolve() == arguments[0]
if name is not None:
assert arguments[0].name == name


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 08cea7b

Please sign in to comment.