44Exports:
55
66 get_topics_list()
7- get_answer_dict ()
7+ get_answers ()
88"""
9- from __future__ import print_function
109
11- import re
1210import random
11+ import re
12+ from typing import Any , Dict , List
13+
1314import cache
1415import adapter .cheat_sheets
1516import adapter .cmd
@@ -83,20 +84,28 @@ def get_topics_list(self, skip_dirs=False, skip_internal=False):
8384 self ._cached_topics_list = answer
8485 return answer
8586
86- def get_topic_type (self , topic ) :
87+ def get_topic_type (self , topic : str ) -> List [ str ] :
8788 """
88- Return topic type for `topic` or "unknown" if topic can't be determined.
89+ Return list of topic types for `topic`
90+ or ["unknown"] if topic can't be determined.
8991 """
9092
91- def __get_topic_type (topic ):
93+ def __get_topic_type (topic : str ) -> List [str ]:
94+ result = []
9295 for regexp , route in self .routing_table :
9396 if re .search (regexp , topic ):
9497 if route in self ._adapter :
9598 if self ._adapter [route ].is_found (topic ):
96- return route
99+ result . append ( route )
97100 else :
98- return route
99- return CONFIG ["routing.default" ]
101+ result .append (route )
102+ if not result :
103+ return [CONFIG ["routing.default" ]]
104+
105+ # cut the default route off, if there are more than one route found
106+ if len (result ) > 1 :
107+ return result [:- 1 ]
108+ return result
100109
101110 if topic not in self ._cached_topic_type :
102111 self ._cached_topic_type [topic ] = __get_topic_type (topic )
@@ -111,7 +120,9 @@ def _get_page_dict(self, query, topic_type, request_options=None):
111120
112121 def handle_if_random_request (self , topic ):
113122 """
114- Check if the `query` if a :random one, if yes we check its correctness and then randomly select a topic, based on the provided prefix.
123+ Check if the `query` is a :random one,
124+ if yes we check its correctness and then randomly select a topic,
125+ based on the provided prefix.
115126
116127 """
117128
@@ -152,60 +163,80 @@ def __select_random_topic(prefix, topic_list):
152163 #Here if not a random requst, we just forward the topic
153164 return topic
154165
155- def get_answer_dict (self , topic , request_options = None ):
166+ def get_answers (self , topic : str , request_options : Dict [ str , str ] = None ) -> List [ Dict [ str , Any ]] :
156167 """
157- Find cheat sheet for the topic.
168+ Find cheat sheets for the topic.
158169
159170 Args:
160171 `topic` (str): the name of the topic of the cheat sheet
161172
162173 Returns:
163- answer_dict: the answer dictionary
174+ [ answer_dict] : list of answers (dictionaries)
164175 """
165176
177+ # if topic specified as <topic_type>:<topic>,
178+ # cut <topic_type> off
179+ topic_type = ""
180+ if re .match ("[^/]+:" , topic ):
181+ topic_type , topic = topic .split (":" , 1 )
182+
166183 topic = self .handle_if_random_request (topic )
167- topic_type = self .get_topic_type (topic )
184+ topic_types = self .get_topic_type (topic )
185+
186+ # if topic_type is specified explicitly,
187+ # show pages only of that type
188+ if topic_type and topic_type in topic_types :
189+ topic_types = [topic_type ]
168190
169191 # 'question' queries are pretty expensive, that's why they should be handled
170192 # in a special way:
171193 # we do not drop the old style cache entries and try to reuse them if possible
172- if topic_type == 'question' :
194+ if topic_types == [ 'question' ] :
173195 answer = cache .get ('q:' + topic )
174196 if answer :
175197 if isinstance (answer , dict ):
176- return answer
177- return {
198+ return [ answer ]
199+ return [ {
178200 'topic' : topic ,
179201 'topic_type' : 'question' ,
180202 'answer' : answer ,
181203 'format' : 'text+code' ,
182- }
204+ }]
183205
184- answer = self ._get_page_dict (topic , topic_type , request_options = request_options )
206+ answer = self ._get_page_dict (topic , topic_types [ 0 ] , request_options = request_options )
185207 if answer .get ("cache" , True ):
186208 cache .put ('q:' + topic , answer )
187- return answer
209+ return [ answer ]
188210
189211 # Try to find cacheable queries in the cache.
190212 # If answer was not found in the cache, resolve it in a normal way and save in the cache
191- cache_needed = self ._adapter [topic_type ].is_cache_needed ()
192- if cache_needed :
193- answer = cache .get (topic )
194- if not isinstance (answer , dict ):
195- answer = None
196- if answer :
197- return answer
213+ answers = []
214+ for topic_type in topic_types :
198215
199- answer = self ._get_page_dict (topic , topic_type , request_options = request_options )
200- if isinstance (answer , dict ):
201- if "cache" in answer :
202- cache_needed = answer ["cache" ]
216+ cache_entry_name = f"{ topic_type } :{ topic } "
217+ cache_needed = self ._adapter [topic_type ].is_cache_needed ()
203218
204- if cache_needed and answer :
205- cache .put (topic , answer )
206- return answer
219+ if cache_needed :
220+ answer = cache .get (cache_entry_name )
221+ if not isinstance (answer , dict ):
222+ answer = None
223+ if answer :
224+ answers .append (answer )
225+ continue
226+
227+ answer = self ._get_page_dict (topic , topic_type , request_options = request_options )
228+ if isinstance (answer , dict ):
229+ if "cache" in answer :
230+ cache_needed = answer ["cache" ]
231+
232+ if cache_needed and answer :
233+ cache .put (cache_entry_name , answer )
234+
235+ answers .append (answer )
236+
237+ return answers
207238
208239# pylint: disable=invalid-name
209240_ROUTER = Router ()
210241get_topics_list = _ROUTER .get_topics_list
211- get_answer_dict = _ROUTER .get_answer_dict
242+ get_answers = _ROUTER .get_answers
0 commit comments