Skip to content

Commit bd05ce4

Browse files
committed
refactored lib/adapter/cmd.py
1 parent 268c8e5 commit bd05ce4

File tree

3 files changed

+104
-72
lines changed

3 files changed

+104
-72
lines changed

lib/adapter/cmd.py

Lines changed: 80 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
patch_all()
44

55
import sys
6+
import abc
67
import os
78
import glob
89
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
@@ -12,56 +13,82 @@
1213
def _get_filenames(path):
1314
return [os.path.split(topic)[1] for topic in glob.glob(path)]
1415

15-
def get_tldr_list():
16-
return [filename[:-3]
17-
for filename in _get_filenames(PATH_TLDR_PAGES) if filename.endswith('.md')]
18-
19-
_TLDR_LIST = get_tldr_list()
20-
def tldr_is_found(topic):
21-
return topic in _TLDR_LIST
22-
23-
def get_tldr(topic, request_options=None):
24-
cmd = ["tldr", topic]
25-
proc = Popen(cmd, stdout=PIPE, stderr=PIPE)
26-
answer = proc.communicate()[0]
27-
28-
fixed_answer = []
29-
for line in answer.splitlines():
30-
line = line[2:]
31-
if line.startswith('-'):
32-
line = '# '+line[2:]
33-
elif not line.startswith(' '):
34-
line = "# "+line
35-
else:
36-
pass
37-
38-
fixed_answer.append(line)
39-
40-
answer = "\n".join(fixed_answer) + "\n"
41-
return answer.decode('utf-8')
42-
43-
def get_cheat_list():
44-
return _get_filenames(PATH_CHEAT_PAGES)
45-
46-
_CHEAT_LIST = get_cheat_list()
47-
def cheat_is_found(topic):
48-
return topic in _CHEAT_LIST
49-
50-
def get_cheat(topic, request_options=None):
51-
cmd = ["cheat", topic]
52-
proc = Popen(cmd, stdout=PIPE, stderr=PIPE)
53-
answer = proc.communicate()[0].decode('utf-8')
54-
return answer
55-
56-
def get_translation(topic, request_options=None):
57-
from_, topic = topic.split('/', 1)
58-
to_ = request_options.get('lang', 'en')
59-
if '-' in from_:
60-
from_, to_ = from_.split('-', 1)
61-
62-
cmd = ["/home/igor/cheat.sh/bin/get_translation",
63-
from_, to_, topic.replace('+', ' ')]
64-
print("calling:", cmd)
65-
proc = Popen(cmd, stdout=PIPE, stderr=PIPE)
66-
answer = proc.communicate()[0].decode('utf-8')
67-
return answer
16+
class Cmd(object):
17+
def __init__(self):
18+
self._list = self._get_list()
19+
20+
@abc.abstractmethod
21+
def _get_list(self):
22+
return []
23+
24+
def get_list(self):
25+
return self._list
26+
27+
def is_found(self, topic):
28+
return topic in self._list
29+
30+
@abc.abstractmethod
31+
def get_page(self, topic, request_options=None):
32+
pass
33+
34+
class Tldr(Cmd):
35+
def _get_list(self):
36+
return [filename[:-3]
37+
for filename in _get_filenames(PATH_TLDR_PAGES) if filename.endswith('.md')]
38+
39+
def get_page(self, topic, request_options=None):
40+
cmd = ["tldr", topic]
41+
proc = Popen(cmd, stdout=PIPE, stderr=PIPE)
42+
answer = proc.communicate()[0]
43+
44+
fixed_answer = []
45+
for line in answer.splitlines():
46+
line = line[2:]
47+
if line.startswith('-'):
48+
line = '# '+line[2:]
49+
elif not line.startswith(' '):
50+
line = "# "+line
51+
else:
52+
pass
53+
54+
fixed_answer.append(line)
55+
56+
answer = "\n".join(fixed_answer) + "\n"
57+
return answer.decode('utf-8')
58+
59+
class Cheat(Cmd):
60+
def _get_list(self):
61+
return _get_filenames(PATH_CHEAT_PAGES)
62+
63+
def get_page(self, topic, request_options=None):
64+
cmd = ["cheat", topic]
65+
proc = Popen(cmd, stdout=PIPE, stderr=PIPE)
66+
answer = proc.communicate()[0].decode('utf-8')
67+
return answer
68+
69+
class Fosdem(Cmd):
70+
def _get_list(self):
71+
return ['fosdem']
72+
73+
def get_page(self, topic, request_options=None):
74+
cmd = ["sudo", "/home/igor/bin/current-fosdem-slide"]
75+
proc = Popen(cmd, stdout=PIPE, stderr=PIPE)
76+
answer = proc.communicate()[0].decode('utf-8')
77+
return answer
78+
79+
class Translation(Cmd):
80+
def _get_list(self):
81+
return []
82+
83+
def get_page(self, topic, request_options=None):
84+
from_, topic = topic.split('/', 1)
85+
to_ = request_options.get('lang', 'en')
86+
if '-' in from_:
87+
from_, to_ = from_.split('-', 1)
88+
89+
cmd = ["/home/igor/cheat.sh/bin/get_translation",
90+
from_, to_, topic.replace('+', ' ')]
91+
print("calling:", cmd)
92+
proc = Popen(cmd, stdout=PIPE, stderr=PIPE)
93+
answer = proc.communicate()[0].decode('utf-8')
94+
return answer

