1
1
mirror of https://github.com/chubin/cheat.sh.git synced 2024-11-25 06:22:14 +03:00

Add support for 'action' queries

This commit is contained in:
Igor Chubin 2021-04-25 12:19:02 +00:00
parent 48e2626c3e
commit 26348029c8
3 changed files with 29 additions and 4 deletions

View File

@ -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')

View File

@ -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)

View File

@ -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