# Path: ac_mediator/exceptions.py
# class ACFieldTranslateException(ACException):
#     pass
# 
# class ACException(Exception):
#     msg = None
#     status = None
# 
#     def __init__(self, msg=None, status=None):
# 
#         # Set defaults (if not set in subclass)
#         if self.msg is None:
#             self.msg = ""
#         if self.status is None:
#             self.msg = -1
# 
#         # Override with arguments (if any)
#         if msg is not None:
#             self.msg = msg
#         if status is not None:
#             self.status = status
# 
#     def __repr__(self):
#         return self.msg
# 
# class ACFilterParsingException(ACException):
#     pass
# 
# Path: services/acservice/utils.py
# def parse_filter(filter_string):
#     """
#     Parse a filter string using the 'filterExpr' grammar defined above.
#     This function returns a pyparsing.ParseResults object that should be further
#     processed with the build_filter_string method of a ACServiceTextSearchMixin instance.
#     """
#     return filterExpr.parseString(filter_string, parseAll=True)


from ac_mediator.exceptions import ACFieldTranslateException, ACException, ACFilterParsingException
from services.acservice.constants import *
from services.acservice.utils import parse_filter
import pyparsing
        These are the filters defined with the decorator @translates_filter_for_field
        :return: list of available AudioCommons field names (fields equivalent to the filters)
        """
        return list(self.direct_filters_mapping.keys()) + list(self.translate_filter_methods_registry.keys())

    def translate_filter(self, ac_field_name, value):
        """
        Given an Audio Commons field name and a value for a filter (e.g. "ac:duration" and "3.5"), this method
        returns the corresponding field name and values that can be understood by a third party service. Following from
        the previous example, this method could return something like ("duration", 3500) if the field for "ac:duration"
        is named "duration" in the third party service and durations are specified in milliseconds instead of seconds.
        To perform this translation first we check if the field is available in
        self.direct_filters_mapping. If that is the case, this function simply returns the corresponding
        (key, value) tuple with the key being changed to the one specified in self.direct_filters_mapping.
        If field name is not available in self.direct_filters_mapping, then we check if it is available
        in the registry of translate filters for field methods self.translate_filter_methods_registry which is built
        when running self.conf_textsearch() (see ACServiceTextSearchMixin.conf_textsearch(self, *args).
        If a method for the ac_field_name exists in self.translate_filter_methods_registry we call it and
        return its response.
        If field does not exist in self.direct_filters_mapping or self.translate_filter_methods_registry
        we raise an exception to inform that filter for field could not be translated.
        :param ac_field_name: Audio Commons name of the field to filter
        :param value: value for the filter
        :return: (field_name, value) tuple with field_name and value translated to the third party service domain
        """
        try:
            if ac_field_name in self.direct_filters_mapping:
                return self.direct_filters_mapping[ac_field_name], value  # Do direct mapping
            return self.translate_filter_methods_registry[ac_field_name](value)  # Invoke translate method
        except KeyError:

# Path: ac_mediator/exceptions.py
# class ACFieldTranslateException(ACException):
#     pass
# 
# class ACException(Exception):
#     msg = None
#     status = None
# 
#     def __init__(self, msg=None, status=None):
# 
#         # Set defaults (if not set in subclass)
#         if self.msg is None:
#             self.msg = ""
#         if self.status is None:
#             self.msg = -1
# 
#         # Override with arguments (if any)
#         if msg is not None:
#             self.msg = msg
#         if status is not None:
#             self.status = status
# 
#     def __repr__(self):
#         return self.msg
# 
# class ACFilterParsingException(ACException):
#     pass
# 
# Path: services/acservice/utils.py
# def parse_filter(filter_string):
#     """
#     Parse a filter string using the 'filterExpr' grammar defined above.
#     This function returns a pyparsing.ParseResults object that should be further
#     processed with the build_filter_string method of a ACServiceTextSearchMixin instance.
#     """
#     return filterExpr.parseString(filter_string, parseAll=True)


# Path: services/acservice/search.py
from ac_mediator.exceptions import ACFieldTranslateException, ACException, ACFilterParsingException
from services.acservice.constants import *
from services.acservice.utils import parse_filter
import pyparsing

        These are the filters defined with the decorator @translates_filter_for_field
        :return: list of available AudioCommons field names (fields equivalent to the filters)
        """
        return list(self.direct_filters_mapping.keys()) + list(self.translate_filter_methods_registry.keys())

    def translate_filter(self, ac_field_name, value):
        """
        Given an Audio Commons field name and a value for a filter (e.g. "ac:duration" and "3.5"), this method
        returns the corresponding field name and values that can be understood by a third party service. Following from
        the previous example, this method could return something like ("duration", 3500) if the field for "ac:duration"
        is named "duration" in the third party service and durations are specified in milliseconds instead of seconds.
        To perform this translation first we check if the field is available in
        self.direct_filters_mapping. If that is the case, this function simply returns the corresponding
        (key, value) tuple with the key being changed to the one specified in self.direct_filters_mapping.
        If field name is not available in self.direct_filters_mapping, then we check if it is available
        in the registry of translate filters for field methods self.translate_filter_methods_registry which is built
        when running self.conf_textsearch() (see ACServiceTextSearchMixin.conf_textsearch(self, *args).
        If a method for the ac_field_name exists in self.translate_filter_methods_registry we call it and
        return its response.
        If field does not exist in self.direct_filters_mapping or self.translate_filter_methods_registry
        we raise an exception to inform that filter for field could not be translated.
        :param ac_field_name: Audio Commons name of the field to filter
        :param value: value for the filter
        :return: (field_name, value) tuple with field_name and value translated to the third party service domain
        """
        try:
            if ac_field_name in self.direct_filters_mapping:
                return self.direct_filters_mapping[ac_field_name], value  # Do direct mapping
            return self.translate_filter_methods_registry[ac_field_name](value)  # Invoke translate method
        except KeyError:

            raise ACFilterParsingException('Filter for field \'{0}\' not supported'.format(ac_field_name))