Skip to content

Commit b0e7ac4

Browse files
committed
adapters refactoring
1 parent 4d044c0 commit b0e7ac4

File tree

2 files changed

+11
-29
lines changed

2 files changed

+11
-29
lines changed

lib/adapter/cmd.py

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,12 @@
99
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
1010

1111
from globals import PATH_TLDR_PAGES, PATH_CHEAT_PAGES
12+
from adapter import Adapter
1213

1314
def _get_filenames(path):
1415
return [os.path.split(topic)[1] for topic in glob.glob(path)]
1516

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):
17+
class Tldr(Adapter):
3518
def _get_list(self):
3619
return [filename[:-3]
3720
for filename in _get_filenames(PATH_TLDR_PAGES) if filename.endswith('.md')]
@@ -56,7 +39,7 @@ def get_page(self, topic, request_options=None):
5639
answer = "\n".join(fixed_answer) + "\n"
5740
return answer.decode('utf-8')
5841

59-
class Cheat(Cmd):
42+
class Cheat(Adapter):
6043
def _get_list(self):
6144
return _get_filenames(PATH_CHEAT_PAGES)
6245

@@ -66,17 +49,17 @@ def get_page(self, topic, request_options=None):
6649
answer = proc.communicate()[0].decode('utf-8')
6750
return answer
6851

69-
class Fosdem(Cmd):
52+
class Fosdem(Adapter):
7053
def _get_list(self):
7154
return ['fosdem']
7255

7356
def get_page(self, topic, request_options=None):
74-
cmd = ["sudo", "/home/igor/bin/current-fosdem-slide"]
57+
cmd = ["sudo", "/usr/local/bin/current-fosdem-slide"]
7558
proc = Popen(cmd, stdout=PIPE, stderr=PIPE)
7659
answer = proc.communicate()[0].decode('utf-8')
7760
return answer
7861

79-
class Translation(Cmd):
62+
class Translation(Adapter):
8063
def _get_list(self):
8164
return []
8265

lib/get_answer.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
from globals import REDISHOST, MAX_SEARCH_LEN
1919
from languages_data import LANGUAGE_ALIAS, SO_NAME, rewrite_editor_section_name
2020

21-
from adapter_learnxiny import get_learnxiny, get_learnxiny_list, is_valid_learnxy
2221
import adapter.cheat_sheets
2322
import adapter.cmd
2423
import adapter.latenz
@@ -68,7 +67,7 @@ def __init__(self):
6867
"late.nz": adapter.latenz.get_list(),
6968
"cheat.sheets": adapter.cheat_sheets.get_list(),
7069
"cheat.sheets dir": adapter.cheat_sheets.get_dirs_list(),
71-
"learnxiny": get_learnxiny_list(),
70+
"learnxiny": adapter.learnxiny.get_learnxiny_list(),
7271
}
7372
for key, obj in self._adapter.items():
7473
self._topic_list[key] = obj.get_list()
@@ -77,7 +76,7 @@ def __init__(self):
7776
"late.nz": adapter.latenz.is_found,
7877
"cheat.sheets": adapter.cheat_sheets.is_found,
7978
"cheat.sheets dir": adapter.cheat_sheets.is_dir_found,
80-
"learnxiny": is_valid_learnxy,
79+
"learnxiny": adapter.learnxiny.is_valid_learnxy,
8180
}
8281
for key, obj in self._adapter.items():
8382
self._topic_found[key] = obj.is_found
@@ -89,7 +88,7 @@ def __init__(self):
8988
("late.nz", adapter.latenz.get_answer),
9089
("cheat.sheets", adapter.cheat_sheets.get_page),
9190
("cheat.sheets dir", adapter.cheat_sheets.get_dir),
92-
("learnxiny", get_learnxiny),
91+
("learnxiny", adapter.learnxiny.get_learnxiny),
9392
("question", adapter.question.get_page),
9493
("fosdem", self._adapter["fosdem"].get_page),
9594
("rosetta", self._adapter["rosetta"].get_page),
@@ -122,7 +121,7 @@ def get_topics_list(self, skip_dirs=False, skip_internal=False):
122121
answer = sorted(set(answer.keys()))
123122

124123
# doing it in this strange way to save the order of the topics
125-
for topic in get_learnxiny_list():
124+
for topic in adapter.learnxiny.get_learnxiny_list():
126125
if topic not in answer:
127126
answer.append(topic)
128127

@@ -160,7 +159,7 @@ def __get_topic_type(topic):
160159

161160
# topic contains '/'
162161
#
163-
if is_valid_learnxy(topic):
162+
if adapter.learnxiny.is_valid_learnxy(topic):
164163
return 'learnxiny'
165164
topic_type = topic.split('/', 1)[0]
166165
if topic_type in ['ru', 'fr'] or re.match(r'[a-z][a-z]-[a-z][a-z]$', topic_type):

0 commit comments

Comments
 (0)