def and_(items, keys, values): ''' :param items: items which are to be searched for matching all values. :param keys: keys which to be match, key in values and items should be the same. :param values: values which to be match ''' result = [] # Note(hzrandd):check the values should search or not, # to deal with the sort_key(s) which is(are) error. # such as : 'hos'or 'hosts', it likes 'host', # so return all the items for user. tag = False for k, v in values.items(): if values[k] is not None: tag = True break if not tag: return items for item in items: matched = True for key in keys: # target is what user want target = values.get(key, None) if target is None: # No search for this key continue item_attr = utils.get_value_by_key(item, key) if item_attr is None: # item doesn't contain key LOG.warn(_("Item does not contain key %s.") % key) matched = False break elif isinstance(item_attr, list): # if get item_attr list from item, # compare each item_attr. # All not matched, the item is abandoned. one_match = False for each_value in item_attr: if target.lower() == each_value.lower(): one_match = True break if not one_match: matched = False break elif isinstance(item_attr, bool): if item_attr != (target.lower() in TRUE_STR_LIST): matched = False break elif target.lower() != item_attr.lower(): # if item_attr is not None and not list, # compare with target. matched = False break if matched: result.append(item) return result
def sort_key_for_ip(item): result = [] try: ips = utils.get_value_by_key(item, sort_key) if not ips: return result if not isinstance(ips, list): return utils.convert_IP_to_tuple(ips) for ip in ips: result.append(utils.convert_IP_to_tuple(ip)) except (AttributeError, TypeError, ValueError): LOG.exception("Error occurred when sort by IP.") raise exception.InvalidIP(message=_("IP which is sorting" " is not valid.")) return result
def or_(items, keys, value, tenantid_key, tenantid): ''' specify keys in items and search match any key and value. ''' LOG.debug(_("or_ for value %s and tenantid %s") % (value, tenantid)) if value is None and tenantid is None: return items results = [] for item in items: if tenantid is not None and tenantid in item.get(tenantid_key, None): # If tenantid is matched, add matched item # and no need for compare other keys. # Then continue to match other items. results.append(item) continue if value is None: # search only by tenant id continue for key in keys: item_attr = utils.get_value_by_key(item, key) if item_attr is None: LOG.debug(_("get value by key %s and return item_attr %s") % (key, item_attr)) elif isinstance(item_attr, list): # if get value list from item, # compare each value. # One matched. append the item. one_match = False for each_target in item_attr: if value.lower() == each_target.lower(): one_match = True break if one_match: results.append(item) break elif isinstance(item_attr, bool): if item_attr == (value.lower() in TRUE_STR_LIST): results.append(item) break elif value.lower() == item_attr.lower(): # if item_attr is not none and not list # compare value. results.append(item) break return results