Skip to content

Commit

Permalink
Escape unsupported unicode emojis for legacy windows console
Browse files Browse the repository at this point in the history
Signed-off-by: Wei-Chun, Chang <[email protected]>
  • Loading branch information
wcchang1115 committed Oct 20, 2023
1 parent 80655ee commit 3e4496b
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions piperider_cli/validator.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import io
import platform
from abc import ABCMeta, abstractmethod
from typing import List

from rich.console import Console
from rich.console import Console, _STD_STREAMS
from rich.markup import escape

from piperider_cli.assertion_engine import AssertionEngine, ValidationResult
Expand Down Expand Up @@ -34,6 +36,22 @@ def __init__(self, dbt_profile=None, dbt_target=None):
'target': dbt_target
}

def _escape_console_msg(self, msg: str) -> str:
# escape unsupported unicode emojis for legacy windows console (ref: rich/console.py)
use_legacy_windows_render = False
if platform.system() == "Windows" and self.console.legacy_windows:
try:
use_legacy_windows_render = (

Check warning on line 44 in piperider_cli/validator.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/validator.py#L41-L44

Added lines #L41 - L44 were not covered by tests
self.console.file.fileno() in _STD_STREAMS
)
except (ValueError, io.UnsupportedOperation):
pass

Check warning on line 48 in piperider_cli/validator.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/validator.py#L47-L48

Added lines #L47 - L48 were not covered by tests

if use_legacy_windows_render:
return msg.encode(encoding='cp1252', errors='ignore').decode(encoding='cp1252')

Check warning on line 51 in piperider_cli/validator.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/validator.py#L50-L51

Added lines #L50 - L51 were not covered by tests

return msg

Check warning on line 53 in piperider_cli/validator.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/validator.py#L53

Added line #L53 was not covered by tests

def set_checker(self, name: str, checker: AbstractChecker):
self.checker_chain.append({'name': name, 'cls': checker()})

Expand All @@ -55,14 +73,14 @@ def execute(self):
error_msg = ', '.join(str(e) for e in error_msg)
elif isinstance(error_msg, PipeRiderError):
hint = error_msg.hint
self.console.print(CONSOLE_MSG_FAIL)
self.console.print(self._escape_console_msg(CONSOLE_MSG_FAIL))

Check warning on line 76 in piperider_cli/validator.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/validator.py#L76

Added line #L76 was not covered by tests
self.console.print(f"[bold red]Error:[/bold red] {checker['cls'].__class__.__name__}: {error_msg}")
if hint:
self.console.print(f'[bold yellow]Hint[/bold yellow]:\n {escape(hint)}')
return False
self.console.print(CONSOLE_MSG_PASS)
self.console.print(self._escape_console_msg(CONSOLE_MSG_PASS))

Check warning on line 81 in piperider_cli/validator.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/validator.py#L81

Added line #L81 was not covered by tests

self.console.print(CONSOLE_MSG_ALL_SET)
self.console.print(self._escape_console_msg(CONSOLE_MSG_ALL_SET))

Check warning on line 83 in piperider_cli/validator.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/validator.py#L83

Added line #L83 was not covered by tests
return True


Expand Down

0 comments on commit 3e4496b

Please sign in to comment.