import re

SOLR_QUERY_OPS_ESCAPE_RE = re.compile(r"[*?{}\[\]:\\]")


def solr_escape_op(match):
    return "\\" + match.group()


class UniversalQueryInputProcessor(object):
    def process(self, user_input):
        # remove/escape:
        #  - the wildcards: ?, * (for performance reasons)
        # escape:
        #  - escape character: \ (obviously necessary)
        #  - field specification: :
        #  - range and subquery: {, }, [, ]
        return SOLR_QUERY_OPS_ESCAPE_RE.sub(solr_escape_op, user_input)
