Skip to content

Commit 4fff68b

Browse files
committed
Merge branch 'master' of https://github.com/chubin/cheat.sh
2 parents 6d74567 + b9839f4 commit 4fff68b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+4860
-2629
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -482,9 +482,9 @@ Features supported by cheat.sh plugins for different editors:
482482
|-------------------|-----|-------|---|------|----|
483483
|Command queries ||||||
484484
|Queries from buffer| | ||| |
485-
|Toggle comments | | || | |
486-
|Prev/next answer | | || | |
487-
|Multiple answers | || | | |
485+
|Toggle comments | | || | |
486+
|Prev/next answer | | || | |
487+
|Multiple answers | || | | |
488488
|Warnings as queries| | || | |
489489
|Queries history | | ||| |
490490
|Session id | | || | |

bin/srv.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,11 @@ def answer(topic=None):
247247
return "429 %s\n" % not_allowed, 429
248248

249249
html_is_needed = is_html_needed(user_agent) and not is_result_a_script(topic)
250-
result, found = cheat_wrapper(topic, request_options=options, html=html_is_needed)
250+
if html_is_needed:
251+
output_format='html'
252+
else:
253+
output_format='ansi'
254+
result, found = cheat_wrapper(topic, request_options=options, output_format=output_format)
251255
if 'Please come back in several hours' in result and html_is_needed:
252256
return MALFORMED_RESPONSE_HTML_PAGE
253257

lib/adapter/adapter.py

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,68 @@
11
import abc
22

33
class Adapter(object):
4+
5+
_adapter_name = None
6+
_output_format = 'code'
7+
_cache_needed = False
8+
49
def __init__(self):
5-
self._list = self._get_list()
10+
self._list = {None: self._get_list()}
611

712
@abc.abstractmethod
8-
def _get_list(self):
13+
def _get_list(self, prefix=None):
914
return []
1015

11-
def get_list(self):
12-
return self._list
16+
def get_list(self, prefix=None):
17+
"""
18+
Return available pages for `prefix`
19+
"""
20+
21+
if prefix in self._list:
22+
return self._list[prefix]
23+
24+
self._list[prefix] = set(self._get_list(prefix=prefix))
25+
return self._list[prefix]
1326

1427
def is_found(self, topic):
15-
return topic in self._list
28+
"""
29+
check if `topic` is available
30+
CAUTION: only root is checked
31+
"""
32+
return topic in self._list[None]
33+
34+
def is_cache_needed(self):
35+
"""
36+
Return True if answers should be cached.
37+
Return False if answers should not be cached.
38+
"""
39+
return self._cache_needed
1640

1741
@abc.abstractmethod
18-
def get_page(self, topic, request_options=None):
42+
def _get_page(self, topic, request_options=None):
43+
"""
44+
Return page for `topic`
45+
"""
1946
pass
47+
48+
def _get_output_format(self, topic):
49+
if '/' in topic:
50+
subquery = topic.split('/')[-1]
51+
else:
52+
subquery = topic
53+
54+
if subquery in [':list']:
55+
return 'text'
56+
return self._output_format
57+
58+
def get_page_dict(self, topic, request_options=None):
59+
"""
60+
Return page dict for `topic`
61+
"""
62+
answer_dict = {
63+
'topic': topic,
64+
'topic_type': self._adapter_name,
65+
'answer': self._get_page(topic, request_options=request_options),
66+
'format': self._get_output_format(topic),
67+
}
68+
return answer_dict

lib/adapter/cheat_sheets.py

Lines changed: 48 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
66
from globals import PATH_CHEAT_SHEETS
77

8+
from adapter import Adapter
9+
810
def _remove_initial_underscore(filename):
911
if filename.startswith('_'):
1012
filename = filename[1:]
@@ -31,41 +33,51 @@ def _get_answers_and_dirs():
3133
answers = [os.path.split(topic)[1] for topic in topics if not _isdir(topic)]
3234
return answers, answer_dirs
3335

