Skip to content

Commit e83fd89

Browse files
committed
routing refactoring (get_answer.py => routing.py + postprocessing.py)
1 parent f70b13a commit e83fd89

File tree

7 files changed

+122
-191
lines changed

7 files changed

+122
-191
lines changed

lib/adapter/adapter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def get_list(self, prefix=None):
2121
if prefix in self._list:
2222
return self._list[prefix]
2323

24-
self._list[prefix] = self._get_list(prefix=prefix)
24+
self._list[prefix] = set(self._get_list(prefix=prefix))
2525
return self._list[prefix]
2626

2727
def is_found(self, topic):

lib/adapter/internal.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,16 @@ def _get_page(self, topic, request_options=None):
124124
125125
%s
126126
""" % possible_topics_text
127+
128+
class Search(Adapter):
129+
130+
_adapter_name = 'search'
131+
_output_format = 'text'
132+
_cache_needed = False
133+
134+
@staticmethod
135+
def get_list(prefix=None):
136+
return []
137+
138+
def is_found(topic):
139+
return True

lib/adapter/learnxiny.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,9 @@ def is_found(self, topic):
825825
Return whether `topic` is a valid learnxiny topic
826826
"""
827827

828+
if '/' not in topic:
829+
return False
830+
828831
lang, topic = topic.split('/', 1)
829832
if lang not in self.adapters:
830833
return False

lib/adapter/question.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ def _get_page(self, topic, request_options=None):
3232
section_name, topic = topic.split('/', 1)
3333
if ':' in section_name:
3434
_, section_name = section_name.split(':', 1)
35+
section_name = SO_NAME.get(section_name, section_name)
3536
topic = "%s/%s" % (section_name, topic)
3637

38+
3739
# some clients send queries with - instead of + so we have to rewrite them to
3840
topic = re.sub(r"(?<!-)-", ' ', topic)
3941

lib/adapter/rosetta.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Rosetta(Adapter):
2323
Adapter for RosettaCode
2424
"""
2525

26-
__section_name = 'rosetta'
26+
__section_name = "rosetta"
2727
_adapter_name = "rosetta"
2828
_output_format = "code"
2929

lib/cheat_wrapper.py

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111
import re
1212
import json
1313

14-
from get_answer import get_answer, find_answer_by_keyword, get_topics_list
14+
from routing import get_answer_dict, get_topics_list
15+
from search import find_answers_by_keyword
16+
from languages_data import LANGUAGE_ALIAS, rewrite_editor_section_name
17+
import postprocessing
18+
1519
import frontend.html
1620
import frontend.ansi
1721

@@ -22,6 +26,28 @@ def cheat_wrapper(query, request_options=None, output_format='ansi'):
2226
Additional request options specified in `request_options`.
2327
"""
2428

29+
def _rewrite_aliases(word):
30+
if word == ':bash.completion':
31+
return ':bash_completion'
32+
return word
33+
34+
def _rewrite_section_name(query):
35+
"""
36+
Rewriting special section names:
37+
* EDITOR:NAME => emacs:go-mode
38+
"""
39+
40+
if '/' not in query:
41+
return query
42+
43+
section_name, rest = query.split('/', 1)
44+
45+
if ':' in section_name:
46+
section_name = rewrite_editor_section_name(section_name)
47+
section_name = LANGUAGE_ALIAS.get(section_name, section_name)
48+
49+
return "%s/%s" % (section_name, rest)
50+
2551
def _sanitize_query(query):
2652
return re.sub('[<>"]', '', query)
2753

@@ -48,17 +74,26 @@ def _parse_query(query):
4874
return topic, keyword, search_options
4975

5076
query = _sanitize_query(query)
77+
query = _rewrite_aliases(query)
78+
query = _rewrite_section_name(query)
79+
5180

5281
# at the moment, we just remove trailing slashes
5382
# so queries python/ and python are equal
5483
query = _strip_hyperlink(query.rstrip('/'))
5584
topic, keyword, search_options = _parse_query(query)
5685

5786
if keyword:
58-
answers = find_answer_by_keyword(
87+
answers = find_answers_by_keyword(
5988
topic, keyword, options=search_options, request_options=request_options)
6089
else:
61-
answers = [get_answer(topic, keyword, request_options=request_options)]
90+
answers = [get_answer_dict(topic, request_options=request_options)]
91+
92+
answers = [
93+
postprocessing.postprocess(
94+
answer, keyword, search_options, request_options=request_options)
95+
for answer in answers
96+
]
6297

6398
answer_data = {
6499
'query': query,

0 commit comments

Comments
 (0)