Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored text functions out of util module #2969

Merged
merged 1 commit into from
Nov 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/molecule/command/idempotence.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

from molecule import logger, util
from molecule.command import base
from molecule.text import strip_ansi_escape

LOG = logger.get_logger(__name__)

Expand Down Expand Up @@ -119,7 +120,7 @@ def _non_idempotent_tasks(self, output):
output = re.sub(r"\n\s*\n*", "\n", output)

# Remove ansi escape sequences.
output = util.strip_ansi_escape(output)
output = strip_ansi_escape(output)

# Split the output into a list and go through it.
output_lines = output.split("\n")
Expand Down
3 changes: 2 additions & 1 deletion lib/molecule/test/functional/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from molecule import logger, util
from molecule.config import ansible_version
from molecule.test.conftest import change_dir_to
from molecule.text import strip_ansi_color
from molecule.util import run_command

LOG = logger.get_logger(__name__)
Expand Down Expand Up @@ -184,7 +185,7 @@ def list(x):
def list_with_format_plain(x):
cmd = ["molecule", "list", "--format", "plain"]
result = util.run_command(cmd)
out = util.strip_ansi_color(result.stdout)
out = strip_ansi_color(result.stdout)

for l in x.splitlines():
assert l in out
Expand Down
5 changes: 3 additions & 2 deletions lib/molecule/test/unit/test_scenarios.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@

import pytest

from molecule import config, scenario, scenarios, util
from molecule import config, scenario, scenarios
from molecule.console import console
from molecule.text import chomp, strip_ansi_escape


@pytest.fixture
Expand Down Expand Up @@ -77,7 +78,7 @@ def test_all_filters_on_scenario_name_property(_instance):
def test_print_matrix(capsys, _instance):
with console.capture() as capture:
_instance.print_matrix()
result = util.chomp(util.strip_ansi_escape(capture.get()))
result = chomp(strip_ansi_escape(capture.get()))

matrix_out = u"""---
default:
Expand Down
13 changes: 13 additions & 0 deletions lib/molecule/test/unit/test_text.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from molecule.text import strip_ansi_color, strip_ansi_escape


def test_strip_ansi_escape():
string = "ls\r\n\x1b[00m\x1b[01;31mfoo\x1b[00m\r\n\x1b[01;31m"

assert "ls\r\nfoo\r\n" == strip_ansi_escape(string)


def test_strip_ansi_color():
s = "foo\x1b[0m\x1b[0m\x1b[0m\n\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m"

assert "foo\n" == strip_ansi_color(s)
17 changes: 3 additions & 14 deletions lib/molecule/test/unit/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from molecule import util
from molecule.console import console
from molecule.constants import MOLECULE_HEADER
from molecule.text import strip_ansi_escape


def test_print_debug():
Expand All @@ -37,7 +38,7 @@ def test_print_debug():
with console.capture() as capture:
util.print_debug("test_title", "test_data")

result = util.strip_ansi_escape(capture.get())
result = strip_ansi_escape(capture.get())
assert result == expected


Expand All @@ -64,7 +65,7 @@ def test_print_environment_vars(capsys):

with console.capture() as capture:
util.print_environment_vars(env)
result = util.strip_ansi_escape(capture.get())
result = strip_ansi_escape(capture.get())
assert result == expected


Expand Down Expand Up @@ -229,18 +230,6 @@ def test_instance_with_scenario_name():
assert "foo-bar" == util.instance_with_scenario_name("foo", "bar")


def test_strip_ansi_escape():
string = "ls\r\n\x1b[00m\x1b[01;31mfoo\x1b[00m\r\n\x1b[01;31m"

assert "ls\r\nfoo\r\n" == util.strip_ansi_escape(string)


def test_strip_ansi_color():
s = "foo\x1b[0m\x1b[0m\x1b[0m\n\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m"

assert "foo\n" == util.strip_ansi_color(s)


def test_verbose_flag():
options = {"verbose": True, "v": True}

Expand Down
30 changes: 30 additions & 0 deletions lib/molecule/text.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""Text utils."""
import re


def chomp(text: str) -> str:
"""Remove any training spaces from string."""
return "\n".join([x.rstrip() for x in text.splitlines()])


def strip_ansi_escape(data):
"""Remove all ANSI escapes from string or bytes.

If bytes is passed instead of string, it will be converted to string
using UTF-8.
"""
if isinstance(data, bytes):
data = data.decode("utf-8")

return re.sub(r"\x1b[^m]*m", "", data)


def strip_ansi_color(data):
"""Remove ANSI colors from string or bytes."""
if isinstance(data, bytes):
data = data.decode("utf-8")

# Taken from tabulate
invisible_codes = re.compile(r"\x1b\[\d*m")

return re.sub(invisible_codes, "", data)
28 changes: 0 additions & 28 deletions lib/molecule/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,29 +251,6 @@ def instance_with_scenario_name(instance_name, scenario_name):
return "{}-{}".format(instance_name, scenario_name)


def strip_ansi_escape(data):
"""Remove all ANSI escapes from string or bytes.

If bytes is passed instead of string, it will be converted to string
using UTF-8.
"""
if isinstance(data, bytes):
data = data.decode("utf-8")

return re.sub(r"\x1b[^m]*m", "", data)


def strip_ansi_color(data):
"""Remove ANSI colors from string or bytes."""
if isinstance(data, bytes):
data = data.decode("utf-8")

# Taken from tabulate
invisible_codes = re.compile(r"\x1b\[\d*m")

return re.sub(invisible_codes, "", data)


def verbose_flag(options):
"""Return computed verbosity flag."""
verbose = "v"
Expand Down Expand Up @@ -440,8 +417,3 @@ def print_as_yaml(data: Any) -> None:
"""Render python object as yaml on console."""
result = Syntax(safe_dump(data), "yaml")
console.print(result)


def chomp(text: str) -> str:
"""Remove any training spaces from string."""
return "\n".join([x.rstrip() for x in text.splitlines()])