Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 34 additions & 26 deletions tldr.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ def load_page_from_cache(command, platform, language):
with open(get_cache_file_path(
command,
platform,
language), 'rb'
) as cache_file:
language
), 'rb') as cache_file:
cache_file_contents = cache_file.read()
return cache_file_contents
except Exception:
Expand Down Expand Up @@ -176,6 +176,7 @@ def get_language_list():


def get_page(command, remote=None, platforms=None, languages=None):
returnErr = {}
if platforms is None:
platforms = get_platform_list()
if languages is None:
Expand All @@ -185,15 +186,23 @@ def get_page(command, remote=None, platforms=None, languages=None):
if platform is None:
continue
try:
return get_page_for_platform(command, platform, remote, language)
return {
"entry": get_page_for_platform(
command,
platform,
remote,
language
),
"error": False
}
except HTTPError as err:
if err.code != 404:
raise
except URLError:
returnErr = {"entry": err, "error": True}
except URLError as err:
if not PAGES_SOURCE_LOCATION.startswith('file://'):
raise
returnErr = {"entry": err, "error": True}

return False
return returnErr


DEFAULT_COLORS = {
Expand Down Expand Up @@ -269,7 +278,7 @@ def output(page):
colored(
line.replace('>', '').replace('<', ''),
*colors_of('description')
)
)
sys.stdout.buffer.write(line.encode('utf-8'))
elif line[0] == '-':
line = '\n' + ' ' * LEADING_SPACES_NUM + \
Expand Down Expand Up @@ -405,24 +414,23 @@ def main():
with open(command, encoding='utf-8') as open_file:
output(open_file.read().encode('utf-8').splitlines())
else:
try:
command = '-'.join(options.command)
result = get_page(
command,
options.source,
options.platform,
options.language
)
if not result:
sys.exit((
"`{cmd}` documentation is not available. "
"Consider contributing Pull Request to "
"https://github.com/tldr-pages/tldr"
).format(cmd=command))
else:
output(result)
except URLError as e:
sys.exit("Error fetching from tldr: {}".format(e))
command = '-'.join(options.command)
result = get_page(
command,
options.source,
options.platform,
options.language
)
if result.get('error'):
sys.exit("Error fetching from tldr: {}".format(result['entry']))
elif result.get("entry"):
output(result['entry'])
else:
sys.exit((
"`{cmd}` documentation is not available. "
"Consider contributing Pull Request to "
"https://github.com/tldr-pages/tldr"
).format(cmd=command))


def cli():
Expand Down