Skip to content

Commit daa4031

Browse files
authored
Update tldr.py : add clear_cache feature
- add clear_cache function to clear the local cache - add the parser link to the fonction
1 parent 5decc07 commit daa4031

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

tldr.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,24 @@ def update_cache(language: Optional[List[str]] = None) -> None:
489489
f"{language} from {cache_location}"
490490
)
491491

492+
def clear_cache(language: Optional[List[str]] = None) -> None:
493+
languages = get_language_list()
494+
if language and language[0] not in languages:
495+
languages.append(language[0])
496+
for language in languages:
497+
try:
498+
cache_location = f"{DOWNLOAD_CACHE_LOCATION[:-4]}-pages.{language}.zip"
499+
os.remove(cache_location)
500+
print(
501+
"Cleared cache for language "
502+
f"{language}"
503+
)
504+
except Exception:
505+
print(
506+
"Error: Unable to clear cache for language "
507+
f"{language} from {cache_location}"
508+
)
509+
492510

493511
def create_parser() -> ArgumentParser:
494512
parser = ArgumentParser(
@@ -514,6 +532,10 @@ def create_parser() -> ArgumentParser:
514532
action='store_true',
515533
help="Update the local cache of pages and exit")
516534

535+
parser.add_argument('-k', '--clear-cache',
536+
action='store_true',
537+
help="Delete the local cache of pages and exit")
538+
517539
parser.add_argument(
518540
'-p', '--platform',
519541
nargs=1,
@@ -591,6 +613,12 @@ def main() -> None:
591613
elif len(sys.argv) == 1:
592614
parser.print_help(sys.stderr)
593615
sys.exit(1)
616+
if options.clear:
617+
clear_cache()
618+
return
619+
elif len(sys.argv) == 1:
620+
parser.print_help(sys.stderr)
621+
sys.exit(1)
594622
if options.list:
595623
print('\n'.join(get_commands(options.platform, options.language)))
596624
elif options.render:

0 commit comments

Comments
 (0)