示例#1
0
def cheat_wrapper(query, request_options=None, html=False):
    """
    Giant megafunction that delivers cheat sheet for `query`.
    If `html` is True, the answer is formatted as HTML.
    Additional request options specified in `request_options`.

    This function is really really bad, and should be rewritten
    as soon as possible.
    """
    def _strip_hyperlink(query):
        return re.sub('(,[0-9]+)+$', '', query)

    def _parse_query(query):
        topic = query
        keyword = None
        search_options = ""

        keyword = None
        if '~' in query:
            topic = query
            pos = topic.index('~')
            keyword = topic[pos + 1:]
            topic = topic[:pos]

            if '/' in keyword:
                search_options = keyword[::-1]
                search_options = search_options[:search_options.index('/')]
                keyword = keyword[:-len(search_options) - 1]

        return topic, keyword, search_options

    query = _sanitize_query(query)

    # at the moment, we just remove trailing slashes
    # so queries python/ and python are equal
    query = _strip_hyperlink(query.rstrip('/'))
    topic, keyword, search_options = _parse_query(query)

    if keyword:
        answers = find_answer_by_keyword(topic,
                                         keyword,
                                         options=search_options,
                                         request_options=request_options)
    else:
        answers = [(topic,
                    get_answer(topic, keyword,
                               request_options=request_options))]

    return _visualize(query, keyword, answers, request_options, html=html)
示例#2
0
def cheat_wrapper(query, request_options=None, html=False):
    """
    Giant megafunction that delivers cheat sheet for `query`.
    If `html` is True, the answer is formatted as HTML.
    Additional request options specified in `request_options`.

    This function is really really bad, and should be rewritten
    as soon as possible.
    """

    def _strip_hyperlink(query):
        return re.sub('(,[0-9]+)+$', '', query)

    def _parse_query(query):
        topic = query
        keyword = None
        search_options = ""

        keyword = None
        if '~' in query:
            topic = query
            pos = topic.index('~')
            keyword = topic[pos+1:]
            topic = topic[:pos]

            if '/' in keyword:
                search_options = keyword[::-1]
                search_options = search_options[:search_options.index('/')]
                keyword = keyword[:-len(search_options)-1]

        return topic, keyword, search_options

    query = _sanitize_query(query)

    # at the moment, we just remove trailing slashes
    # so queries python/ and python are equal
    query = _strip_hyperlink(query.rstrip('/'))
    topic, keyword, search_options = _parse_query(query)

    if keyword:
        answers = find_answer_by_keyword(
            topic, keyword, options=search_options, request_options=request_options)
    else:
        answers = [(topic, get_answer(topic, keyword, request_options=request_options))]

    return _visualize(query, keyword, answers, request_options, html=html)