def filter_by(lookup_dict,
              lookup,
              traverse,
              merge=None,
              default='default',
              base=None):
    '''
    Common code to filter data structures like grains and pillar
    '''
    ret = None
    # Default value would be an empty list if lookup not found
    val = traverse_dict_and_list(traverse, lookup, [])

    # Iterate over the list of values to match against patterns in the
    # lookup_dict keys
    for each in val if isinstance(val, list) else [val]:
        for key in lookup_dict:
            test_key = key if isinstance(key, six.string_types) \
                else six.text_type(key)
            test_each = each if isinstance(each, six.string_types) \
                else six.text_type(each)
            if fnmatch.fnmatchcase(test_each, test_key):
                ret = lookup_dict[key]
                break
        if ret is not None:
            break

    if ret is None:
        ret = lookup_dict.get(default, None)

    if base and base in lookup_dict:
        base_values = lookup_dict[base]
        if ret is None:
            ret = base_values

        elif isinstance(base_values, Mapping):
            if not isinstance(ret, Mapping):
                raise SaltException(
                    'filter_by default and look-up values must both be '
                    'dictionaries.')
            ret = salt.utils.dictupdate.update(copy.deepcopy(base_values), ret)

    if merge:
        if not isinstance(merge, Mapping):
            raise SaltException(
                'filter_by merge argument must be a dictionary.')

        if ret is None:
            ret = merge
        else:
            salt.utils.dictupdate.update(ret, copy.deepcopy(merge))

    return ret
def filter_by ( lookup_dict , lookup , traverse , merge = None , default = 'default' , base = None ) : ret = None val = traverse_dict_and_list ( traverse , lookup , [ ] ) for each in val if isinstance ( val , list ) else [ val ] : for key in lookup_dict : test_key = key if isinstance ( key , six . string_types ) else six . text_type ( key ) test_each = each if isinstance ( each , six . string_types ) else six . text_type ( each ) if fnmatch . fnmatchcase ( test_each , test_key ) : ret = lookup_dict [ key ] break if ret is not None : break if ret is None : ret = lookup_dict . get ( default , None ) if base and base in lookup_dict : base_values = lookup_dict [ base ] if ret is None : ret = base_values elif isinstance ( base_values , Mapping ) : if not isinstance ( ret , Mapping ) : raise SaltException ( 'filter_by default and look-up values must both be ' 'dictionaries.' ) ret = salt . utils . dictupdate . update ( copy . deepcopy ( base_values ) , ret ) if merge : if not isinstance ( merge , Mapping ) : raise SaltException ( 'filter_by merge argument must be a dictionary.' ) if ret is None : ret = merge else : salt . utils . dictupdate . update ( ret , copy . deepcopy ( merge ) ) return ret
Common code to filter data structures like grains and pillar