lib/cheat_wrapper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ def _visualize(query, keyword, answers, request_options, html=None): # pylint: d
217217

218218
if topic_type == "internal" and highlight:
219219
answer = _colorize_internal(topic, answer, html)
220-
elif topic_type == "late.nz":
220+
elif topic_type in ["late.nz", "fosdem"]:
221221
pass
222222
else:
223223
answer = _colorize_ansi_answer(

lib/get_answer.py

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -48,32 +48,36 @@ def __init__(self):
4848
self._cached_topics_list = []
4949
self._cached_topic_type = {}
5050

51-
self.adapter_internal = adapter.internal.InternalPages(
52-
get_topic_type=self.get_topic_type,
53-
get_topics_list=self.get_topics_list)
54-
self.adapter_unknown = adapter.internal.UnknownPages(
55-
get_topic_type=self.get_topic_type,
56-
get_topics_list=self.get_topics_list)
51+
self._adapter = {
52+
"internal": adapter.internal.InternalPages(
53+
get_topic_type=self.get_topic_type,
54+
get_topics_list=self.get_topics_list),
55+
"unknown": adapter.internal.UnknownPages(
56+
get_topic_type=self.get_topic_type,
57+
get_topics_list=self.get_topics_list),
58+
"tldr": adapter.cmd.Tldr(),
59+
"cheat": adapter.cmd.Cheat(),
60+
"fosdem": adapter.cmd.Fosdem(),
61+
"translation": adapter.cmd.Translation(),
62+
}
5763

5864
self._topic_list = {
5965
"late.nz": adapter.latenz.get_list(),
60-
"internal": self.adapter_internal.get_list(),
61-
"tldr": adapter.cmd.get_tldr_list(),
62-
"cheat": adapter.cmd.get_cheat_list(),
6366
"cheat.sheets": adapter.cheat_sheets.get_list(),
6467
"cheat.sheets dir": adapter.cheat_sheets.get_dirs_list(),
6568
"learnxiny": get_learnxiny_list(),
6669
}
70+
for key, obj in self._adapter.items():
71+
self._topic_list[key] = obj.get_list()
6772

6873
self._topic_found = {
6974
"late.nz": adapter.latenz.is_found,
70-
"internal": self.adapter_internal.is_found,
71-
"tldr": adapter.cmd.tldr_is_found,
72-
"cheat": adapter.cmd.cheat_is_found,
7375
"cheat.sheets": adapter.cheat_sheets.is_found,
7476
"cheat.sheets dir": adapter.cheat_sheets.is_dir_found,
7577
"learnxiny": is_valid_learnxy,
7678
}
79+
for key, obj in self._adapter.items():
80+
self._topic_found[key] = obj.is_found
7781

7882
# topic_type, function_getter
7983
# should be replaced with a decorator
@@ -82,13 +86,14 @@ def __init__(self):
8286
("late.nz", adapter.latenz.get_answer),
8387
("cheat.sheets", adapter.cheat_sheets.get_page),
8488
("cheat.sheets dir", adapter.cheat_sheets.get_dir),
85-
("tldr", adapter.cmd.get_tldr),
86-
("internal", self.adapter_internal.get_page),
87-
("cheat", adapter.cmd.get_cheat),
8889
("learnxiny", get_learnxiny),
89-
("translation", adapter.cmd.get_translation),
9090
("question", adapter.question.get_page),
91-
("unknown", self.adapter_unknown.get_page),
91+
("fosdem", self._adapter["fosdem"].get_page),
92+
("tldr", self._adapter["tldr"].get_page),
93+
("internal", self._adapter["internal"].get_page),
94+
("cheat", self._adapter["cheat"].get_page),
95+
("translation", self._adapter["translation"].get_page),
96+
("unknown", self._adapter["unknown"].get_page),
9297
)
9398
# pylint: enable=bad-whitespace
9499

@@ -138,7 +143,7 @@ def __get_topic_type(topic):
138143
if self._topic_found['cheat.sheets dir'](topic):
139144
return "cheat.sheets dir"
140145

141-
for source in ['cheat.sheets', 'cheat', 'tldr', 'late.nz']:
146+
for source in ['cheat.sheets', 'cheat', 'tldr', 'late.nz', 'fosdem']:
142147
if self._topic_found[source](topic):
143148
return source
144149

0 commit comments

Comments
 (0)