Skip to content

Commit

Permalink
Moved more text functions to text module (#2983)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea authored Nov 18, 2020
1 parent a94af5f commit e4b2bc8
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 49 deletions.
6 changes: 3 additions & 3 deletions src/molecule/command/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from click_help_colors import HelpColorsCommand, HelpColorsGroup

import molecule.scenarios
from molecule import config, logger, util
from molecule import config, logger, text, util
from molecule.console import should_do_markup

LOG = logger.get_logger(__name__)
Expand All @@ -46,7 +46,7 @@ def wrapper(*args, **kwargs):
LOG.info(
"[info]Running [scenario]%s[/] > [action]%s[/][/]",
self._config.scenario.name,
util.underscore(self.__class__.__name__),
text.underscore(self.__class__.__name__),
extra={"markup": True},
)
rt = func(*args, **kwargs)
Expand Down Expand Up @@ -150,7 +150,7 @@ def execute_cmdline_scenarios(scenario_name, args, command_args, ansible_args=()
def execute_subcommand(config, subcommand):
"""Execute subcommand."""
command_module = getattr(molecule.command, subcommand)
command = getattr(command_module, util.camelize(subcommand))
command = getattr(command_module, text.camelize(subcommand))
# knowledge of the current action is used by some provisioners
# to ensure they behave correctly during certain sequence steps,
# particulary the setting of ansible options in create/destroy,
Expand Down
4 changes: 2 additions & 2 deletions src/molecule/command/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from rich.syntax import Syntax
from rich.table import Table

from molecule import logger, scenarios, util
from molecule import logger, scenarios, text, util
from molecule.command import base
from molecule.console import console
from molecule.status import Status
Expand Down Expand Up @@ -113,7 +113,7 @@ def list(ctx, scenario_name, format): # pragma: no cover
for scenario in s:
statuses.extend(base.execute_subcommand(scenario.config, subcommand))

headers = [util.title(name) for name in Status._fields]
headers = [text.title(name) for name in Status._fields]
if format == "simple" or format == "plain":
table_format = format # "simple"

Expand Down
31 changes: 27 additions & 4 deletions src/molecule/test/unit/test_text.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
from molecule.text import strip_ansi_color, strip_ansi_escape
from molecule.text import (
camelize,
strip_ansi_color,
strip_ansi_escape,
title,
underscore,
)


def test_camelize():
assert "Foo" == camelize("foo")
assert "FooBar" == camelize("foo_bar")
assert "FooBarBaz" == camelize("foo_bar_baz")


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)


def test_strip_ansi_escape():
Expand All @@ -7,7 +25,12 @@ def test_strip_ansi_escape():
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"
def test_title():
assert "Foo" == title("foo")
assert "Foo Bar" == title("foo_bar")

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

def test_underscore():
assert "foo" == underscore("Foo")
assert "foo_bar" == underscore("FooBar")
assert "foo_bar_baz" == underscore("FooBarBaz")
17 changes: 0 additions & 17 deletions src/molecule/test/unit/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,6 @@ def test_filter_verbose_permutation():
assert x == util.filter_verbose_permutation(options)


def test_title():
assert "Foo" == util.title("foo")
assert "Foo Bar" == util.title("foo_bar")


def test_abs_path(temp_dir):
x = os.path.abspath(os.path.join(os.getcwd(), os.path.pardir, "foo", "bar"))

Expand All @@ -280,18 +275,6 @@ def test_abs_path_with_none_path():
assert util.abs_path(None) is None


def test_camelize():
assert "Foo" == util.camelize("foo")
assert "FooBar" == util.camelize("foo_bar")
assert "FooBarBaz" == util.camelize("foo_bar_baz")


def test_underscore():
assert "foo" == util.underscore("Foo")
assert "foo_bar" == util.underscore("FooBar")
assert "foo_bar_baz" == util.underscore("FooBarBaz")


@pytest.mark.parametrize(
"a,b,x",
[
Expand Down
23 changes: 23 additions & 0 deletions src/molecule/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
import re


def camelize(string):
"""Format string as camel-case."""
# NOTE(retr0h): Taken from jpvanhal/inflection
# https://github.com/jpvanhal/inflection
return re.sub(r"(?:^|_)(.)", lambda m: m.group(1).upper(), string)


def chomp(text: str) -> str:
"""Remove any training spaces from string."""
return "\n".join([x.rstrip() for x in text.splitlines()])
Expand All @@ -28,3 +35,19 @@ def strip_ansi_color(data):
invisible_codes = re.compile(r"\x1b\[\d*m")

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


def underscore(string):
"""Format string to underlined notation."""
# NOTE(retr0h): Taken from jpvanhal/inflection
# https://github.com/jpvanhal/inflection
string = re.sub(r"([A-Z]+)([A-Z][a-z])", r"\1_\2", string)
string = re.sub(r"([a-z\d])([A-Z])", r"\1_\2", string)
string = string.replace("-", "_")

return string.lower()


def title(word: str) -> str:
"""Format title."""
return " ".join(x.capitalize() or "_" for x in word.split("_"))
23 changes: 0 additions & 23 deletions src/molecule/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,35 +272,12 @@ def filter_verbose_permutation(options):
return {k: options[k] for k in options if not re.match("^[v]+$", k)}


def title(word):
"""Format title."""
return " ".join(x.capitalize() or "_" for x in word.split("_"))


def abs_path(path):
"""Return absolute path."""
if path:
return os.path.abspath(path)


def camelize(string):
"""Format string as camel-case."""
# NOTE(retr0h): Taken from jpvanhal/inflection
# https://github.com/jpvanhal/inflection
return re.sub(r"(?:^|_)(.)", lambda m: m.group(1).upper(), string)


def underscore(string):
"""Format string to underlined notation."""
# NOTE(retr0h): Taken from jpvanhal/inflection
# https://github.com/jpvanhal/inflection
string = re.sub(r"([A-Z]+)([A-Z][a-z])", r"\1_\2", string)
string = re.sub(r"([a-z\d])([A-Z])", r"\1_\2", string)
string = string.replace("-", "_")

return string.lower()


def merge_dicts(a, b):
"""
Merge the values of b into a and returns a new dict.
Expand Down

0 comments on commit e4b2bc8

Please sign in to comment.