Skip to content

get remote resource only after all cache failed #151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 1, 2021
Merged
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
27 changes: 26 additions & 1 deletion tldr.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
}


class CacheNotExist(Exception):
pass


def get_language_code(language):
language = language.split('.')[0]
if language in ['pt_PT', 'pt_BR', 'zh_TW']:
Expand Down Expand Up @@ -129,10 +133,15 @@ def get_page_url(command, platform, remote, language):
return remote + language + "/" + platform + "/" + quote(command) + ".md"


def get_page_for_platform(command, platform, remote, language):
def get_page_for_platform(command, platform, remote, language, only_use_cache=False):
data_downloaded = False
if USE_CACHE and have_recent_cache(command, platform, language):
data = load_page_from_cache(command, platform, language)
elif only_use_cache:
raise CacheNotExist("Cache for {} in {} not Found".format(
command,
platform,
))
else:
page_url = get_page_url(command, platform, remote, language)
try:
Expand Down Expand Up @@ -196,6 +205,22 @@ def get_page(command, remote=None, platforms=None, languages=None):
platforms = get_platform_list()
if languages is None:
languages = get_language_list()
# only use cache
if USE_CACHE:
for platform in platforms:
for language in languages:
if platform is None:
continue
try:
return get_page_for_platform(
command,
platform,
remote,
language,
only_use_cache=True,
)
except CacheNotExist:
continue
for platform in platforms:
for language in languages:
if platform is None:
Expand Down