1
0
mirror of https://github.com/chubin/cheat.sh.git synced 2026-06-20 13:16:44 +02:00

refactored lib/adapter/cmd.py

This commit is contained in:
Igor Chubin
2019-01-31 14:32:35 +00:00
parent 268c8e5bf3
commit bd05ce4193
3 changed files with 94 additions and 62 deletions
+70 -43
View File
@@ -3,6 +3,7 @@ from gevent.subprocess import Popen, PIPE
patch_all()
import sys
import abc
import os
import glob
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
@@ -12,56 +13,82 @@ from globals import PATH_TLDR_PAGES, PATH_CHEAT_PAGES
def _get_filenames(path):
return [os.path.split(topic)[1] for topic in glob.glob(path)]
def get_tldr_list():
return [filename[:-3]
for filename in _get_filenames(PATH_TLDR_PAGES) if filename.endswith('.md')]
class Cmd(object):
def __init__(self):
self._list = self._get_list()
_TLDR_LIST = get_tldr_list()
def tldr_is_found(topic):
return topic in _TLDR_LIST
@abc.abstractmethod
def _get_list(self):
return []
def get_tldr(topic, request_options=None):
cmd = ["tldr", topic]
proc = Popen(cmd, stdout=PIPE, stderr=PIPE)
answer = proc.communicate()[0]
def get_list(self):
return self._list
fixed_answer = []
for line in answer.splitlines():
line = line[2:]
if line.startswith('-'):
line = '# '+line[2:]
elif not line.startswith(' '):
line = "# "+line
else:
pass
def is_found(self, topic):
return topic in self._list
fixed_answer.append(line)
@abc.abstractmethod
def get_page(self, topic, request_options=None):
pass
answer = "\n".join(fixed_answer) + "\n"
return answer.decode('utf-8')
class Tldr(Cmd):
def _get_list(self):
return [filename[:-3]
for filename in _get_filenames(PATH_TLDR_PAGES) if filename.endswith('.md')]
def get_cheat_list():
return _get_filenames(PATH_CHEAT_PAGES)
def get_page(self, topic, request_options=None):
cmd = ["tldr", topic]
proc = Popen(cmd, stdout=PIPE, stderr=PIPE)
answer = proc.communicate()[0]
_CHEAT_LIST = get_cheat_list()
def cheat_is_found(topic):
return topic in _CHEAT_LIST
fixed_answer = []
for line in answer.splitlines():
line = line[2:]
if line.startswith('-'):
line = '# '+line[2:]
elif not line.startswith(' '):
line = "# "+line
else:
pass
def get_cheat(topic, request_options=None):
cmd = ["cheat", topic]
proc = Popen(cmd, stdout=PIPE, stderr=PIPE)
answer = proc.communicate()[0].decode('utf-8')
return answer
fixed_answer.append(line)
def get_translation(topic, request_options=None):
from_, topic = topic.split('/', 1)
to_ = request_options.get('lang', 'en')
if '-' in from_:
from_, to_ = from_.split('-', 1)
answer = "\n".join(fixed_answer) + "\n"
return answer.decode('utf-8')
cmd = ["/home/igor/cheat.sh/bin/get_translation",
from_, to_, topic.replace('+', ' ')]
print("calling:", cmd)
proc = Popen(cmd, stdout=PIPE, stderr=PIPE)
answer = proc.communicate()[0].decode('utf-8')
return answer
class Cheat(Cmd):
def _get_list(self):
return _get_filenames(PATH_CHEAT_PAGES)
def get_page(self, topic, request_options=None):
cmd = ["cheat", topic]
proc = Popen(cmd, stdout=PIPE, stderr=PIPE)
answer = proc.communicate()[0].decode('utf-8')
return answer
class Fosdem(Cmd):
def _get_list(self):
return ['fosdem']
def get_page(self, topic, request_options=None):
cmd = ["sudo", "/home/igor/bin/current-fosdem-slide"]
proc = Popen(cmd, stdout=PIPE, stderr=PIPE)
answer = proc.communicate()[0].decode('utf-8')
return answer
class Translation(Cmd):
def _get_list(self):
return []
def get_page(self, topic, request_options=None):
from_, topic = topic.split('/', 1)
to_ = request_options.get('lang', 'en')
if '-' in from_:
from_, to_ = from_.split('-', 1)
cmd = ["/home/igor/cheat.sh/bin/get_translation",
from_, to_, topic.replace('+', ' ')]
print("calling:", cmd)
proc = Popen(cmd, stdout=PIPE, stderr=PIPE)
answer = proc.communicate()[0].decode('utf-8')
return answer
+1 -1
View File
@@ -217,7 +217,7 @@ def _visualize(query, keyword, answers, request_options, html=None): # pylint: d
if topic_type == "internal" and highlight:
answer = _colorize_internal(topic, answer, html)
elif topic_type == "late.nz":
elif topic_type in ["late.nz", "fosdem"]:
pass
else:
answer = _colorize_ansi_answer(
+23 -18
View File
@@ -48,32 +48,36 @@ class Router(object):
self._cached_topics_list = []
self._cached_topic_type = {}
self.adapter_internal = adapter.internal.InternalPages(
get_topic_type=self.get_topic_type,
get_topics_list=self.get_topics_list)
self.adapter_unknown = adapter.internal.UnknownPages(
get_topic_type=self.get_topic_type,
get_topics_list=self.get_topics_list)
self._adapter = {
"internal": adapter.internal.InternalPages(
get_topic_type=self.get_topic_type,
get_topics_list=self.get_topics_list),
"unknown": adapter.internal.UnknownPages(
get_topic_type=self.get_topic_type,
get_topics_list=self.get_topics_list),
"tldr": adapter.cmd.Tldr(),
"cheat": adapter.cmd.Cheat(),
"fosdem": adapter.cmd.Fosdem(),
"translation": adapter.cmd.Translation(),
}
self._topic_list = {
"late.nz": adapter.latenz.get_list(),
"internal": self.adapter_internal.get_list(),
"tldr": adapter.cmd.get_tldr_list(),
"cheat": adapter.cmd.get_cheat_list(),
"cheat.sheets": adapter.cheat_sheets.get_list(),
"cheat.sheets dir": adapter.cheat_sheets.get_dirs_list(),
"learnxiny": get_learnxiny_list(),
}
for key, obj in self._adapter.items():
self._topic_list[key] = obj.get_list()
self._topic_found = {
"late.nz": adapter.latenz.is_found,
"internal": self.adapter_internal.is_found,
"tldr": adapter.cmd.tldr_is_found,
"cheat": adapter.cmd.cheat_is_found,
"cheat.sheets": adapter.cheat_sheets.is_found,
"cheat.sheets dir": adapter.cheat_sheets.is_dir_found,
"learnxiny": is_valid_learnxy,
}
for key, obj in self._adapter.items():
self._topic_found[key] = obj.is_found
# topic_type, function_getter
# should be replaced with a decorator
@@ -82,13 +86,14 @@ class Router(object):
("late.nz", adapter.latenz.get_answer),
("cheat.sheets", adapter.cheat_sheets.get_page),
("cheat.sheets dir", adapter.cheat_sheets.get_dir),
("tldr", adapter.cmd.get_tldr),
("internal", self.adapter_internal.get_page),
("cheat", adapter.cmd.get_cheat),
("learnxiny", get_learnxiny),
("translation", adapter.cmd.get_translation),
("question", adapter.question.get_page),
("unknown", self.adapter_unknown.get_page),
("fosdem", self._adapter["fosdem"].get_page),
("tldr", self._adapter["tldr"].get_page),
("internal", self._adapter["internal"].get_page),
("cheat", self._adapter["cheat"].get_page),
("translation", self._adapter["translation"].get_page),
("unknown", self._adapter["unknown"].get_page),
)
# pylint: enable=bad-whitespace
@@ -138,7 +143,7 @@ class Router(object):
if self._topic_found['cheat.sheets dir'](topic):
return "cheat.sheets dir"
for source in ['cheat.sheets', 'cheat', 'tldr', 'late.nz']:
for source in ['cheat.sheets', 'cheat', 'tldr', 'late.nz', 'fosdem']:
if self._topic_found[source](topic):
return source