本文整理汇总了Python中salt.utils.odict.OrderedDict.replace方法的典型用法代码示例。如果您正苦于以下问题:Python OrderedDict.replace方法的具体用法?Python OrderedDict.replace怎么用?Python OrderedDict.replace使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类salt.utils.odict.OrderedDict
的用法示例。
在下文中一共展示了OrderedDict.replace方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: format_resolve
# 需要导入模块: from salt.utils.odict import OrderedDict [as 别名]
# 或者: from salt.utils.odict.OrderedDict import replace [as 别名]
def format_resolve(value,
original_dict=None,
global_tries=50,
this_call=0, topdb=False):
"""Resolve a dict of formatted strings, mappings & list to a valued dict
Please also read the associated test::
{"a": ["{b}", "{c}", "{e}"],
"b": 1,
"c": "{d}",
"d": "{b}",
"e": "{d}",
}
====>
{"a": ["1", "1", "{e}"],
"b": 1,
"c": "{d}",
"d": "{b}",
"e": "{d}",
}
"""
if not original_dict:
original_dict = OrderedDict()
if this_call == 0 and not original_dict and isinstance(value, dict):
original_dict = value
left = False
cycle = True
if isinstance(value, dict):
new = OrderedDict()
for key, val in value.items():
val = format_resolve(val, original_dict, this_call=this_call + 1, topdb=topdb)
new[key] = val
elif isinstance(value, (list, tuple)):
new = type(value)()
for v in value:
val = format_resolve(v, original_dict, this_call=this_call + 1, topdb=topdb)
new = new + type(value)([val])
elif isinstance(value, basestring):
new = value
if '/downloads' in new:
topdb= True
# do not directly call format to handle keyerror in original mapping
# where we may have yet keyerrors
if isinstance(original_dict, dict):
for k in original_dict:
reprk = k
if not isinstance(reprk, basestring):
reprk = '{0}'.format(k)
subst = '{' + reprk + '}'
subst_val = original_dict[k]
if subst in new:
if isinstance(subst_val, (list, dict)):
inner_new = format_resolve(
subst_val, original_dict, this_call=this_call + 1, topdb=topdb)
# composed, we take the repr
if new != subst:
new = new.replace(subst, str(inner_new))
# no composed value, take the original list
else:
new = inner_new
else:
if new != subst_val:
new = new.replace(subst,
str(subst_val))
if ('{' in new) and ('}' in new):
i = 0
while True:
try:
this_call += 1
if this_call > 1000:
raise _CycleError('cycle')
new_val = format_resolve(
new, original_dict, this_call=this_call + 1, topdb=topdb)
new_braces = new.count('{'), new.count('}')
newval_braces = new_val.count('{'), new_val.count('}')
if new_braces == newval_braces:
break
else:
new = new_val
except _CycleError:
cycle = True
break
if ('{' in new) and ('}' in new):
left = True
else:
new = value
if left:
if this_call == 0:
for i in global_tries:
new_val = format_resolve(
new, original_dict, this_call=this_call + 1, topdb=topdb)
if (new == new_val) or cycle:
break
else:
new = new_val
else:
while not cycle:
#.........这里部分代码省略.........