From f31595ef91e6f7c8b0aacfb6d6f09e7efb8ca01e Mon Sep 17 00:00:00 2001 From: Xiang Wang Date: Mon, 4 Jan 2021 17:47:01 +0800 Subject: [PATCH 1/5] get remote resource only after all cache failed --- tldr.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/tldr.py b/tldr.py index 1e936b9..0fb7221 100755 --- a/tldr.py +++ b/tldr.py @@ -46,6 +46,11 @@ } +class CacheNotExist(Exception): + + pass + + def get_language_code(language): language = language.split('.')[0] if language in ['pt_PT', 'pt_BR', 'zh_TW']: @@ -129,11 +134,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) else: + if only_use_cache: + raise CacheNotExist("Cache for {} in {} not Found".format( + command, platform + )) page_url = get_page_url(command, platform, remote, language) try: data = urlopen( @@ -196,6 +205,16 @@ 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 + 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: From f968fce1f526c7e31beb66034ba8a6223ef951dd Mon Sep 17 00:00:00 2001 From: Xiang Wang Date: Mon, 4 Jan 2021 22:39:08 +0800 Subject: [PATCH 2/5] Apply suggestions from code review * format style * change else if to elif Co-authored-by: Matthew Peveler --- tldr.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tldr.py b/tldr.py index 0fb7221..f88772a 100755 --- a/tldr.py +++ b/tldr.py @@ -212,7 +212,12 @@ def get_page(command, remote=None, platforms=None, languages=None): continue try: return get_page_for_platform( - command, platform, remote, language, only_use_cache=True) + command, + platform, + remote, + language, + only_use_cache=True, + ) except CacheNotExist: continue for platform in platforms: From 612da309c84c9c3d068e9da7f01a44db759c626a Mon Sep 17 00:00:00 2001 From: Xiang Wang Date: Mon, 4 Jan 2021 22:41:30 +0800 Subject: [PATCH 3/5] wrap USE_CACHE to avoid wasted cycles --- tldr.py | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/tldr.py b/tldr.py index f88772a..ca8f5d9 100755 --- a/tldr.py +++ b/tldr.py @@ -206,20 +206,21 @@ def get_page(command, remote=None, platforms=None, languages=None): if languages is None: languages = get_language_list() # only 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 + 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: From 12f9d0bf110a3bbf412ccf96c1c54d11a382b4ec Mon Sep 17 00:00:00 2001 From: Xiang Wang Date: Mon, 4 Jan 2021 22:50:51 +0800 Subject: [PATCH 4/5] code format * format the code style * combine the elif statement --- tldr.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tldr.py b/tldr.py index ca8f5d9..eeb3134 100755 --- a/tldr.py +++ b/tldr.py @@ -138,11 +138,12 @@ def get_page_for_platform(command, platform, remote, language, only_use_cache=Fa 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: - if only_use_cache: - raise CacheNotExist("Cache for {} in {} not Found".format( - command, platform - )) page_url = get_page_url(command, platform, remote, language) try: data = urlopen( From 4581289cc2fd713607d9997f3532cf72fd827943 Mon Sep 17 00:00:00 2001 From: Matthew Peveler Date: Sun, 31 Jan 2021 20:52:45 -0500 Subject: [PATCH 5/5] Update tldr.py --- tldr.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tldr.py b/tldr.py index eeb3134..35a6f49 100755 --- a/tldr.py +++ b/tldr.py @@ -47,7 +47,6 @@ class CacheNotExist(Exception): - pass