From 989a0de129845b7b3e5e52192585c6374657f91e Mon Sep 17 00:00:00 2001 From: Juliya Smith Date: Thu, 4 Apr 2024 09:03:35 -0500 Subject: [PATCH 1/2] feat: hide home dir --- src/ape/managers/project/manager.py | 6 +++--- src/ape/utils/os.py | 11 +++++++++-- src/ape_console/plugin.py | 9 +++++++++ tests/functional/test_project.py | 7 +++++++ tests/functional/utils/test_os.py | 15 +++++++++++++++ 5 files changed, 43 insertions(+), 5 deletions(-) diff --git a/src/ape/managers/project/manager.py b/src/ape/managers/project/manager.py index 2e383e2897..a4ee60a29d 100644 --- a/src/ape/managers/project/manager.py +++ b/src/ape/managers/project/manager.py @@ -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): @@ -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="") def __repr__(self) -> str: - path = f" {self.path}" if self.path else "" + path = f" {clean_path(self.path)}" if self.path else "" return f"" @property diff --git a/src/ape/utils/os.py b/src/ape/utils/os.py index 0adc80e3bf..43ce1f5c44 100644 --- a/src/ape/utils/os.py +++ b/src/ape/utils/os.py @@ -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"{path.relative_to(home)}" + + return f"{path}" diff --git a/src/ape_console/plugin.py b/src/ape_console/plugin.py index 60a22fcefa..50487d125d 100644 --- a/src/ape_console/plugin.py +++ b/src/ape_console/plugin.py @@ -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 @@ -12,6 +14,7 @@ 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 @@ -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)) + ) diff --git a/tests/functional/test_project.py b/tests/functional/test_project.py index c782899860..9b8b5a3179 100644 --- a/tests/functional/test_project.py +++ b/tests/functional/test_project.py @@ -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"" + assert actual == expected diff --git a/tests/functional/utils/test_os.py b/tests/functional/utils/test_os.py index f4b145d6b7..1aeca6c98c 100644 --- a/tests/functional/utils/test_os.py +++ b/tests/functional/utils/test_os.py @@ -3,6 +3,7 @@ import pytest from ape.utils.os import ( + clean_path, create_tempdir, get_all_files_in_directory, get_full_extension, @@ -117,3 +118,17 @@ 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) + assert actual == name + + +def test_clean_path_not_relative_to_home(): + name = "__canary_ape_test__" + path = Path(name) + actual = clean_path(path) + assert actual == name From 7b498c1512f9da57273b4a31655e40c9a0349646 Mon Sep 17 00:00:00 2001 From: Juliya Smith Date: Mon, 6 May 2024 19:47:10 -0500 Subject: [PATCH 2/2] fix: env var prefix --- src/ape/utils/os.py | 2 +- tests/functional/utils/test_os.py | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/ape/utils/os.py b/src/ape/utils/os.py index 43ce1f5c44..91d2ce18f5 100644 --- a/src/ape/utils/os.py +++ b/src/ape/utils/os.py @@ -218,6 +218,6 @@ def run_in_tempdir( def clean_path(path: Path) -> str: home = Path.home() if path.is_relative_to(home): - return f"{path.relative_to(home)}" + return f"$HOME{os.path.sep}{path.relative_to(home)}" return f"{path}" diff --git a/tests/functional/utils/test_os.py b/tests/functional/utils/test_os.py index 1aeca6c98c..1bd4031737 100644 --- a/tests/functional/utils/test_os.py +++ b/tests/functional/utils/test_os.py @@ -124,7 +124,8 @@ def test_clean_path_relative_to_home(): name = "__canary_ape_test__" path = Path.home() / name actual = clean_path(path) - assert actual == name + expected = f"$HOME/{name}" + assert actual == expected def test_clean_path_not_relative_to_home():