|
| 1 | +import sys |
| 2 | +import os |
| 3 | +import glob |
| 4 | +import collections |
| 5 | + |
| 6 | +from fuzzywuzzy import process, fuzz |
| 7 | + |
| 8 | +sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) |
| 9 | +from globals import MYDIR, COLOR_STYLES |
| 10 | +from colorize_internal import colorize_internal |
| 11 | + |
| 12 | +_INTERNAL_TOPICS = [ |
| 13 | + ":cht.sh", |
| 14 | + ":bash_completion", |
| 15 | + ":emacs", |
| 16 | + ":emacs-ivy", |
| 17 | + ":firstpage", |
| 18 | + ":firstpage-v1", |
| 19 | + ":firstpage-v2", |
| 20 | + ":fish", |
| 21 | + ":help", |
| 22 | + ":intro", |
| 23 | + ":list", |
| 24 | + ":post", |
| 25 | + ":styles", |
| 26 | + ":styles-demo", |
| 27 | + ":vim", |
| 28 | + ":zsh", |
| 29 | + ":share", |
| 30 | + ] |
| 31 | + |
| 32 | +_COLORIZED_INTERNAL_TOPICS = [ |
| 33 | + ':intro', |
| 34 | +] |
| 35 | + |
| 36 | +class InternalPages(object): |
| 37 | + |
| 38 | + def __init__(self, get_topic_type=None, get_topics_list=None): |
| 39 | + self.get_topic_type = get_topic_type |
| 40 | + self.get_topics_list = get_topics_list |
| 41 | + |
| 42 | + def _get_stat(self): |
| 43 | + stat = collections.Counter([ |
| 44 | + self.get_topic_type(topic) |
| 45 | + for topic in self.get_topics_list() |
| 46 | + ]) |
| 47 | + |
| 48 | + answer = "" |
| 49 | + for key, val in stat.items(): |
| 50 | + answer += "%s %s\n" % (key, val) |
| 51 | + return answer |
| 52 | + |
| 53 | + |
| 54 | + @staticmethod |
| 55 | + def get_list(): |
| 56 | + return _INTERNAL_TOPICS |
| 57 | + |
| 58 | + def _get_list_answer(self, topic, request_options=None): |
| 59 | + if '/' in topic: |
| 60 | + topic_type, topic_name = topic.split('/', 1) |
| 61 | + if topic_name == ":list": |
| 62 | + topic_list = [x[len(topic_type)+1:] |
| 63 | + for x in self.get_topics_list() |
| 64 | + if x.startswith(topic_type + "/")] |
| 65 | + return "\n".join(topic_list)+"\n" |
| 66 | + |
| 67 | + answer = "" |
| 68 | + if topic == ":list": |
| 69 | + answer = "\n".join(x for x in self.get_topics_list()) + "\n" |
| 70 | + |
| 71 | + return answer |
| 72 | + |
| 73 | + def get_page(self, topic, request_options=None): |
| 74 | + if topic.endswith('/:list') or topic.lstrip('/') == ':list': |
| 75 | + return self._get_list_answer(topic) |
| 76 | + |
| 77 | + answer = "" |
| 78 | + if topic == ':styles': |
| 79 | + answer = "\n".join(COLOR_STYLES) + "\n" |
| 80 | + elif topic == ":stat": |
| 81 | + answer = self._get_stat()+"\n" |
| 82 | + elif topic in _INTERNAL_TOPICS: |
| 83 | + answer = open(os.path.join(MYDIR, "share", topic[1:]+".txt"), "r").read() |
| 84 | + if topic in _COLORIZED_INTERNAL_TOPICS: |
| 85 | + answer = colorize_internal(answer) |
| 86 | + |
| 87 | + return answer |
| 88 | + |
| 89 | + def is_found(self, topic): |
| 90 | + return topic in self.get_list() |
| 91 | + |
| 92 | +class UnknownPages(InternalPages): |
| 93 | + |
| 94 | + @staticmethod |
| 95 | + def get_list(): |
| 96 | + return [] |
| 97 | + |
| 98 | + @staticmethod |
| 99 | + def is_found(topic): |
| 100 | + return False |
| 101 | + |
| 102 | + def get_page(self, topic, request_options=None): |
| 103 | + topics_list = self.get_topics_list() |
| 104 | + if topic.startswith(':'): |
| 105 | + topics_list = [x for x in topics_list if x.startswith(':')] |
| 106 | + else: |
| 107 | + topics_list = [x for x in topics_list if not x.startswith(':')] |
| 108 | + |
| 109 | + possible_topics = process.extract(topic, topics_list, scorer=fuzz.ratio)[:3] |
| 110 | + possible_topics_text = "\n".join([(" * %s %s" % x) for x in possible_topics]) |
| 111 | + return """ |
| 112 | +Unknown topic. |
| 113 | +Do you mean one of these topics maybe? |
| 114 | +
|
| 115 | +%s |
| 116 | + """ % possible_topics_text |
0 commit comments