From b62bb9118ce6d6631839e9c327a19d07578a986a Mon Sep 17 00:00:00 2001 From: fedeb Date: Thu, 5 Nov 2020 14:15:18 +0100 Subject: [PATCH 01/10] Added a check on get_answer_dict that identifies random requests --- lib/routing.py | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/lib/routing.py b/lib/routing.py index 1da0def..5684c34 100644 --- a/lib/routing.py +++ b/lib/routing.py @@ -9,7 +9,7 @@ Exports: from __future__ import print_function import re - +import random import cache import adapter.cheat_sheets import adapter.cmd @@ -106,10 +106,36 @@ class Router(object): """ Return answer_dict for the `query`. """ - return self._adapter[topic_type]\ .get_page_dict(query, request_options=request_options) + + def is_random_request(self,topic): + print("Entrato in is_random_request con {}".format(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() + topic = topic[:-8] + topic_list = [x[len(topic):] + for x in self.get_topics_list() + if x.startswith(topic + "/")] + if topic_list: + #This is a correct formatted query like /cpp/:random + print("La richiesta random è giusta") + print("Questa è la topic_list della richiesta :random = {}".format(topic_list)) + return True + return False + + def select_random_topic(self,topic): + print("Entrato in select_random_topic con {}".format(topic)) + topic = topic[:-8] + topic_list = [x[len(topic):] + for x in self.get_topics_list() + if x.startswith(topic + "/")] + if ":list" in topic_list: topic_list.remove(":list") + random_topic = topic + random.choice(topic_list) + return random_topic + + def get_answer_dict(self, topic, request_options=None): """ Find cheat sheet for the topic. @@ -120,6 +146,8 @@ class Router(object): Returns: answer_dict: the answer dictionary """ + if self.is_random_request(topic): + topic = self.select_random_topic(topic) topic_type = self.get_topic_type(topic) From bfa25504f328f16810106d87ef2319b61a2992d5 Mon Sep 17 00:00:00 2001 From: fedeb Date: Thu, 5 Nov 2020 14:32:34 +0100 Subject: [PATCH 02/10] Remved /:list from the possible random choice --- lib/routing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/routing.py b/lib/routing.py index 5684c34..65b1745 100644 --- a/lib/routing.py +++ b/lib/routing.py @@ -131,7 +131,7 @@ class Router(object): topic_list = [x[len(topic):] for x in self.get_topics_list() if x.startswith(topic + "/")] - if ":list" in topic_list: topic_list.remove(":list") + if "/:list" in topic_list: topic_list.remove("/:list") random_topic = topic + random.choice(topic_list) return random_topic From 417f7f9e69934c162d16261f88f51ba685c151cc Mon Sep 17 00:00:00 2001 From: fedeb Date: Sat, 7 Nov 2020 17:50:19 +0100 Subject: [PATCH 03/10] removed rosetta/ and '' from possible random choices --- lib/routing.py | 58 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 21 deletions(-) diff --git a/lib/routing.py b/lib/routing.py index 65b1745..bd27a3e 100644 --- a/lib/routing.py +++ b/lib/routing.py @@ -109,31 +109,48 @@ class Router(object): return self._adapter[topic_type]\ .get_page_dict(query, request_options=request_options) - - def is_random_request(self,topic): - print("Entrato in is_random_request con {}".format(topic)) + + def handle_if_random_request(self,topic): + """ + Return topic type for `topic` or "unknown" if topic can't be determined. + """ + + def __select_random_topic(stripped_topic,topic_list): + #Here we remove the special pages if present + if ":list" in topic_list: topic_list.remove(":list") + if "rosetta/" in topic_list: topic_list.remove("rosetta/") + random_topic = random.choice(topic_list) + print("Il random_topic è {}".format(stripped_topic + random_topic)) + return stripped_topic + random_topic + + print("Entrato in handle_if_random_request con {}".format(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() - topic = topic[:-8] - topic_list = [x[len(topic):] - for x in self.get_topics_list() - if x.startswith(topic + "/")] + + # Here we take the cheat.sh/x/ part + stripped_topic = topic[:-7] + + topic_list = [x[len(stripped_topic):] + for x in self.get_topics_list() + if x.startswith(stripped_topic)] + + if '' in topic_list: topic_list.remove('') if topic_list: #This is a correct formatted query like /cpp/:random print("La richiesta random è giusta") print("Questa è la topic_list della richiesta :random = {}".format(topic_list)) - return True - return False + random_topic = __select_random_topic(stripped_topic,topic_list) + return random_topic + else: + #This is wrongly random formatted + # It is not a valid random request, we just strip the /:random + wrongly_formatted_random = topic[:-8] + print("La richiesta random è sbagliata") + print("Eseguo lo stripping = {}".format(wrongly_formatted_random)) + return wrongly_formatted_random + #Here if not a random requst, we just forward the topic + return topic - def select_random_topic(self,topic): - print("Entrato in select_random_topic con {}".format(topic)) - topic = topic[:-8] - topic_list = [x[len(topic):] - for x in self.get_topics_list() - if x.startswith(topic + "/")] - if "/:list" in topic_list: topic_list.remove("/:list") - random_topic = topic + random.choice(topic_list) - return random_topic def get_answer_dict(self, topic, request_options=None): @@ -146,9 +163,8 @@ class Router(object): Returns: answer_dict: the answer dictionary """ - if self.is_random_request(topic): - topic = self.select_random_topic(topic) - + + topic = self.handle_if_random_request(topic) topic_type = self.get_topic_type(topic) # 'question' queries are pretty expensive, that's why they should be handled From e3ab4eafcb9cab6cadd35d59d68c8643938af2d1 Mon Sep 17 00:00:00 2001 From: fedeb Date: Sat, 7 Nov 2020 20:15:54 +0100 Subject: [PATCH 04/10] Fixed comments regarding :random --- lib/routing.py | 33 ++++++++++++--------------------- 1 file changed, 12 insertions(+), 21 deletions(-) diff --git a/lib/routing.py b/lib/routing.py index bd27a3e..925f123 100644 --- a/lib/routing.py +++ b/lib/routing.py @@ -114,40 +114,31 @@ class Router(object): """ Return topic type for `topic` or "unknown" if topic can't be determined. """ - - def __select_random_topic(stripped_topic,topic_list): - #Here we remove the special pages if present + + def __select_random_topic(prefix,topic_list): + #Here we remove the special cases if ":list" in topic_list: topic_list.remove(":list") if "rosetta/" in topic_list: topic_list.remove("rosetta/") random_topic = random.choice(topic_list) - print("Il random_topic è {}".format(stripped_topic + random_topic)) - return stripped_topic + random_topic + return prefix + random_topic - print("Entrato in handle_if_random_request con {}".format(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() - - # Here we take the cheat.sh/x/ part - stripped_topic = topic[:-7] - - topic_list = [x[len(stripped_topic):] + prefix = topic[:-7] + topic_list = [x[len(prefix):] for x in self.get_topics_list() - if x.startswith(stripped_topic)] - + if x.startswith(prefix)] if '' in topic_list: topic_list.remove('') if topic_list: - #This is a correct formatted query like /cpp/:random - print("La richiesta random è giusta") - print("Questa è la topic_list della richiesta :random = {}".format(topic_list)) - random_topic = __select_random_topic(stripped_topic,topic_list) + # This is a correct formatted random query like /cpp/:random, the topic_list is not empty. + random_topic = __select_random_topic(prefix,topic_list) return random_topic else: - #This is wrongly random formatted - # It is not a valid random request, we just strip the /:random + # This is a wrongly formatted random query like /xyxyxy/:random, the topic_list not empty + # we just strip the /:random and let the already implemented logic handle it. wrongly_formatted_random = topic[:-8] - print("La richiesta random è sbagliata") - print("Eseguo lo stripping = {}".format(wrongly_formatted_random)) return wrongly_formatted_random + #Here if not a random requst, we just forward the topic return topic From cb753025678864292da99385bd0d0075dbe01f8a Mon Sep 17 00:00:00 2001 From: fedeb Date: Sat, 7 Nov 2020 20:25:23 +0100 Subject: [PATCH 05/10] Fixed typo --- lib/routing.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/routing.py b/lib/routing.py index 925f123..0c220b2 100644 --- a/lib/routing.py +++ b/lib/routing.py @@ -112,9 +112,9 @@ class Router(object): def handle_if_random_request(self,topic): """ - Return topic type for `topic` or "unknown" if topic can't be determined. + 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 if ":list" in topic_list: topic_list.remove(":list") From f788f238a464fc6ef50f3943922a770a35965e02 Mon Sep 17 00:00:00 2001 From: fedeb Date: Sat, 7 Nov 2020 20:48:38 +0100 Subject: [PATCH 06/10] Minor fix on checking if is a random request --- lib/routing.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/routing.py b/lib/routing.py index 0c220b2..d8ac53c 100644 --- a/lib/routing.py +++ b/lib/routing.py @@ -124,17 +124,18 @@ class Router(object): 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)] if '' in topic_list: topic_list.remove('') if topic_list: - # This is a correct formatted random query like /cpp/:random, the topic_list is not empty. + # This is a correct formatted random query like /cpp/:random as the topic_list is not empty. random_topic = __select_random_topic(prefix,topic_list) return random_topic else: - # This is a wrongly formatted random query like /xyxyxy/:random, the topic_list not empty + # This is a wrongly formatted random query like /xyxyxy/:random as the topic_list is empty # we just strip the /:random and let the already implemented logic handle it. wrongly_formatted_random = topic[:-8] return wrongly_formatted_random From 2c9185d8358125be6b6502aa261d5b8581b2b69d Mon Sep 17 00:00:00 2001 From: fedeb Date: Sat, 7 Nov 2020 21:00:27 +0100 Subject: [PATCH 07/10] Added a line on the readme --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f7edf81..81490a5 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ Such a thing exists. [![Build Status](https://travis-ci.org/chubin/cheat.sh.svg?branch=master)](https://travis-ci.org/chubin/cheat.sh) ## Features - +notnot **cheat.sh** * Has a simple curl/browser interface. @@ -658,6 +658,7 @@ Other pages: :post how to post new cheat sheet :styles list of color styles :styles-demo show color styles usage examples + :random fetches a random page (can be used in a subsection too: /go/:random) ``` ## Search From f4cd21e4e07ab80a0fc7a235400509e87e3e64e7 Mon Sep 17 00:00:00 2001 From: fedeb Date: Sat, 7 Nov 2020 21:15:26 +0100 Subject: [PATCH 08/10] Another check if a given topic has just the :list and rosetta/ --- lib/routing.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/routing.py b/lib/routing.py index d8ac53c..6be5269 100644 --- a/lib/routing.py +++ b/lib/routing.py @@ -119,6 +119,9 @@ class Router(object): #Here we remove the special cases if ":list" in topic_list: topic_list.remove(":list") if "rosetta/" in topic_list: topic_list.remove("rosetta/") + #Here we still check that topic_list in not empty + if not topic_list: + return prefix random_topic = random.choice(topic_list) return prefix + random_topic From 73e4e46e3e69d121c9a699e34bb3373c52a1bd47 Mon Sep 17 00:00:00 2001 From: fedeb Date: Mon, 9 Nov 2020 18:39:52 +0100 Subject: [PATCH 09/10] Fixed readme & style, removed special cases from random pool. --- README.md | 2 +- lib/routing.py | 33 ++++++++++++++++++++------------- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 81490a5..e0aa288 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ Such a thing exists. [![Build Status](https://travis-ci.org/chubin/cheat.sh.svg?branch=master)](https://travis-ci.org/chubin/cheat.sh) ## Features -notnot + **cheat.sh** * Has a simple curl/browser interface. diff --git a/lib/routing.py b/lib/routing.py index 6be5269..e37c62b 100644 --- a/lib/routing.py +++ b/lib/routing.py @@ -109,33 +109,42 @@ class Router(object): return self._adapter[topic_type]\ .get_page_dict(query, request_options=request_options) - - def handle_if_random_request(self,topic): + 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): + + def __select_random_topic(prefix, topic_list): #Here we remove the special cases - if ":list" in topic_list: topic_list.remove(":list") - if "rosetta/" in topic_list: topic_list.remove("rosetta/") - #Here we still check that topic_list in not empty - if not topic_list: + if "rosetta/" in topic_list: + topic_list.remove("rosetta/") + + cleaned_topic_list = [ x for x in topic_list if ':' 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(topic_list) + + 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('/') + 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)] - if '' in topic_list: topic_list.remove('') + + if '' in topic_list: + topic_list.remove('') + if topic_list: # This is a correct formatted random query like /cpp/:random as the topic_list is not empty. - random_topic = __select_random_topic(prefix,topic_list) + random_topic = __select_random_topic(prefix, topic_list) return random_topic else: # This is a wrongly formatted random query like /xyxyxy/:random as the topic_list is empty @@ -145,8 +154,6 @@ class Router(object): #Here if not a random requst, we just forward the topic return topic - - def get_answer_dict(self, topic, request_options=None): """ From 45c803a274172ae2723a89998e84c5de10b28bf7 Mon Sep 17 00:00:00 2001 From: fedeb Date: Tue, 10 Nov 2020 11:19:43 +0100 Subject: [PATCH 10/10] Further removing of special cases --- lib/routing.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/routing.py b/lib/routing.py index e37c62b..b471373 100644 --- a/lib/routing.py +++ b/lib/routing.py @@ -117,10 +117,7 @@ class Router(object): def __select_random_topic(prefix, topic_list): #Here we remove the special cases - if "rosetta/" in topic_list: - topic_list.remove("rosetta/") - - cleaned_topic_list = [ x for x in topic_list if ':' not in x] + 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: