mirror of
https://github.com/xonsh/xonsh.git
synced 2025-03-04 08:24:40 +01:00
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
parent
9c1a751fee
commit
ed11f319fa
4 changed files with 97 additions and 16 deletions
|
@ -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>
|
|
@ -61,6 +61,7 @@ from xonsh.tools import (
|
|||
is_string,
|
||||
is_string_or_callable,
|
||||
is_string_seq,
|
||||
is_tok_color_dict,
|
||||
is_writable_file,
|
||||
logfile_opt_to_str,
|
||||
path_to_str,
|
||||
|
@ -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
|
||||
|
|
|
@ -69,10 +69,10 @@ from xonsh.tools import (
|
|||
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,
|
||||
|
@ -97,7 +97,7 @@ from xonsh.tools import (
|
|||
to_logfile_opt,
|
||||
to_repr_pretty_,
|
||||
to_shlvl,
|
||||
to_str_str_dict,
|
||||
to_tok_color_dict,
|
||||
)
|
||||
|
||||
events.doc(
|
||||
|
@ -1151,8 +1151,8 @@ The file should contain a function with the signature
|
|||
"``!()`` 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"
|
||||
|
@ -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)",
|
||||
|
|
|
@ -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):
|
||||
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue