Skip to content

Commit

Permalink
[MAINTENANCE] Refactor out termcolor dependency (#6348)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdkini authored Nov 14, 2022
1 parent 143a9ff commit 57cd559
Show file tree
Hide file tree
Showing 26 changed files with 328 additions and 352 deletions.
1 change: 0 additions & 1 deletion docs_rtd/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ sphinxcontrib-jsmath==1.0.1
sphinxcontrib-qthelp==1.0.3
sphinxcontrib-serializinghtml==1.1.4
sybil==1.4.0
termcolor==1.1.0
terminado==0.8.3
testpath==0.4.4
toml==0.10.1
Expand Down
40 changes: 19 additions & 21 deletions great_expectations/cli/pretty_printing.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
import re
import sys
from typing import List, Optional
from typing import List, Optional, Tuple

from termcolor import colored
import click
from typing_extensions import Final

SUPPORTED_CLI_COLORS: Final[Tuple[str, ...]] = (
"blue",
"cyan",
"green",
"yellow",
"red",
)


def cli_message(string: str) -> None:
print(cli_colorize_string(string))


def cli_colorize_string(string: str) -> str:
# the DOTALL flag means that `.` includes newlines for multiline comments inside these tags
flags = re.DOTALL
mod_string = re.sub(
"<blue>(.*?)</blue>", colored(r"\g<1>", "blue"), string, flags=flags
)
mod_string = re.sub(
"<cyan>(.*?)</cyan>", colored(r"\g<1>", "cyan"), mod_string, flags=flags
)
mod_string = re.sub(
"<green>(.*?)</green>", colored(r"\g<1>", "green"), mod_string, flags=flags
)
mod_string = re.sub(
"<yellow>(.*?)</yellow>", colored(r"\g<1>", "yellow"), mod_string, flags=flags
)
mod_string = re.sub(
"<red>(.*?)</red>", colored(r"\g<1>", "red"), mod_string, flags=flags
)

return colored(mod_string)
for color in SUPPORTED_CLI_COLORS:
string = re.sub(
f"<{color}>(.*?)</{color}>",
click.style(r"\g<1>", fg=color),
string,
flags=re.DOTALL, # the DOTALL flag means that `.` includes newlines for multiline comments inside these tags
)
return string


def display_not_implemented_message_and_exit() -> None:
Expand Down
46 changes: 20 additions & 26 deletions great_expectations/cli/v012/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,41 @@
import re
import sys
from types import ModuleType
from typing import Optional
from typing import Optional, Tuple

import click
import pkg_resources
from pkg_resources import Distribution, WorkingSet
from typing_extensions import Final

from great_expectations.cli.v012 import toolkit
from great_expectations.cli.v012.python_subprocess import (
execute_shell_command_with_progress_polling,
)
from great_expectations.util import import_library_module, is_library_loadable

try:
from termcolor import colored
except ImportError:
colored = None
SUPPORTED_CLI_COLORS: Final[Tuple[str, ...]] = (
"blue",
"cyan",
"green",
"yellow",
"red",
)


def cli_message(string) -> None:
def cli_message(string: str) -> None:
print(cli_colorize_string(string))


def cli_colorize_string(string):
# the DOTALL flag means that `.` includes newlines for multiline comments inside these tags
flags = re.DOTALL
mod_string = re.sub(
"<blue>(.*?)</blue>", colored(r"\g<1>", "blue"), string, flags=flags
)
mod_string = re.sub(
"<cyan>(.*?)</cyan>", colored(r"\g<1>", "cyan"), mod_string, flags=flags
)
mod_string = re.sub(
"<green>(.*?)</green>", colored(r"\g<1>", "green"), mod_string, flags=flags
)
mod_string = re.sub(
"<yellow>(.*?)</yellow>", colored(r"\g<1>", "yellow"), mod_string, flags=flags
)
mod_string = re.sub(
"<red>(.*?)</red>", colored(r"\g<1>", "red"), mod_string, flags=flags
)

return colored(mod_string)
def cli_colorize_string(string: str) -> str:
for color in SUPPORTED_CLI_COLORS:
string = re.sub(
f"<{color}>(.*?)</{color}>",
click.style(r"\g<1>", fg=color),
string,
flags=re.DOTALL, # the DOTALL flag means that `.` includes newlines for multiline comments inside these tags
)
return string


def cli_message_list(string_list, list_intro_string=None) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ class GEDependencies:
"requests",
"ruamel.yaml",
"scipy",
"termcolor",
"tqdm",
"typing-extensions",
"urllib3",
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ pytz>=2021.3
requests>=2.20
ruamel.yaml>=0.16,<0.17.18
scipy>=0.19.0
termcolor>=1.1.0,<2.1.0 # Temporary pin due to breaking change made in 2.1.0
tqdm>=4.59.0
typing-extensions>=3.10.0.0 # Leverage type annotations from recent Python releases
tzlocal>=1.2
Expand Down
14 changes: 7 additions & 7 deletions tests/cli/test_datasource_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from great_expectations import DataContext
from great_expectations.cli import cli
from tests.cli.utils import assert_no_logging_messages_or_tracebacks
from tests.cli.utils import assert_no_logging_messages_or_tracebacks, escape_ansi


@mock.patch(
Expand Down Expand Up @@ -75,13 +75,13 @@ def test_cli_datasource_list_on_project_with_one_datasource(
catch_exceptions=False,
)

expected_output = """Using v3 (Batch Request) API\x1b[0m
1 Datasource found:[0m
[0m
- [36mname:[0m my_datasource[0m
[36mclass_name:[0m Datasource[0m
expected_output = """Using v3 (Batch Request) API
1 Datasource found:
- name: my_datasource
class_name: Datasource
""".strip()
stdout = result.stdout.strip()
stdout = escape_ansi(result.stdout).strip()
assert stdout == expected_output
assert_no_logging_messages_or_tracebacks(caplog, result)

Expand Down
14 changes: 7 additions & 7 deletions tests/cli/test_datasource_sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from great_expectations import DataContext
from great_expectations.cli import cli
from tests.cli.utils import assert_no_logging_messages_or_tracebacks
from tests.cli.utils import assert_no_logging_messages_or_tracebacks, escape_ansi


@mock.patch(
Expand Down Expand Up @@ -46,13 +46,13 @@ def test_cli_datasource_list(
catch_exceptions=False,
)
expected_output = """\
Using v3 (Batch Request) API\x1b[0m
1 Datasource found:[0m
[0m
- [36mname:[0m wow_a_datasource[0m
[36mclass_name:[0m SqlAlchemyDatasource[0m
Using v3 (Batch Request) API
1 Datasource found:
- name: wow_a_datasource
class_name: SqlAlchemyDatasource
""".strip()
stdout = result.stdout.strip()
stdout = escape_ansi(result.stdout).strip()

assert stdout == expected_output

Expand Down
35 changes: 18 additions & 17 deletions tests/cli/upgrade_helpers/test_upgrade_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from tests.cli.utils import (
VALIDATION_OPERATORS_DEPRECATION_MESSAGE,
assert_no_logging_messages_or_tracebacks,
escape_ansi,
)


Expand Down Expand Up @@ -119,7 +120,7 @@ def test_upgrade_helper_intervention_on_cli_command(
input="n\n",
catch_exceptions=False,
)
stdout: str = result.stdout
stdout: str = escape_ansi(result.stdout).strip()

assert (
"Your project appears to have an out-of-date config version (1.0) - the version number must be at least 3."
Expand All @@ -131,12 +132,12 @@ def test_upgrade_helper_intervention_on_cli_command(
in stdout
)
assert (
"Ok, exiting now. To upgrade at a later time, use the following command: [36mgreat_expectations project "
"upgrade[0m" in stdout
"Ok, exiting now. To upgrade at a later time, use the following command: great_expectations project "
"upgrade" in stdout
)
assert (
"To learn more about the upgrade process, visit ["
"36mhttps://docs.greatexpectations.io/docs/guides/miscellaneous/migration_guide#migrating-to-the-batch-request-v3-api"
"To learn more about the upgrade process, visit "
"https://docs.greatexpectations.io/docs/guides/miscellaneous/migration_guide#migrating-to-the-batch-request-v3-api"
in stdout
)
assert_no_logging_messages_or_tracebacks(
Expand Down Expand Up @@ -197,15 +198,15 @@ def test_basic_project_upgrade(v10_project_directory, caplog):
input="\n",
catch_exceptions=False,
)
stdout: str = result.stdout
stdout: str = escape_ansi(result.stdout).strip()

with open(
file_relative_path(
__file__,
"../../test_fixtures/upgrade_helper/test_basic_project_upgrade_expected_stdout.fixture",
)
) as f:
expected_stdout: str = f.read()
expected_stdout: str = f.read().strip()
expected_stdout = expected_stdout.replace(
"GE_PROJECT_DIR", v10_project_directory
)
Expand Down Expand Up @@ -303,15 +304,15 @@ def test_project_upgrade_with_manual_steps(
input="\n",
catch_exceptions=False,
)
stdout: str = result.stdout
stdout: str = escape_ansi(result.stdout).strip()

with open(
file_relative_path(
__file__,
"../../test_fixtures/upgrade_helper/test_project_upgrade_with_manual_steps_expected_stdout.fixture",
)
) as f:
expected_stdout: str = f.read()
expected_stdout: str = f.read().strip()
expected_stdout = expected_stdout.replace(
"GE_PROJECT_DIR", v10_project_directory
)
Expand Down Expand Up @@ -414,15 +415,15 @@ def test_project_upgrade_with_exception(v10_project_directory, caplog):
input="\n",
catch_exceptions=False,
)
stdout: str = result.stdout
stdout: str = escape_ansi(result.stdout).strip()

with open(
file_relative_path(
__file__,
"../../test_fixtures/upgrade_helper/test_project_upgrade_with_exception_expected_stdout.fixture",
)
) as f:
expected_stdout: str = f.read()
expected_stdout: str = f.read().strip()
expected_stdout = expected_stdout.replace(
"GE_PROJECT_DIR", v10_project_directory
)
Expand Down Expand Up @@ -511,15 +512,15 @@ def test_v2_to_v3_project_upgrade_with_all_manual_steps_checkpoints_datasources_
input="\n",
catch_exceptions=False,
)
stdout: str = result.stdout
stdout: str = escape_ansi(result.stdout).strip()

with open(
file_relative_path(
__file__,
"../../test_fixtures/upgrade_helper/test_v2_to_v3_project_upgrade_with_manual_steps_checkpoints_datasources_validation_operators_expected_stdout.fixture",
)
) as f:
expected_stdout: str = f.read()
expected_stdout: str = f.read().strip()
expected_stdout = expected_stdout.replace(
"GE_PROJECT_DIR", v20_project_directory
)
Expand Down Expand Up @@ -618,15 +619,15 @@ def test_v2_to_v3_project_upgrade_with_manual_steps_checkpoints(
input="\n",
catch_exceptions=False,
)
stdout: str = result.stdout
stdout: str = escape_ansi(result.stdout).strip()

with open(
file_relative_path(
__file__,
"../../test_fixtures/upgrade_helper/test_v2_to_v3_project_upgrade_with_manual_steps_checkpoints.fixture",
)
) as f:
expected_stdout: str = f.read()
expected_stdout: str = f.read().strip()
expected_stdout = expected_stdout.replace(
"GE_PROJECT_DIR",
v20_project_directory_with_v30_configuration_and_v20_checkpoints,
Expand Down Expand Up @@ -732,15 +733,15 @@ def test_v2_to_v3_project_upgrade_without_manual_steps(
input="\n",
catch_exceptions=False,
)
stdout: str = result.stdout
stdout: str = escape_ansi(result.stdout).strip()

with open(
file_relative_path(
__file__,
"../../test_fixtures/upgrade_helper/test_v2_to_v3_project_upgrade_without_manual_steps_expected_stdout.fixture",
)
) as f:
expected_stdout: str = f.read()
expected_stdout: str = f.read().strip()
expected_stdout = expected_stdout.replace(
"GE_PROJECT_DIR",
v20_project_directory_with_v30_configuration_and_no_checkpoints,
Expand Down
7 changes: 7 additions & 0 deletions tests/cli/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
import traceback

from _pytest.logging import LogCaptureFixture
Expand Down Expand Up @@ -128,3 +129,9 @@ def assert_no_tracebacks(click_result):
except ValueError as ve:
# sometimes stderr is not captured separately
pass


def escape_ansi(line: str) -> str:
# https://stackoverflow.com/a/38662876
ansi_escape = re.compile(r"(?:\x1B[@-_]|[\x80-\x9F])[0-?]*[ -/]*[@-~]")
return ansi_escape.sub("", line)
Loading

0 comments on commit 57cd559

Please sign in to comment.