Skip to content

Commit

Permalink
Fix: Allows user to assign #XONSH_STYLE_OVERRIDES a {Token:str} dicti…
Browse files Browse the repository at this point in the history
…onary xonsh#4375 (xonsh#4973)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
IAustin-YangI and pre-commit-ci[bot] committed Oct 24, 2022
1 parent 9c1a751 commit ed11f31
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
**Added:**

* tests for methods changed in tools.py (is_tok_color_dict)

**Changed:**

* is_str_str_dict changed to check for Token:style dict

**Deprecated:**

* <news item>

**Removed:**

* <news item>

**Fixed:**

* $XONSH_STYLE_OVERRIDES cannot be assigned dict of {Token: str} #4375

**Security:**

* <news item>
36 changes: 36 additions & 0 deletions tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
is_string,
is_string_or_callable,
is_string_seq,
is_tok_color_dict,
is_writable_file,
logfile_opt_to_str,
path_to_str,
Expand Down Expand Up @@ -2000,3 +2001,38 @@ def test_is_regex_true():

def test_is_regex_false():
assert not is_regex("**")


from xonsh.style_tools import Token


@pytest.mark.parametrize(
"val, exp",
[
(
{
Token.Literal.String: "bold ansigreen",
"Token.Name.Tag": "underline ansiblue",
},
True,
),
(
{
Token.Literal.String: "bold ansigreen",
1: "bold ansigreen",
},
False,
),
({1: "bold ansigreen"}, False),
(
{Token.Literal.String: "123"},
False,
),
(
{"Token.Name.Tag": 123},
False,
),
],
)
def test_is_tok_color_dict(val, exp):
assert is_tok_color_dict(val) == exp
12 changes: 6 additions & 6 deletions xonsh/environ.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@
is_nonstring_seq_of_strings,
is_path,
is_regex,
is_str_str_dict,
is_string,
is_string_or_callable,
is_string_set,
is_tok_color_dict,
is_valid_shlvl,
logfile_opt_to_str,
path_to_str,
Expand All @@ -97,7 +97,7 @@
to_logfile_opt,
to_repr_pretty_,
to_shlvl,
to_str_str_dict,
to_tok_color_dict,
)

events.doc(
Expand Down Expand Up @@ -1151,8 +1151,8 @@ class GeneralSetting(Xettings):
"``!()`` and ``![]`` operators.",
)
XONSH_STYLE_OVERRIDES = Var(
is_str_str_dict,
to_str_str_dict,
is_tok_color_dict,
to_tok_color_dict,
dict_to_str,
{},
"A dictionary containing custom prompt_toolkit/pygments style definitions.\n"
Expand Down Expand Up @@ -1625,8 +1625,8 @@ class PTKSetting(PromptSetting): # sub-classing -> sub-group
"colors. Default is an empty string which means that prompt toolkit decide.",
)
PTK_STYLE_OVERRIDES = Var(
is_str_str_dict,
to_str_str_dict,
is_tok_color_dict,
to_tok_color_dict,
dict_to_str,
{},
"A dictionary containing custom prompt_toolkit style definitions. (deprecated)",
Expand Down
42 changes: 32 additions & 10 deletions xonsh/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1767,11 +1767,33 @@ def to_completion_mode(x):
return y


def is_str_str_dict(x):
"""Tests if something is a str:str dictionary"""
return isinstance(x, dict) and all(
isinstance(k, str) and isinstance(v, str) for k, v in x.items()
)
def is_tok_color_dict(x):
from pygments.token import _TokenType, string_to_tokentype

from xonsh.ptk_shell.shell import _style_from_pygments_dict

"""Tests if something is a Token:Style dictionary"""
if not isinstance(x, dict):
return False
"""Check if is a Token:str dict"""
for k, v in x.items():
if not isinstance(v, str):
return False
try:
k = _TokenType(k)
string_to_tokentype(k)
except (TypeError, AttributeError):
msg = f'"{k}" is not a valid Token.'
warnings.warn(msg, RuntimeWarning)
return False
"""Check each str is a valid style"""
try:
_style_from_pygments_dict(x)
except (AssertionError, ValueError):
msg = f'"{x}" contains an invalid style.'
warnings.warn(msg, RuntimeWarning)
return False
return True


def to_dict(x):
Expand All @@ -1787,13 +1809,13 @@ def to_dict(x):
return x


def to_str_str_dict(x):
"""Converts a string to str:str dictionary"""
if is_str_str_dict(x):
def to_tok_color_dict(x):
"""Converts a string to Token:str dictionary"""
if is_tok_color_dict(x):
return x
x = to_dict(x)
if not is_str_str_dict(x):
msg = f'"{x}" can not be converted to str:str dictionary.'
if not is_tok_color_dict(x):
msg = f'"{x}" can not be converted to Token:str dictionary.'
warnings.warn(msg, RuntimeWarning)
x = dict()
return x
Expand Down

0 comments on commit ed11f31

Please sign in to comment.