34-
def _update_cheat_sheets_topics():
35-
answers = _get_answer_files_from_folder()
36-
cheatsheet_answers, cheatsheet_dirs = _get_answers_and_dirs()
37-
return answers+cheatsheet_answers, cheatsheet_dirs
38-
39-
def get_list():
40-
return _update_cheat_sheets_topics()[0]
41-
42-
def get_dirs_list():
43-
return _update_cheat_sheets_topics()[1]
44-
45-
_CHEAT_SHEETS_LIST = get_list()
46-
def is_found(topic):
47-
return topic in _CHEAT_SHEETS_LIST
48-
49-
_CHEAT_SHEETS_DIRS = get_dirs_list()
50-
def is_dir_found(topic):
51-
return topic in _CHEAT_SHEETS_DIRS
52-
53-
def get_page(topic, request_options=None):
54-
"""
55-
Get the cheat sheet topic from the own repository (cheat.sheets).
56-
It's possible that topic directory starts with omitted underscore
57-
"""
58-
filename = PATH_CHEAT_SHEETS + "%s" % topic
59-
if not os.path.exists(filename):
60-
filename = PATH_CHEAT_SHEETS + "_%s" % topic
61-
if os.path.isdir(filename):
62-
return ""
63-
else:
36+
class CheatSheets(Adapter):
37+
38+
_adapter_name = "cheat.sheets"
39+
_output_format = "code"
40+
41+
def __init__(self):
42+
self._answers = []
43+
self._cheatsheet_answers = []
44+
self._cheatsheet_dirs = []
45+
Adapter.__init__(self)
46+
47+
def _update_cheat_sheets_topics(self):
48+
self._answers = _get_answer_files_from_folder()
49+
self._cheatsheet_answers, self._cheatsheet_dirs = _get_answers_and_dirs()
50+
return self._answers + self._cheatsheet_answers, self._cheatsheet_dirs
51+
52+
def _get_list(self, prefix=None):
53+
return self._update_cheat_sheets_topics()[0]
54+
55+
def _get_page(self, topic, request_options=None):
56+
"""
57+
Get the cheat sheet topic from the own repository (cheat.sheets).
58+
It's possible that topic directory starts with omitted underscore
59+
"""
60+
filename = PATH_CHEAT_SHEETS + "%s" % topic
61+
if not os.path.exists(filename):
62+
filename = PATH_CHEAT_SHEETS + "_%s" % topic
63+
if os.path.isdir(filename):
64+
return ""
6465
return open(filename, "r").read().decode('utf-8')
6566

66-
def get_dir(topic, request_options=None):
67-
answer = []
68-
for f_name in glob.glob(PATH_CHEAT_SHEETS + "%s/*" % topic.rstrip('/')):
69-
answer.append(os.path.basename(f_name))
70-
topics = sorted(answer)
71-
return "\n".join(topics) + "\n"
67+
class CheatSheetsDir(CheatSheets):
68+
69+
_adapter_name = "cheat.sheets dir"
70+
_output_format = "text"
71+
72+
def _get_list(self, prefix=None):
73+
return self._update_cheat_sheets_topics()[1]
74+
75+
def _get_page(self, topic, request_options=None):
76+
answer = []
77+
for f_name in glob.glob(PATH_CHEAT_SHEETS + "%s/*" % topic.rstrip('/')):
78+
answer.append(os.path.basename(f_name))
79+
topics = sorted(answer)
80+
return "\n".join(topics) + "\n"
81+
82+
def is_found(self, topic):
83+
return CheatSheets.is_found(self, topic.rstrip('/'))

lib/adapter/cmd.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,16 @@ def _get_filenames(path):
1515
return [os.path.split(topic)[1] for topic in glob.glob(path)]
1616

1717
class Tldr(Adapter):
18-
def _get_list(self):
18+
19+
_adapter_name = "tldr"
20+
_output_format = "code"
21+
_cache_needed = True
22+
23+
def _get_list(self, prefix=None):
1924
return [filename[:-3]
2025
for filename in _get_filenames(PATH_TLDR_PAGES) if filename.endswith('.md')]
2126

22-
def get_page(self, topic, request_options=None):
27+
def _get_page(self, topic, request_options=None):
2328
cmd = ["tldr", topic]
2429
proc = Popen(cmd, stdout=PIPE, stderr=PIPE)
2530
answer = proc.communicate()[0]
@@ -40,32 +45,44 @@ def get_page(self, topic, request_options=None):
4045
return answer.decode('utf-8')
4146

4247
class Cheat(Adapter):
43-
def _get_list(self):
48+
49+
_adapter_name = "cheat"
50+
_output_format = "code"
51+
_cache_needed = True
52+
53+
def _get_list(self, prefix=None):
4454
return _get_filenames(PATH_CHEAT_PAGES)
4555

46-
def get_page(self, topic, request_options=None):
56+
def _get_page(self, topic, request_options=None):
4757
cmd = ["/usr/local/bin/cheat", topic]
48-
print(cmd)
4958
proc = Popen(cmd, stdout=PIPE, stderr=PIPE)
5059
answer = proc.communicate()[0].decode('utf-8')
51-
print("answer=%s"% answer)
5260
return answer
5361

