""" Configuration parameters: path.internal.bin.upstream """ # pylint: disable=relative-import from __future__ import print_function import os import re from subprocess import Popen, PIPE from config import CONFIG from languages_data import SO_NAME from .upstream import UpstreamAdapter NOT_FOUND_MESSAGE = """404 NOT FOUND Unknown cheat sheet. Please try to reformulate your query. Query format: /LANG/QUESTION Examples: /python/read+json /golang/run+external+program /js/regex+search See /:help for more info. If the problem persists, file a GitHub issue at github.com/chubin/cheat.sh or ping @igor_chubin """ class Question(UpstreamAdapter): """ Answer to a programming language question, using Stackoverflow as the main data source. Heavy lifting is done by an external program `CONFIG["path.internal.bin.upstream"]`. If the program is not found, fallback to the superclass `UpstreamAdapter`, which queries the upstream server (by default https://cheat.sh/) for the answer """ _adapter_name = "question" _output_format = "text+code" _cache_needed = True def _get_page(self, topic, request_options=None): """ Find answer for the `topic` question. """ if not os.path.exists(CONFIG["path.internal.bin.upstream"]): # if the upstream program is not found, use normal upstream adapter self._output_format = "ansi" return UpstreamAdapter._get_page( self, topic, request_options=request_options ) topic = topic.replace("+", " ") # if there is a language name in the section name, # cut it off (de:python => python) if "/" in topic: section_name, topic = topic.split("/", 1) if ":" in section_name: _, section_name = section_name.split(":", 1) section_name = SO_NAME.get(section_name, section_name) topic = "%s/%s" % (section_name, topic) # some clients send queries with - instead of + so we have to rewrite them to topic = re.sub(r"(?