forked from chubin/cheat.sh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheat_sheets.py
More file actions
133 lines (104 loc) · 3.78 KB
/
Copy pathcheat_sheets.py
File metadata and controls
133 lines (104 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
"""
Implementation of the adapter for the native cheat.sh cheat sheets repository,
cheat.sheets. The cheat sheets repository is hierarchically structured: cheat
sheets covering programming languages are are located in subdirectories.
"""
# pylint: disable=relative-import
import os
import glob
from .git_adapter import GitRepositoryAdapter
def _remove_initial_underscore(filename):
if filename.startswith('_'):
filename = filename[1:]
return filename
def _sanitize_dirnames(filename, restore=False):
"""
Remove (or add) leading _ in the directories names in `filename`
The `restore` param means that the path name should be restored from the queryname,
i.e. conversion should be done in the opposite direction
"""
parts = filename.split('/')
newparts = []
for part in parts[:-1]:
if restore:
newparts.append('_'+part)
continue
if part.startswith('_'):
newparts.append(part[1:])
else:
newparts.append(part)
newparts.append(parts[-1])
return "/".join(newparts)
class CheatSheets(GitRepositoryAdapter):
"""
Adapter for the cheat.sheets cheat sheets.
"""
_adapter_name = "cheat.sheets"
_output_format = "code"
_repository_url = "https://github.com/chubin/cheat.sheets"
_cheatsheet_files_prefix = "sheets/"
def _get_list(self, prefix=None):
"""
Return all files on the first and the second level,
excluding directories and hidden files
"""
hidden_files = ["_info.yaml"]
answer = []
prefix = os.path.join(
self.local_repository_location(),
self._cheatsheet_files_prefix)
for mask in ['*', '*/*']:
template = os.path.join(
prefix,
mask)
answer += [
_sanitize_dirnames(f_name[len(prefix):])
for f_name in glob.glob(template)
if not os.path.isdir(f_name)
and os.path.basename(f_name) not in hidden_files]
return sorted(answer)
def _get_page(self, topic, request_options=None):
filename = os.path.join(
self.local_repository_location(),
self._cheatsheet_files_prefix,
_sanitize_dirnames(topic, restore=True))
if os.path.exists(filename):
answer = self._format_page(open(filename, 'r').read())
else:
# though it should not happen
answer = "%s:%s not found" % (str(self.__class__), topic)
return answer
class CheatSheetsDir(CheatSheets):
"""
Adapter for the cheat sheets directories.
Provides pages named according to subdirectories:
_dir => dir/
(currently only _get_list() is used; _get_page is shadowed
by the CheatSheets adapter)
"""
_adapter_name = "cheat.sheets dir"
_output_format = "text"
def _get_list(self, prefix=None):
template = os.path.join(
self.local_repository_location(),
self._cheatsheet_files_prefix,
'*')
answer = sorted([
_remove_initial_underscore(os.path.basename(f_name)) + "/"
for f_name in glob.glob(template)
if os.path.isdir(f_name)])
return answer
def _get_page(self, topic, request_options=None):
"""
Content of the `topic` dir is the list of the pages in the dir
"""
template = os.path.join(
self.local_repository_location(),
self._cheatsheet_files_prefix,
topic.rstrip('/'),
'*')
answer = sorted([
os.path.basename(f_name) for f_name in glob.glob(template)])
return "\n".join(answer) + "\n"
def is_found(self, topic):
return CheatSheets.is_found(self, topic.rstrip('/'))