-
-
Notifications
You must be signed in to change notification settings - Fork 92
/
tldr.py
316 lines (260 loc) · 10.4 KB
/
tldr.py
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#!/usr/bin/env python
from __future__ import unicode_literals, print_function, division
import sys
import os
import errno
import subprocess
import re
from argparse import ArgumentParser
from datetime import datetime
from termcolor import colored, cprint
from six.moves.urllib.parse import quote
from six.moves.urllib.request import urlopen
from six.moves.urllib.error import HTTPError
from six.moves import map
# Required for Windows
import colorama
colorama.init()
DEFAULT_REMOTE = "http://raw.github.com/tldr-pages/tldr/master/pages"
USE_CACHE = int(os.environ.get('TLDR_CACHE_ENABLED', '1')) > 0
MAX_CACHE_AGE = int(os.environ.get('TLDR_CACHE_MAX_AGE', 24))
COMMAND_FILE_REGEX = re.compile(r'(?P<command>^.+?)_(?P<platform>.+?)\.md$')
def get_terminal_size():
def get_terminal_size_windows():
try:
from ctypes import windll, create_string_buffer
import struct
# stdin handle is -10
# stdout handle is -11
# stderr handle is -12
h = windll.kernel32.GetStdHandle(-12)
csbi = create_string_buffer(22)
res = windll.kernel32.GetConsoleScreenBufferInfo(h, csbi)
if res:
(bufx, bufy, curx, cury, wattr,
left, top, right, bottom,
maxx, maxy) = struct.unpack("hhhhHhhhhhh", csbi.raw)
sizex = right - left + 1
sizey = bottom - top + 1
return sizex, sizey
except:
pass
def get_terminal_size_stty():
try:
return map(int, subprocess.check_output(['stty', 'size']).split())
except:
pass
def get_terminal_size_tput():
try:
return map(int, [subprocess.check_output(['tput', 'lines']), subprocess.check_output(['tput', 'rows'])])
except:
pass
return get_terminal_size_windows() or get_terminal_size_stty() or get_terminal_size_tput() or (25, 80)
# get terminal size
rows, columns = get_terminal_size()
os_directories = {
"linux": "linux",
"darwin": "osx",
"sunos": "sunos"
}
def get_cache_dir():
if not os.environ.get('XDG_CACHE_HOME', False):
if not os.environ.get('HOME', False):
return os.path.join(os.path.expanduser("~"), ".cache", "tldr")
return os.path.join(os.environ.get('HOME'), '.cache', 'tldr')
return os.path.join(os.environ.get('XDG_CACHE_HOME'), 'tldr')
def get_cache_file_path(command, platform):
cache_file_name = command + "_" + platform + ".md"
cache_file_path = os.path.join(get_cache_dir(), cache_file_name)
return cache_file_path
def load_page_from_cache(command, platform):
try:
with open(get_cache_file_path(command, platform), 'rb') as cache_file:
cache_file_contents = cache_file.read()
return cache_file_contents
except Exception:
pass
def store_page_to_cache(page, command, platform):
def mkdir_p(path):
"""
Create all the intermediate directories in a path.
Similar to the `mkdir -p` command.
"""
try:
os.makedirs(path)
except OSError as exc: # Python >2.5
if exc.errno == errno.EEXIST and os.path.isdir(path):
pass
else:
raise
try:
cache_file_path = get_cache_file_path(command, platform)
mkdir_p(os.path.dirname(cache_file_path))
with open(cache_file_path, "wb") as cache_file:
cache_file.write(page)
except Exception:
pass
def have_recent_cache(command, platform):
try:
cache_file_path = get_cache_file_path(command, platform)
last_modified = datetime.fromtimestamp(os.path.getmtime(cache_file_path))
hours_passed = (datetime.now() - last_modified).total_seconds() / 3600
return hours_passed <= MAX_CACHE_AGE
except Exception:
return False
def get_page_url(platform, command, remote=None):
if remote is None:
remote = DEFAULT_REMOTE
return remote + "/" + platform + "/" + quote(command) + ".md"
def get_page_for_platform(command, platform, remote=None):
data_downloaded = False
if USE_CACHE and have_recent_cache(command, platform):
data = load_page_from_cache(command, platform)
else:
page_url = get_page_url(platform, command, remote)
try:
data = urlopen(page_url).read()
data_downloaded = True
except Exception:
data = load_page_from_cache(command, platform)
if data is None:
raise
if data_downloaded:
store_page_to_cache(data, command, platform)
return data.splitlines()
def download_and_store_page_for_platform(command, platorm, remote=None):
page_url = get_page_url(platform, command, remote)
data = urlopen(page_url).read()
store_page_to_cache(data, command, platform)
def get_platform():
for key in os_directories:
if sys.platform.startswith(key):
return os_directories[key]
def get_page(command, remote=None, platform=None):
if platform is None:
platform = ["common", get_platform()]
for _platform in platform:
if _platform is None:
continue
try:
return get_page_for_platform(command, _platform, remote=remote)
except HTTPError as e:
if e.code != 404:
raise
return False
DEFAULT_COLORS = {
'blank': 'white',
'name': 'white bold',
'description': 'white',
'example': 'green',
'command': 'red',
'parameter': 'white',
}
LEADING_SPACES_NUM = 2
command_splitter = re.compile(r'(?P<param>{{.+?}})')
param_regex = re.compile(r'(?:{{)(?P<param>.+?)(?:}})')
def colors_of(key):
env_key = 'TLDR_COLOR_%s' % key.upper()
values = os.environ.get(env_key, '').strip() or DEFAULT_COLORS[key]
values = values.split()
return (
values[0] if len(values) > 0 else None,
values[1] if len(values) > 1 and values[1].startswith('on_') else None,
values[2:] if len(values) > 1 and values[1].startswith('on_') else values[1:],
)
def output(page):
# Need a better fancy method?
if page is not None:
for line in page:
line = line.rstrip().decode('utf-8')
if len(line) < 1:
cprint(line.ljust(columns), *colors_of('blank'))
elif line[0] == '#':
cprint(line.ljust(columns), *colors_of('name'))
elif line[0] == '>':
line = ' ' + line[1:]
cprint(line.ljust(columns), *colors_of('description'))
elif line[0] == '-':
cprint(line.ljust(columns), *colors_of('example'))
elif line[0] == '`':
line = line[1:-1] # need to actually parse ``
elements = [
colored(' ' * LEADING_SPACES_NUM, *colors_of('blank')), ]
replaced_spaces = 0
for item in command_splitter.split(line):
item, replaced = param_regex.subn(
lambda x: colored(
x.group('param'), *colors_of('parameter')),
item)
if not replaced:
item = colored(item, *colors_of('command'))
else:
# In replacement of {{}} from template pattern
replaced_spaces += 4
elements.append(item)
# Manually adding painted in blank spaces
elements.append(colored(' ' * (columns
- len(line)
- LEADING_SPACES_NUM
+ replaced_spaces), *colors_of('blank')))
print(''.join(elements))
else:
cprint(line.ljust(columns), *colors_of('description'))
# Need a cleaner way to pad three colored lines
[cprint(''.ljust(columns), *colors_of('blank')) for i in range(3)]
def update_cache(remote=None):
cache_path = get_cache_dir()
if not os.path.exists(cache_path):
return
files = [file_name for file_name in os.listdir(cache_path)
if os.path.isfile(os.path.join(cache_path, file_name)) and
COMMAND_FILE_REGEX.match(file_name)]
for file_name in files:
match = COMMAND_FILE_REGEX.match(file_name)
command = match.group('command')
platform = match.group('platform')
try:
download_and_store_page_for_platform(command, platform, remote=remote)
print('Updated cache for %s (%s) from %s' % (command, platform, remote))
except Exception:
print('Error: Unable to get %s (%s) from %s' % (command, platform, remote))
def main():
parser = ArgumentParser(description="Python command line client for tldr")
parser.add_argument('-u', '--update_cache',
action='store_true',
help="Update the cached commands")
parser.add_argument('-o', '--os',
nargs=1,
default=None,
type=str,
choices=['linux', 'osx', 'sunos'],
help="Override the operating system [linux, osx, sunos]")
parser.add_argument('-s', '--source',
default=DEFAULT_REMOTE,
type=str,
help="Override the default page source")
options, other_options = parser.parse_known_args()
if options.update_cache:
update_cache(remote=options.source)
return
parser.add_argument(
'command', type=str, nargs='+', help="command to lookup")
options = parser.parse_args(other_options)
try:
result = get_page('-'.join(options.command), platform=options.os, remote=options.source)
if not result:
for command in options.command:
result = get_page(command, platform=options.os, remote=options.source)
if not result:
print((
"`{cmd}` documentation is not available"
"Consider contributing Pull Request to https://github.com/tldr-pages/tldr"
).format(cmd=command))
else:
output(result)
else:
output(result)
except Exception:
print("No internet connection detected. Please reconnect and try again.")
if __name__ == "__main__":
main()