vault.py 文件源码

python
阅读 25 收藏 0 点赞 0 评论 0

项目:salt-vault 作者: carlpett 项目源码 文件源码
def _expand_pattern_lists(pattern, **mappings):
  '''
  Expands the pattern for any list-valued mappings, such that for any list of
  length N in the mappings present in the pattern, N copies of the pattern are
  returned, each with an element of the list substituted.

  pattern:
      A pattern to expand, for example 'by-role/{grains[roles]}'

  mappings:
      A dictionary of variables that can be expanded into the pattern.

  Example: Given the pattern 'by-role/{grains[roles]}' and the below grains

  .. code-block:: yaml

      grains:
          roles:
              - web
              - database

  This function will expand that into '[by-role/web, by-role/database]'.

  Note that this method does not expand any non-list patterns.
  '''
  expanded_patterns = []
  f = string.Formatter()
  for (_, field_name, _, _) in f.parse(pattern):
    if field_name is None:
      continue
    (value, _) = f.get_field(field_name, None, mappings)
    if isinstance(value, list):
      token = '{{{0}}}'.format(field_name)
      expanded = map(lambda x: pattern.replace(token, str(x)), value)
      for expanded_item in expanded:
        result = _expand_pattern_lists(expanded_item, **mappings)
        expanded_patterns += result
      return expanded_patterns
  return [pattern]
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号