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

adapters refactoring

This commit is contained in:
Igor Chubin
2019-02-08 18:04:45 +00:00
parent 4d044c0456
commit b0e7ac46a8
2 changed files with 11 additions and 29 deletions
+6 -23
View File
@@ -9,29 +9,12 @@ import glob
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from globals import PATH_TLDR_PAGES, PATH_CHEAT_PAGES
from adapter import Adapter
def _get_filenames(path):
return [os.path.split(topic)[1] for topic in glob.glob(path)]
class Cmd(object):
def __init__(self):
self._list = self._get_list()
@abc.abstractmethod
def _get_list(self):
return []
def get_list(self):
return self._list
def is_found(self, topic):
return topic in self._list
@abc.abstractmethod
def get_page(self, topic, request_options=None):
pass
class Tldr(Cmd):
class Tldr(Adapter):
def _get_list(self):
return [filename[:-3]
for filename in _get_filenames(PATH_TLDR_PAGES) if filename.endswith('.md')]
@@ -56,7 +39,7 @@ class Tldr(Cmd):
answer = "\n".join(fixed_answer) + "\n"
return answer.decode('utf-8')
class Cheat(Cmd):
class Cheat(Adapter):
def _get_list(self):
return _get_filenames(PATH_CHEAT_PAGES)
@@ -66,17 +49,17 @@ class Cheat(Cmd):
answer = proc.communicate()[0].decode('utf-8')
return answer
class Fosdem(Cmd):
class Fosdem(Adapter):
def _get_list(self):
return ['fosdem']
def get_page(self, topic, request_options=None):
cmd = ["sudo", "/home/igor/bin/current-fosdem-slide"]
cmd = ["sudo", "/usr/local/bin/current-fosdem-slide"]
proc = Popen(cmd, stdout=PIPE, stderr=PIPE)
answer = proc.communicate()[0].decode('utf-8')
return answer
class Translation(Cmd):
class Translation(Adapter):
def _get_list(self):
return []
+5 -6
View File
@@ -18,7 +18,6 @@ import beautifier
from globals import REDISHOST, MAX_SEARCH_LEN
from languages_data import LANGUAGE_ALIAS, SO_NAME, rewrite_editor_section_name
from adapter_learnxiny import get_learnxiny, get_learnxiny_list, is_valid_learnxy
import adapter.cheat_sheets
import adapter.cmd
import adapter.latenz
@@ -68,7 +67,7 @@ class Router(object):
"late.nz": adapter.latenz.get_list(),
"cheat.sheets": adapter.cheat_sheets.get_list(),
"cheat.sheets dir": adapter.cheat_sheets.get_dirs_list(),
"learnxiny": get_learnxiny_list(),
"learnxiny": adapter.learnxiny.get_learnxiny_list(),
}
for key, obj in self._adapter.items():
self._topic_list[key] = obj.get_list()
@@ -77,7 +76,7 @@ class Router(object):
"late.nz": adapter.latenz.is_found,
"cheat.sheets": adapter.cheat_sheets.is_found,
"cheat.sheets dir": adapter.cheat_sheets.is_dir_found,
"learnxiny": is_valid_learnxy,
"learnxiny": adapter.learnxiny.is_valid_learnxy,
}
for key, obj in self._adapter.items():
self._topic_found[key] = obj.is_found
@@ -89,7 +88,7 @@ class Router(object):
("late.nz", adapter.latenz.get_answer),
("cheat.sheets", adapter.cheat_sheets.get_page),
("cheat.sheets dir", adapter.cheat_sheets.get_dir),
("learnxiny", get_learnxiny),
("learnxiny", adapter.learnxiny.get_learnxiny),
("question", adapter.question.get_page),
("fosdem", self._adapter["fosdem"].get_page),
("rosetta", self._adapter["rosetta"].get_page),
@@ -122,7 +121,7 @@ class Router(object):
answer = sorted(set(answer.keys()))
# doing it in this strange way to save the order of the topics
for topic in get_learnxiny_list():
for topic in adapter.learnxiny.get_learnxiny_list():
if topic not in answer:
answer.append(topic)
@@ -160,7 +159,7 @@ class Router(object):
# topic contains '/'
#
if is_valid_learnxy(topic):
if adapter.learnxiny.is_valid_learnxy(topic):
return 'learnxiny'
topic_type = topic.split('/', 1)[0]
if topic_type in ['ru', 'fr'] or re.match(r'[a-z][a-z]-[a-z][a-z]$', topic_type):