"""Filters. The filters are used in the override mechanisms. Those filters perform checks between the values they hold and the values that are part of the context ( always ensure the context you provide is consistent.) Example: overrides: variant_name: user_ids: [id1, id2] vendor_ids: [7123] In this case, we will compare in the `user_ids` filter, the value in the context to the values provided (`id1`, `id2`), and in the `vendor_ids` filter, the value in the context to the values provided (`7123`). """ from features.constants import context as context_constants from features.constants import yaml as yaml_constants class FilterNotFoundException(Exception): """FilterNotFoundException.""" pass def get_from_context(filter_name): """Get a filter by its name. Args: filter_name (str): name of the filter. Raises: FilterNotFound: if the filter name does not exist. Return: callable: the filter. """ yaml_filter_name = _context_to_yaml_map.get(filter_name) return get(yaml_filter_name) def get(filter_name): """Get a filter by its name. Args: filter_name (str): name of the filter. Raises: FilterNotFound: if the filter name does not exist. Return: callable: the filter. """ filter_method = _yaml_to_filter_map.get(filter_name) if not filter_method: raise FilterNotFoundException( 'The filter {} does not exist'.format(filter_name) ) return filter_method def filter_user_id(values, context): """Filter for user ids. Args: values (list): the list of all the values to compare the user id against. context (dict): the context. Return: boolean: if the user id is in the values. """ return context.get(context_constants.USER_ID) in values def filter_vendor_id(values, context): """Filter for vendor ids. Args: values (list): the list of all the values to compare the vendor id against. context (dict): the context. Return: boolean: if the vendor id is in the values """ return context.get(context_constants.VENDOR_ID) in values def filter_profile_id(values, context): """Filter for profile ids - really {profile_type}:{profile_id}. Args: values (list): the list of all the values to compare {profile_type}:{profile_id} against. context (dict): the context. Return: boolean: if the profile_type:profile_id is in the values """ return context.get(context_constants.PROFILE_TYPE_AND_ID) in values def filter_identity_id(values, context): """Filter for identiy ids. Args: values (list): the list of all the values to compare identity_id against. context (dict): the context. Return: boolean: if the vendor id is in the values """ return context.get(context_constants.IDENTITY_ID) in values _yaml_to_filter_map = { yaml_constants.USER_ID: filter_user_id, yaml_constants.VENDOR_ID: filter_vendor_id, yaml_constants.PROFILE_ID: filter_profile_id, yaml_constants.IDENTITY_ID: filter_identity_id, } _context_to_yaml_map = { context_constants.USER_ID: yaml_constants.USER_ID, context_constants.VENDOR_ID: yaml_constants.VENDOR_ID, context_constants.PROFILE_TYPE_AND_ID: yaml_constants.PROFILE_ID, context_constants.IDENTITY_ID: yaml_constants.IDENTITY_ID, }