Repository: chubin/cheat.sh
File: lib/routing.py (Python)

Code before:
                        if self._adapter[route].is_found(topic):
                            result.append(route)
                    else:
                        result.append(route)
            if not result:
                return [CONFIG["routing.default"]]

            # cut the default route off, if there are more than one route found
            if len(result) > 1:
                return result[:-1]
            return result

        if topic not in self._cached_topic_type:
            self._cached_topic_type[topic] = __get_topic_type(topic)
        return self._cached_topic_type[topic]

    def _get_page_dict(self, query, topic_type, request_options=None):
        """
        Return answer_dict for the `query`.
        """
        return self._adapter[topic_type]\
               .get_page_dict(query, request_options=request_options)

    def handle_if_random_request(self, topic):
        """
        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.

        """

        def __select_random_topic(prefix, topic_list):
            #Here we remove the special cases
            cleaned_topic_list = [ x for x in topic_list if '/' not in x and ':' not in x]

            #Here we still check that cleaned_topic_list in not empty
            if not cleaned_topic_list:
                return prefix
                
            random_topic = random.choice(cleaned_topic_list)
            return prefix + random_topic
        
        if topic.endswith('/:random') or topic.lstrip('/') == ':random':
            #We strip the :random part and see if the query is valid by running a get_topics_list()
            if topic.lstrip('/') == ':random' :
                 topic = topic.lstrip('/')
            prefix = topic[:-7]
            
            topic_list = [x[len(prefix):]
                         for x in self.get_topics_list()
                         if x.startswith(prefix)]

Review comment (suggestion):
```suggestion
        Check if the `query` is a :random one,
```

Code after:
                        if self._adapter[route].is_found(topic):
                            result.append(route)
                    else:
                        result.append(route)
            if not result:
                return [CONFIG["routing.default"]]

            # cut the default route off, if there are more than one route found
            if len(result) > 1:
                return result[:-1]
            return result

        if topic not in self._cached_topic_type:
            self._cached_topic_type[topic] = __get_topic_type(topic)
        return self._cached_topic_type[topic]

    def _get_page_dict(self, query, topic_type, request_options=None):
        """
        Return answer_dict for the `query`.
        """
        return self._adapter[topic_type]\
               .get_page_dict(query, request_options=request_options)

    def handle_if_random_request(self, topic):
        """
        Check if the `query` is a :random one,
        if yes we check its correctness and then randomly select a topic,
        based on the provided prefix.

        """

        def __select_random_topic(prefix, topic_list):
            #Here we remove the special cases
            cleaned_topic_list = [ x for x in topic_list if '/' not in x and ':' not in x]

            #Here we still check that cleaned_topic_list in not empty
            if not cleaned_topic_list:
                return prefix
                
            random_topic = random.choice(cleaned_topic_list)
            return prefix + random_topic
        
        if topic.endswith('/:random') or topic.lstrip('/') == ':random':
            #We strip the :random part and see if the query is valid by running a get_topics_list()
            if topic.lstrip('/') == ':random' :
                 topic = topic.lstrip('/')
            prefix = topic[:-7]
            
            topic_list = [x[len(prefix):]
                         for x in self.get_topics_list()
                         if x.startswith(prefix)]