From 3dabd41b459f71c5189466c49a8be91056b7cac6 Mon Sep 17 00:00:00 2001 From: Juliya Smith Date: Tue, 13 Feb 2024 11:31:22 -0600 Subject: [PATCH] feat: hide home dir --- src/ape/managers/project/manager.py | 5 +++-- src/ape/utils/os.py | 4 ++++ src/ape_console/plugin.py | 9 +++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/ape/managers/project/manager.py b/src/ape/managers/project/manager.py index aa4f854189..d2f39754a7 100644 --- a/src/ape/managers/project/manager.py +++ b/src/ape/managers/project/manager.py @@ -17,6 +17,7 @@ from ape.managers.project.types import ApeProject, BrownieProject from ape.utils import get_relative_path from ape.utils.basemodel import _assert_not_ipython_check +from ape.utils.os import clean_path class ProjectManager(BaseManager): @@ -55,11 +56,11 @@ def __init__( self.path = self.path.parent def __str__(self) -> str: - return f'Project("{self.path}")' + return f'Project("{clean_path(self.path)}")' def __repr__(self): try: - path = f" {self.path}" + path = f" {clean_path(self.path)}" except Exception: # Disallow exceptions in __repr__ path = "" diff --git a/src/ape/utils/os.py b/src/ape/utils/os.py index 3a0b70a859..09568f0d92 100644 --- a/src/ape/utils/os.py +++ b/src/ape/utils/os.py @@ -139,3 +139,7 @@ def __exit__(self, *exc): for path in self.exclude: if path not in sys.path: sys.path.append(path) + + +def clean_path(path: Path) -> str: + return f"{path.relative_to(Path.home())}" 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)) + )