mirror of
https://github.com/python/cpython.git
synced 2025-08-31 05:58:33 +00:00
Accepted Raymond's patch to combine mapping and keyword arguments, with slight
modification. Also, renamed the positional argument to '__mapping' to further reduce the chance of duplicate keyword arguments.
This commit is contained in:
parent
5011d0c683
commit
46b629c101
1 changed files with 32 additions and 5 deletions
|
@ -82,6 +82,23 @@ def maketrans(fromstr, tostr):
|
||||||
####################################################################
|
####################################################################
|
||||||
import re as _re
|
import re as _re
|
||||||
|
|
||||||
|
class _multimap:
|
||||||
|
"""Helper class for combining multiple mappings.
|
||||||
|
|
||||||
|
Used by .{safe_,}substitute() to combine the mapping and keyword
|
||||||
|
arguments.
|
||||||
|
"""
|
||||||
|
def __init__(self, primary, secondary):
|
||||||
|
self._primary = primary
|
||||||
|
self._secondary = secondary
|
||||||
|
|
||||||
|
def __getitem__(self, key):
|
||||||
|
try:
|
||||||
|
return self._primary[key]
|
||||||
|
except KeyError:
|
||||||
|
return self._secondary[key]
|
||||||
|
|
||||||
|
|
||||||
class _TemplateMetaclass(type):
|
class _TemplateMetaclass(type):
|
||||||
pattern = r"""
|
pattern = r"""
|
||||||
(?P<escaped>%(delim)s{2}) | # Escape sequence of two delimiters
|
(?P<escaped>%(delim)s{2}) | # Escape sequence of two delimiters
|
||||||
|
@ -126,19 +143,29 @@ class Template:
|
||||||
raise ValueError('Invalid placeholder in string: line %d, col %d' %
|
raise ValueError('Invalid placeholder in string: line %d, col %d' %
|
||||||
(lineno, colno))
|
(lineno, colno))
|
||||||
|
|
||||||
def substitute(self, mapping):
|
def substitute(self, __mapping=None, **kws):
|
||||||
|
if __mapping is None:
|
||||||
|
__mapping = kws
|
||||||
|
elif kws:
|
||||||
|
__mapping = _multimap(kws, __mapping)
|
||||||
|
# Helper function for .sub()
|
||||||
def convert(mo):
|
def convert(mo):
|
||||||
if mo.group('escaped') is not None:
|
if mo.group('escaped') is not None:
|
||||||
return '$'
|
return '$'
|
||||||
if mo.group('bogus') is not None:
|
if mo.group('bogus') is not None:
|
||||||
self._bogus(mo)
|
self._bogus(mo)
|
||||||
val = mapping[mo.group('named') or mo.group('braced')]
|
val = __mapping[mo.group('named') or mo.group('braced')]
|
||||||
# We use this idiom instead of str() because the latter will fail
|
# We use this idiom instead of str() because the latter will fail
|
||||||
# if val is a Unicode containing non-ASCII characters.
|
# if val is a Unicode containing non-ASCII characters.
|
||||||
return '%s' % val
|
return '%s' % val
|
||||||
return self.pattern.sub(convert, self.template)
|
return self.pattern.sub(convert, self.template)
|
||||||
|
|
||||||
def safe_substitute(self, mapping):
|
def safe_substitute(self, __mapping=None, **kws):
|
||||||
|
if __mapping is None:
|
||||||
|
__mapping = kws
|
||||||
|
elif kws:
|
||||||
|
__mapping = _multimap(kws, __mapping)
|
||||||
|
# Helper function for .sub()
|
||||||
def convert(mo):
|
def convert(mo):
|
||||||
if mo.group('escaped') is not None:
|
if mo.group('escaped') is not None:
|
||||||
return '$'
|
return '$'
|
||||||
|
@ -149,12 +176,12 @@ class Template:
|
||||||
try:
|
try:
|
||||||
# We use this idiom instead of str() because the latter
|
# We use this idiom instead of str() because the latter
|
||||||
# will fail if val is a Unicode containing non-ASCII
|
# will fail if val is a Unicode containing non-ASCII
|
||||||
return '%s' % mapping[named]
|
return '%s' % __mapping[named]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
return '$' + named
|
return '$' + named
|
||||||
braced = mo.group('braced')
|
braced = mo.group('braced')
|
||||||
try:
|
try:
|
||||||
return '%s' % mapping[braced]
|
return '%s' % __mapping[braced]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
return '${' + braced + '}'
|
return '${' + braced + '}'
|
||||||
return self.pattern.sub(convert, self.template)
|
return self.pattern.sub(convert, self.template)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue