def eval_filter()

in build/update_instance_types.py [0:0]


def eval_filter(ec2_filter, instances):
    value, condition, key = ec2_filter
    matched_instances = {}
    for instance_type, instance in instances.items():
        instance_key = instance.copy()
        no_key = False
        for k in key.split('.'):
            if k not in instance_key:
                no_key = True
                break
            instance_key = instance_key[k]
        actual = json.loads(json.dumps(instance_key))
        if isinstance(actual, str):
            actual = f'"{actual}"'
        if isinstance(value, (bool, int, list)):
            rendered_value = str(value)
        elif value.isdigit() or value in ["True", "False"]:
            rendered_value = value
        else:
            rendered_value = f'"""{value}"""'
        stmnt = f'{rendered_value} {condition} {actual}'
        if no_key:
            t = instance['instance_type'] if 'instance_type' in instance else instance.get('InstanceType', instance)
            # print(f"dropping {t} because it has no key matching {key}")
        elif eval(stmnt, {}, {}) is True:
            matched_instances[instance_type] = instance
        # elif instance_type not in matched_instances:
            # print(f'{instance_type} does not match `{rendered_value} {condition} {actual}`')
    return matched_instances