5462
class Fosdem(Adapter):
55-
def _get_list(self):
63+
64+
_adapter_name = "fosdem"
65+
_output_format = "ansi"
66+
67+
def _get_list(self, prefix=None):
5668
return ['fosdem']
5769

58-
def get_page(self, topic, request_options=None):
70+
def _get_page(self, topic, request_options=None):
5971
cmd = ["sudo", "/usr/local/bin/current-fosdem-slide"]
6072
proc = Popen(cmd, stdout=PIPE, stderr=PIPE)
6173
answer = proc.communicate()[0].decode('utf-8')
6274
return answer
6375

6476
class Translation(Adapter):
65-
def _get_list(self):
77+
78+
_adapter_name = "translation"
79+
_output_format = "text"
80+
_cache_needed = True
81+
82+
def _get_list(self, prefix=None):
6683
return []
6784

68-
def get_page(self, topic, request_options=None):
85+
def _get_page(self, topic, request_options=None):
6986
from_, topic = topic.split('/', 1)
7087
to_ = request_options.get('lang', 'en')
7188
if '-' in from_:

lib/adapter/internal.py

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
99
from globals import MYDIR, COLOR_STYLES
10-
from colorize_internal import colorize_internal
10+
from adapter import Adapter
11+
from fmt.internal import colorize_internal
1112

1213
_INTERNAL_TOPICS = [
1314
":cht.sh",
@@ -33,9 +34,13 @@
3334
':intro',
3435
]
3536

36-
class InternalPages(object):
37+
class InternalPages(Adapter):
38+
39+
_adapter_name = 'internal'
40+
_output_format = 'ansi'
3741

3842
def __init__(self, get_topic_type=None, get_topics_list=None):
43+
Adapter.__init__(self)
3944
self.get_topic_type = get_topic_type
4045
self.get_topics_list = get_topics_list
4146

@@ -50,9 +55,8 @@ def _get_stat(self):
5055
answer += "%s %s\n" % (key, val)
5156
return answer
5257

53-
5458
@staticmethod
55-
def get_list():
59+
def get_list(prefix=None):
5660
return _INTERNAL_TOPICS
5761

5862
def _get_list_answer(self, topic, request_options=None):
@@ -70,7 +74,7 @@ def _get_list_answer(self, topic, request_options=None):
7074

7175
return answer
7276

73-
def get_page(self, topic, request_options=None):
77+
def _get_page(self, topic, request_options=None):
7478
if topic.endswith('/:list') or topic.lstrip('/') == ':list':
7579
return self._get_list_answer(topic)
7680

@@ -87,19 +91,25 @@ def get_page(self, topic, request_options=None):
8791
return answer
8892

8993
def is_found(self, topic):
90-
return topic in self.get_list()
94+
return (
95+
topic in self.get_list()
96+
or topic.endswith('/:list')
97+
)
9198

9299
class UnknownPages(InternalPages):
93100

101+
_adapter_name = 'unknown'
102+
_output_format = 'text'
103+
94104
@staticmethod
95-
def get_list():
105+
def get_list(prefix=None):
96106
return []
97107

98108
@staticmethod
99109
def is_found(topic):
100-
return False
110+
return True
101111

102-
def get_page(self, topic, request_options=None):
112+
def _get_page(self, topic, request_options=None):
103113
topics_list = self.get_topics_list()
104114
if topic.startswith(':'):
105115
topics_list = [x for x in topics_list if x.startswith(':')]
@@ -114,3 +124,16 @@ def get_page(self, topic, request_options=None):
114124
115125
%s
116126
""" % 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/latenz.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
import sys
22
import os
33
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
4-
54
from globals import PATH_LATENZ
5+
from adapter import Adapter
6+
7+
class Latenz(Adapter):
8+
9+
_adapter_name = "late.nz"
10+
_output_format = "ansi"
611

7-
def get_answer(topic, request_options=None):
8-
sys.path.append(PATH_LATENZ)
9-
import latencies
10-
return latencies.render()
12+
def _get_page(self, topic, request_options=None):
13+
sys.path.append(PATH_LATENZ)
14+
import latencies
15+
return latencies.render()
1116

12-
def get_list():
13-
return ['latencies']
17+
def _get_list(self, prefix=None):
18+
return ['latencies']
1419

15-
def is_found(topic):
16-
return topic.lower() in ['latencies', 'late.nz', 'latency']
20+
def is_found(self, topic):
21+
return topic.lower() in ['latencies', 'late.nz', 'latency']

0 commit comments

Comments
 (0)