diff --git a/lib/adapter/cmd.py b/lib/adapter/cmd.py index 9e06974..5e5b489 100644 --- a/lib/adapter/cmd.py +++ b/lib/adapter/cmd.py @@ -8,6 +8,22 @@ from gevent.subprocess import Popen, PIPE from .adapter import Adapter +import os.path +import re + +def _get_abspath(path): + """Find absolute path of the specified `path` + according to its + """ + + if path.startswith("/"): + return path + + import __main__ + return os.path.join( + os.path.dirname(os.path.dirname(__main__.__file__)), + path) + class CommandAdapter(Adapter): """ """ @@ -20,8 +36,11 @@ class CommandAdapter(Adapter): def _get_page(self, topic, request_options=None): cmd = self._get_command(topic, request_options=request_options) if cmd: - proc = Popen(cmd, stdout=PIPE, stderr=PIPE) - answer = proc.communicate()[0].decode('utf-8') + try: + proc = Popen(cmd, stdout=PIPE, stderr=PIPE) + answer = proc.communicate()[0].decode('utf-8') + except OSError: + return "ERROR of the \"%s\" adapter: please create an issue" % self._adapter_name return answer return "" @@ -64,3 +83,34 @@ class Translation(CommandAdapter): return ["/home/igor/cheat.sh/bin/get_translation", from_, to_, topic.replace('+', ' ')] + + +class AdapterRfc(CommandAdapter): + """ + Show RFC by its number. + Exported as: "/rfc/NUMBER" + """ + + _adapter_name = "rfc" + _output_format = "text" + _cache_needed = True + _command = ["share/adapters/rfc.sh"] + + def _get_command(self, topic, request_options=None): + cmd = self._command[:] + if not cmd[0].startswith("/"): + cmd[0] = _get_abspath(cmd[0]) + + # cut rfc/ off + if topic.startswith("rfc/"): + topic = topic[4:] + + return cmd + [topic] + + def _get_list(self, prefix=None): + return list("rfc/%s" % x for x in range(1, 8649)) + + def is_found(self, topic): + if re.match("rfc/[0-9]+$", topic): + return True + return False diff --git a/lib/config.py b/lib/config.py index af40e4c..9bd91f3 100644 --- a/lib/config.py +++ b/lib/config.py @@ -84,6 +84,7 @@ _CONFIG = { "cheat.sheets", "cheat.sheets dir", "learnxiny", + "rfc", ], "adapters.mandatory": [ "search", @@ -112,6 +113,7 @@ _CONFIG = { "routing.pre": [ ("^$", "search"), ("^[^/]*/rosetta(/|$)", "rosetta"), + ("^rfc/", "rfc"), ("^:", "internal"), ("/:list$", "internal"), ("/$", "cheat.sheets dir"),