mirror of
https://github.com/xonsh/xonsh.git
synced 2025-03-04 08:24:40 +01:00
single instance of string.Formatter
This commit is contained in:
parent
32ae582581
commit
f4654fd1fb
7 changed files with 57 additions and 21 deletions
|
@ -1,11 +1,23 @@
|
|||
**Added:** None
|
||||
**Added:**
|
||||
|
||||
**Changed:** None
|
||||
* <news item>
|
||||
|
||||
**Deprecated:** None
|
||||
**Changed:**
|
||||
|
||||
**Removed:** None
|
||||
* <news item>
|
||||
|
||||
**Fixed:** None
|
||||
**Deprecated:**
|
||||
|
||||
**Security:** None
|
||||
* <news item>
|
||||
|
||||
**Removed:**
|
||||
|
||||
* <news item>
|
||||
|
||||
**Fixed:**
|
||||
|
||||
* <news item>
|
||||
|
||||
**Security:**
|
||||
|
||||
* <news item>
|
||||
|
|
24
news/fstrenv.rst
Normal file
24
news/fstrenv.rst
Normal file
|
@ -0,0 +1,24 @@
|
|||
**Added:**
|
||||
|
||||
* <news item>
|
||||
|
||||
**Changed:**
|
||||
|
||||
* Now there is only a single instance of ``string.Formatter()`` in the
|
||||
code base, which is called ``xonsh.tools.FORMATTER``.
|
||||
|
||||
**Deprecated:**
|
||||
|
||||
* <news item>
|
||||
|
||||
**Removed:**
|
||||
|
||||
* <news item>
|
||||
|
||||
**Fixed:**
|
||||
|
||||
* <news item>
|
||||
|
||||
**Security:**
|
||||
|
||||
* <news item>
|
|
@ -14,6 +14,7 @@ from xonsh.color_tools import (
|
|||
rgb2short,
|
||||
rgb_to_256,
|
||||
)
|
||||
from xonsh.tools import FORMATTER
|
||||
|
||||
|
||||
def ansi_partial_color_format(template, style="default", cmap=None, hide=False):
|
||||
|
@ -59,7 +60,6 @@ def _ansi_partial_color_format_main(template, style="default", cmap=None, hide=F
|
|||
print(msg.format(style), file=sys.stderr)
|
||||
builtins.__xonsh__.env["XONSH_COLOR_STYLE"] = "default"
|
||||
cmap = ANSI_STYLES["default"]
|
||||
formatter = string.Formatter()
|
||||
esc = ("\001" if hide else "") + "\033["
|
||||
m = "m" + ("\002" if hide else "")
|
||||
bopen = "{"
|
||||
|
@ -67,7 +67,7 @@ def _ansi_partial_color_format_main(template, style="default", cmap=None, hide=F
|
|||
colon = ":"
|
||||
expl = "!"
|
||||
toks = []
|
||||
for literal, field, spec, conv in formatter.parse(template):
|
||||
for literal, field, spec, conv in FORMATTER.parse(template):
|
||||
toks.append(literal)
|
||||
if field is None:
|
||||
pass
|
||||
|
|
|
@ -54,7 +54,7 @@ class PromptFormatter:
|
|||
def _format_prompt(self, template=DEFAULT_PROMPT):
|
||||
template = template() if callable(template) else template
|
||||
toks = []
|
||||
for literal, field, spec, conv in _FORMATTER.parse(template):
|
||||
for literal, field, spec, conv in xt.FORMATTER.parse(template):
|
||||
toks.append(literal)
|
||||
entry = self._format_field(field, spec, conv)
|
||||
if entry is not None:
|
||||
|
@ -108,11 +108,6 @@ def PROMPT_FIELDS():
|
|||
)
|
||||
|
||||
|
||||
@xl.lazyobject
|
||||
def _FORMATTER():
|
||||
return string.Formatter()
|
||||
|
||||
|
||||
def default_prompt():
|
||||
"""Creates a new instance of the default prompt."""
|
||||
if xp.ON_CYGWIN or xp.ON_MSYS:
|
||||
|
@ -205,7 +200,7 @@ def is_template_string(template, PROMPT_FIELDS=None):
|
|||
"""Returns whether or not the string is a valid template."""
|
||||
template = template() if callable(template) else template
|
||||
try:
|
||||
included_names = set(i[1] for i in _FORMATTER.parse(template))
|
||||
included_names = set(i[1] for i in xt.FORMATTER.parse(template))
|
||||
except ValueError:
|
||||
return False
|
||||
included_names.discard(None)
|
||||
|
@ -227,9 +222,9 @@ def _format_value(val, spec, conv):
|
|||
"""
|
||||
if val is None:
|
||||
return ""
|
||||
val = _FORMATTER.convert_field(val, conv)
|
||||
val = xt.FORMATTER.convert_field(val, conv)
|
||||
if spec:
|
||||
val = _FORMATTER.format(spec, val)
|
||||
val = xt.FORMATTER.format(spec, val)
|
||||
if not isinstance(val, str):
|
||||
val = str(val)
|
||||
return val
|
||||
|
|
|
@ -36,6 +36,7 @@ from xonsh.tools import (
|
|||
ANSICOLOR_NAMES_MAP,
|
||||
PTK_NEW_OLD_COLOR_MAP,
|
||||
hardcode_colors_for_win10,
|
||||
FORMATTER,
|
||||
)
|
||||
|
||||
from xonsh.color_tools import (
|
||||
|
@ -344,7 +345,6 @@ def partial_color_tokenize(template):
|
|||
|
||||
|
||||
def _partial_color_tokenize_main(template, styles):
|
||||
formatter = string.Formatter()
|
||||
bopen = "{"
|
||||
bclose = "}"
|
||||
colon = ":"
|
||||
|
@ -353,7 +353,7 @@ def _partial_color_tokenize_main(template, styles):
|
|||
fg = bg = None
|
||||
value = ""
|
||||
toks = []
|
||||
for literal, field, spec, conv in formatter.parse(template):
|
||||
for literal, field, spec, conv in FORMATTER.parse(template):
|
||||
if field is None:
|
||||
value += literal
|
||||
elif field in KNOWN_COLORS or "#" in field:
|
||||
|
|
|
@ -6,6 +6,7 @@ from collections import defaultdict
|
|||
from xonsh.platform import HAS_PYGMENTS
|
||||
from xonsh.lazyasd import LazyObject
|
||||
from xonsh.color_tools import RE_BACKGROUND
|
||||
from xonsh.tools import FORMATTER
|
||||
|
||||
|
||||
class _TokenType(tuple):
|
||||
|
@ -81,7 +82,6 @@ def partial_color_tokenize(template):
|
|||
|
||||
|
||||
def _partial_color_tokenize_main(template, styles):
|
||||
formatter = string.Formatter()
|
||||
bopen = "{"
|
||||
bclose = "}"
|
||||
colon = ":"
|
||||
|
@ -90,7 +90,7 @@ def _partial_color_tokenize_main(template, styles):
|
|||
fg = bg = None
|
||||
value = ""
|
||||
toks = []
|
||||
for literal, field, spec, conv in formatter.parse(template):
|
||||
for literal, field, spec, conv in FORMATTER.parse(template):
|
||||
if field is None:
|
||||
value += literal
|
||||
elif field in KNOWN_COLORS or "#" in field:
|
||||
|
|
|
@ -37,6 +37,7 @@ import traceback
|
|||
import warnings
|
||||
import operator
|
||||
import ast
|
||||
import string
|
||||
|
||||
# adding imports from further xonsh modules is discouraged to avoid circular
|
||||
# dependencies
|
||||
|
@ -273,6 +274,10 @@ class EnvPath(cabc.MutableSequence):
|
|||
self._l.remove(data)
|
||||
self._l.insert(0 if front else len(self._l), data)
|
||||
|
||||
@lazyobject
|
||||
def FORMATTER():
|
||||
return string.Formatter()
|
||||
|
||||
|
||||
class DefaultNotGivenType(object):
|
||||
"""Singleton for representing when no default value is given."""
|
||||
|
|
Loading…
Add table
Reference in a new issue