Skip to content

Commit 9b683be

Browse files
authored
Fetch individual translation archives for cache (#217) (#218)
* Fetch individual translation archives for cache based on the environment variable configuration (#217) * download cache for all user preferred languages * reduced chars per line to pass linter
1 parent ffaabb1 commit 9b683be

File tree

1 file changed

+32
-29
lines changed

1 file changed

+32
-29
lines changed

tldr.py

+32-29
Original file line numberDiff line numberDiff line change
@@ -377,35 +377,38 @@ def output(page: str, plain: bool = False) -> None:
377377

378378

379379
def update_cache(language: Optional[List[str]] = None) -> None:
380-
if language is None:
381-
tldr_language = os.environ.get("TLDR_LANGUAGE", get_default_language())
382-
language = tldr_language if tldr_language else 'en'
383-
elif isinstance(language, list):
384-
language = language[0]
385-
try:
386-
pages_dir = "pages"
387-
if language and language != 'en':
388-
pages_dir += "." + language
389-
req = urlopen(Request(
390-
DOWNLOAD_CACHE_LOCATION,
391-
headers=REQUEST_HEADERS
392-
), context=URLOPEN_CONTEXT)
393-
zipfile = ZipFile(BytesIO(req.read()))
394-
pattern = re.compile(r"{}/(.+)/(.+)\.md".format(pages_dir))
395-
cached = 0
396-
for entry in zipfile.namelist():
397-
match = pattern.match(entry)
398-
if match:
399-
store_page_to_cache(
400-
zipfile.read(entry),
401-
match.group(2),
402-
match.group(1),
403-
language
404-
)
405-
cached += 1
406-
print("Updated cache for {:d} entries".format(cached))
407-
except Exception:
408-
sys.exit("Error: Unable to update cache from " + DOWNLOAD_CACHE_LOCATION)
380+
languages = get_language_list()
381+
if language and language[0] not in languages:
382+
languages.append(language[0])
383+
for language in languages:
384+
try:
385+
cache_location = f"{DOWNLOAD_CACHE_LOCATION[:-4]}-pages.{language}.zip"
386+
req = urlopen(Request(
387+
cache_location,
388+
headers=REQUEST_HEADERS
389+
), context=URLOPEN_CONTEXT)
390+
zipfile = ZipFile(BytesIO(req.read()))
391+
pattern = re.compile(r"(.+)/(.+)\.md")
392+
cached = 0
393+
for entry in zipfile.namelist():
394+
match = pattern.match(entry)
395+
if match:
396+
store_page_to_cache(
397+
zipfile.read(entry),
398+
match.group(2),
399+
match.group(1),
400+
language
401+
)
402+
cached += 1
403+
print(
404+
"Updated cache for language "
405+
f"{language}: {cached} entries"
406+
)
407+
except Exception:
408+
print(
409+
"Error: Unable to update cache for language "
410+
f"{language} from {cache_location}"
411+
)
409412

410413

411414
def create_parser() -> ArgumentParser:

0 commit comments

Comments
 (0)