Skip to content

Commit 20624de

Browse files
ramwinMasterOdin
andauthored
get remote resource only after all cache failed (#151)
Co-authored-by: Matthew Peveler <[email protected]>
1 parent daf1ebf commit 20624de

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

tldr.py

+26-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@
4646
}
4747

4848

49+
class CacheNotExist(Exception):
50+
pass
51+
52+
4953
def get_language_code(language):
5054
language = language.split('.')[0]
5155
if language in ['pt_PT', 'pt_BR', 'zh_TW']:
@@ -129,10 +133,15 @@ def get_page_url(command, platform, remote, language):
129133
return remote + language + "/" + platform + "/" + quote(command) + ".md"
130134

131135

132-
def get_page_for_platform(command, platform, remote, language):
136+
def get_page_for_platform(command, platform, remote, language, only_use_cache=False):
133137
data_downloaded = False
134138
if USE_CACHE and have_recent_cache(command, platform, language):
135139
data = load_page_from_cache(command, platform, language)
140+
elif only_use_cache:
141+
raise CacheNotExist("Cache for {} in {} not Found".format(
142+
command,
143+
platform,
144+
))
136145
else:
137146
page_url = get_page_url(command, platform, remote, language)
138147
try:
@@ -196,6 +205,22 @@ def get_page(command, remote=None, platforms=None, languages=None):
196205
platforms = get_platform_list()
197206
if languages is None:
198207
languages = get_language_list()
208+
# only use cache
209+
if USE_CACHE:
210+
for platform in platforms:
211+
for language in languages:
212+
if platform is None:
213+
continue
214+
try:
215+
return get_page_for_platform(
216+
command,
217+
platform,
218+
remote,
219+
language,
220+
only_use_cache=True,
221+
)
222+
except CacheNotExist:
223+
continue
199224
for platform in platforms:
200225
for language in languages:
201226
if platform is None:

0 commit comments

Comments
 (0)