Skip to content

Commit

Permalink
properly escape cli messages
Browse files Browse the repository at this point in the history
  • Loading branch information
tfeldmann committed Feb 4, 2024
1 parent 29bfee7 commit 54bf049
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
23 changes: 13 additions & 10 deletions organize/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@
from organize import Config, ConfigError
from organize.find_config import ConfigNotFound, find_config, list_configs
from organize.output import JSONL, Default, Output
from organize.utils import escape

from .__version__ import __is_prerelease__, __version__
from .__version__ import __version__

DOCS_RTD = "https://organize.readthedocs.io"
DOCS_GHPAGES = "https://tfeldmann.github.io/organize/"
Expand Down Expand Up @@ -117,14 +118,16 @@ def new(config: Optional[str]) -> None:
try:
config_path = find_config(config)
console.print(
f'Config "{config_path}" already exists.\n'
f'Config "{escape(config_path)}" already exists.\n'
r'Use "organize new \[name]" to create a config in the default location.'
)
except ConfigNotFound as e:
assert e.init_path is not None
e.init_path.parent.mkdir(parents=True, exist_ok=True)
e.init_path.write_text(EXAMPLE_CONFIG, encoding="utf-8")
console.print(f'Config "{e.init_path.stem}" created at "{e.init_path}"')
console.print(
f'Config "{escape(e.init_path.stem)}" created at "{escape(e.init_path)}"'
)


def edit(config: Optional[str]) -> None:
Expand All @@ -139,7 +142,7 @@ def edit(config: Optional[str]) -> None:
def check(config: Optional[str]) -> None:
config_path = find_config(config)
Config.from_path(config_path=config_path)
console.print(f'No problems found in "{config_path}".')
console.print(f'No problems found in "{escape(config_path)}".')


def debug(config: Optional[str]) -> None:
Expand All @@ -161,7 +164,7 @@ def show(config: Optional[str], path: bool, reveal: bool) -> None:
_open_uri(config_path.parent.as_uri())
else:
syntax = Syntax(config_path.read_text(encoding="utf-8"), "yaml")
console.print(syntax)
console.print(escape(syntax))


def list_() -> None:
Expand All @@ -174,8 +177,8 @@ def list_() -> None:


def docs() -> None:
uri = DOCS_GHPAGES if __is_prerelease__ else DOCS_RTD
print(f'Opening "{uri}"')
uri = DOCS_RTD
print(f'Opening "{escape(uri)}"')
_open_uri(uri=uri)


Expand Down Expand Up @@ -251,13 +254,13 @@ def cli() -> None:
elif args.docs:
docs()
except (ConfigError, ConfigNotFound) as e:
console.print(f"[red]Error: Config problem[/]\n{e}")
console.print(f"[red]Error: Config problem[/]\n{escape(e)}")
sys.exit(1)
except ValidationError as e:
console.print(f"[red]Error: Invalid CLI arguments[/]\n{e}")
console.print(f"[red]Error: Invalid CLI arguments[/]\n{escape(e)}")
sys.exit(2)
except ScannerError as e:
console.print(f"[red]Error: YAML syntax error[/]\n{e}")
console.print(f"[red]Error: YAML syntax error[/]\n{escape(e)}")
sys.exit(3)


Expand Down
11 changes: 5 additions & 6 deletions organize/output/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
from typing import TYPE_CHECKING, Dict, Optional

from rich.console import Console
from rich.markup import escape
from rich.panel import Panel
from rich.prompt import Confirm as RichConfirm
from rich.status import Status
from rich.theme import Theme

from organize.utils import ChangeDetector
from organize.utils import ChangeDetector, escape

from ._sender import sender_name
from .output import Level
Expand All @@ -24,8 +23,8 @@


def format_path(path: Path, base_style: str, main_style: str) -> str:
base = escape(str(f"{path.parent}/"))
main = escape(str(path.name))
base = escape(f"{path.parent}/")
main = escape(path.name)
return f"[{base_style}]{base}[/][{main_style}]{main}[/]"


Expand Down Expand Up @@ -141,9 +140,9 @@ def start(
self.console.print(Panel("SIMULATION", style="simulation"))

if working_dir.resolve() != Path(".").resolve():
self.console.print(f'Working dir: "{escape(str(working_dir))}"')
self.console.print(f'Working dir: "{escape(working_dir)}"')
if config_path:
self.console.print(f'Config: "{escape(str(config_path))}"')
self.console.print(f'Config: "{escape(config_path)}"')

status_verb = "simulating" if simulate else "organizing"
self.status.update(f"[status]{status_verb}[/]")
Expand Down
6 changes: 6 additions & 0 deletions organize/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@
from pathlib import Path
from typing import Any, Union

from rich.markup import escape as rich_escape

ENV_ORGANIZE_NORMALIZE_UNICODE = os.environ.get("ORGANIZE_NORMALIZE_UNICODE", "1")


def escape(msg: Any) -> str:
return rich_escape(str(msg))


def normalize_unicode(text: str, form: str = "NFC") -> str:
if ENV_ORGANIZE_NORMALIZE_UNICODE == "1":
return unicodedata.normalize(form, text)
Expand Down

0 comments on commit 54bf049

Please sign in to comment.