Skip to content

Commit

Permalink
Fetch individual translation archives for cache (#217) (#218)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
SaurabhDRao authored Feb 23, 2024
1 parent ffaabb1 commit 9b683be
Showing 1 changed file with 32 additions and 29 deletions.
61 changes: 32 additions & 29 deletions tldr.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,35 +377,38 @@ def output(page: str, plain: bool = False) -> None:


def update_cache(language: Optional[List[str]] = None) -> None:
if language is None:
tldr_language = os.environ.get("TLDR_LANGUAGE", get_default_language())
language = tldr_language if tldr_language else 'en'
elif isinstance(language, list):
language = language[0]
try:
pages_dir = "pages"
if language and language != 'en':
pages_dir += "." + language
req = urlopen(Request(
DOWNLOAD_CACHE_LOCATION,
headers=REQUEST_HEADERS
), context=URLOPEN_CONTEXT)
zipfile = ZipFile(BytesIO(req.read()))
pattern = re.compile(r"{}/(.+)/(.+)\.md".format(pages_dir))
cached = 0
for entry in zipfile.namelist():
match = pattern.match(entry)
if match:
store_page_to_cache(
zipfile.read(entry),
match.group(2),
match.group(1),
language
)
cached += 1
print("Updated cache for {:d} entries".format(cached))
except Exception:
sys.exit("Error: Unable to update cache from " + DOWNLOAD_CACHE_LOCATION)
languages = get_language_list()
if language and language[0] not in languages:
languages.append(language[0])
for language in languages:
try:
cache_location = f"{DOWNLOAD_CACHE_LOCATION[:-4]}-pages.{language}.zip"
req = urlopen(Request(
cache_location,
headers=REQUEST_HEADERS
), context=URLOPEN_CONTEXT)
zipfile = ZipFile(BytesIO(req.read()))
pattern = re.compile(r"(.+)/(.+)\.md")
cached = 0
for entry in zipfile.namelist():
match = pattern.match(entry)
if match:
store_page_to_cache(
zipfile.read(entry),
match.group(2),
match.group(1),
language
)
cached += 1
print(
"Updated cache for language "
f"{language}: {cached} entries"
)
except Exception:
print(
"Error: Unable to update cache for language "
f"{language} from {cache_location}"
)


def create_parser() -> ArgumentParser:
Expand Down

0 comments on commit 9b683be

Please sign in to comment.