From 26348029c8591ba46eae37cf85fdb94530b3c448 Mon Sep 17 00:00:00 2001 From: Igor Chubin Date: Sun, 25 Apr 2021 12:19:02 +0000 Subject: [PATCH] Add support for 'action' queries --- lib/adapter/question.py | 7 ++++--- lib/cheat_wrapper.py | 17 ++++++++++++++++- lib/routing.py | 9 +++++++++ 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/lib/adapter/question.py b/lib/adapter/question.py index 56bf9ab..f94e46e 100644 --- a/lib/adapter/question.py +++ b/lib/adapter/question.py @@ -39,7 +39,7 @@ If the problem persists, file a GitHub issue at github.com/chubin/cheat.sh or ping @igor_chubin """ -NOT_AUTHORIZED_MESSAGE = """403 UNAUTHORIZED" +NOT_AUTHORIZED_MESSAGE = """403 UNAUTHORIZED""" class Question(UpstreamAdapter): @@ -113,13 +113,14 @@ class Question(UpstreamAdapter): else: topic = [topic] - if request_option.get("action") == "suggest": - if request_option.get("authorized"): + if request_options.get("action") == "suggest": + if request_options.get("authorized"): topic = ["--action", "suggest"] + topic else: return NOT_AUTHORIZED_MESSAGE cmd = [CONFIG["path.internal.bin.upstream"]] + topic + print(cmd) proc = Popen(cmd, stdin=open(os.devnull, "r"), stdout=PIPE, stderr=PIPE) answer = proc.communicate()[0].decode('utf-8') diff --git a/lib/cheat_wrapper.py b/lib/cheat_wrapper.py index 5a5fa62..1fae3f1 100644 --- a/lib/cheat_wrapper.py +++ b/lib/cheat_wrapper.py @@ -94,6 +94,21 @@ def cheat_wrapper(query, request_options=None, output_format='ansi'): # query = _strip_hyperlink(query.rstrip('/')) topic, keyword, search_options = _parse_query(query) + # Process special case when 'action' is specified. + # In this case the response must be returned as is, + # without any changes and postprocessing + # + # 'action' is specified for suggestion queries, link queries etc. + if request_options.get("action"): + answers = get_answers(topic, request_options=request_options) + if answers: + answer = answers[0] + if isinstance(answer, dict): + answer = answer.get("answer", "") + return answer, True + else: + return "", False + if keyword: answers = find_answers_by_keyword( topic, keyword, options=search_options, request_options=request_options) @@ -116,5 +131,5 @@ def cheat_wrapper(query, request_options=None, output_format='ansi'): answer_data['topics_list'] = get_topics_list() return frontend.html.visualize(answer_data, request_options) elif output_format == 'json': - return json.dumps(answer_data, indent=4) + return json.dumps(answer_data, indent=4), True return frontend.ansi.visualize(answer_data, request_options) diff --git a/lib/routing.py b/lib/routing.py index e45002e..2b9a2fe 100644 --- a/lib/routing.py +++ b/lib/routing.py @@ -188,6 +188,15 @@ class Router(object): if topic_type and topic_type in topic_types: topic_types = [topic_type] + + # Process action-queries directly, without caching and postprocessing + if request_options.get("action"): + answers = [] + for topic_type in topic_types: + answers.append( + self._get_page_dict(topic, topic_type, request_options=request_options)) + return answers + # 'question' queries are pretty expensive, that's why they should be handled # in a special way: # we do not drop the old style cache entries and try to reuse them if possible