Example #1
0
def parse_query(query):
    text = ""
    if isinstance(query, Query):
        text = query.text
        q = unicode(text).strip() 
    else:
        text = query
        q = unicode(text).strip()
    try:
        parsetree = splparse(q)
    except:
        logger.exception("Failed to parse query: " + text)
        return None
    return parsetree
Example #2
0
def parse_query(query):
    """Parse the given query if possible.

    This function is a small wrapper for splparser's parse function that 
    handles unicode issues and logs errors and returns None in the case of
    an error rather than raising an exception.

    :param query: The query to parse
    :type query: str
    :rtype: splparser.parsetree.ParseTreeNode
    """
    text = ""
    if isinstance(query, Query):
        q = str(query.text.encode("utf8")) if type(query.text) == unicode else str(query.text.decode("utf8").encode("utf8"))
    else:
        q = str(query.encode("utf8")) if type(query) == unicode else str(query.decode("utf8").encode("utf8"))
    try:
        parsetree = splparse(q)
    except:
        logger.exception("Failed to parse query: " + q)
        return None
    return parsetree