mirror of https://github.com/python/cpython
Factor-out common code for helper classes.
This commit is contained in:
parent
16fe75e4e0
commit
c9423109f5
|
@ -633,7 +633,7 @@ class Counter(dict):
|
||||||
|
|
||||||
|
|
||||||
########################################################################
|
########################################################################
|
||||||
### ChainMap (helper for configparser)
|
### ChainMap (helper for configparser and string.Template)
|
||||||
########################################################################
|
########################################################################
|
||||||
|
|
||||||
class _ChainMap(MutableMapping):
|
class _ChainMap(MutableMapping):
|
||||||
|
|
|
@ -46,23 +46,7 @@ def capwords(s, sep=None):
|
||||||
|
|
||||||
####################################################################
|
####################################################################
|
||||||
import re as _re
|
import re as _re
|
||||||
|
from collections import _ChainMap
|
||||||
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"""
|
||||||
|
@ -116,7 +100,7 @@ class Template(metaclass=_TemplateMetaclass):
|
||||||
if not args:
|
if not args:
|
||||||
mapping = kws
|
mapping = kws
|
||||||
elif kws:
|
elif kws:
|
||||||
mapping = _multimap(kws, args[0])
|
mapping = _ChainMap(kws, args[0])
|
||||||
else:
|
else:
|
||||||
mapping = args[0]
|
mapping = args[0]
|
||||||
# Helper function for .sub()
|
# Helper function for .sub()
|
||||||
|
@ -142,7 +126,7 @@ class Template(metaclass=_TemplateMetaclass):
|
||||||
if not args:
|
if not args:
|
||||||
mapping = kws
|
mapping = kws
|
||||||
elif kws:
|
elif kws:
|
||||||
mapping = _multimap(kws, args[0])
|
mapping = _ChainMap(kws, args[0])
|
||||||
else:
|
else:
|
||||||
mapping = args[0]
|
mapping = args[0]
|
||||||
# Helper function for .sub()
|
# Helper function for .sub()
|
||||||
|
|
Loading…
Reference in New Issue