5
5
import os
6
6
import re
7
7
from argparse import ArgumentParser
8
+ from pathlib import Path
8
9
from zipfile import ZipFile
9
10
from datetime import datetime
10
11
from io import BytesIO
@@ -78,28 +79,28 @@ def get_default_language() -> str:
78
79
return default_lang
79
80
80
81
81
- def get_cache_dir () -> str :
82
+ def get_cache_dir () -> Path :
82
83
if os .environ .get ('XDG_CACHE_HOME' , False ):
83
- return os . path . join (os .environ .get ('XDG_CACHE_HOME' ), 'tldr' )
84
+ return Path (os .environ .get ('XDG_CACHE_HOME' )) / 'tldr'
84
85
if os .environ .get ('HOME' , False ):
85
- return os . path . join (os .environ .get ('HOME' ), '.cache' , 'tldr' )
86
- return os . path . join ( os . path . expanduser ( "~" ), " .cache" , " tldr" )
86
+ return Path (os .environ .get ('HOME' )) / '.cache' / 'tldr'
87
+ return Path . home () / ' .cache' / ' tldr'
87
88
88
89
89
- def get_cache_file_path (command : str , platform : str , language : str ) -> str :
90
+ def get_cache_file_path (command : str , platform : str , language : str ) -> Path :
90
91
pages_dir = "pages"
91
92
if language and language != 'en' :
92
93
pages_dir += "." + language
93
- return os . path . join ( get_cache_dir (), pages_dir , platform , command ) + " .md"
94
+ return get_cache_dir () / pages_dir / platform / f" { command } .md"
94
95
95
96
96
97
def load_page_from_cache (command : str , platform : str , language : str ) -> Optional [str ]:
97
98
try :
98
- with open ( get_cache_file_path (
99
+ with get_cache_file_path (
99
100
command ,
100
101
platform ,
101
- language ), 'rb'
102
- ) as cache_file :
102
+ language
103
+ ). open ( 'rb' ) as cache_file :
103
104
cache_file_contents = cache_file .read ()
104
105
return cache_file_contents
105
106
except Exception :
@@ -114,8 +115,8 @@ def store_page_to_cache(
114
115
) -> Optional [str ]:
115
116
try :
116
117
cache_file_path = get_cache_file_path (command , platform , language )
117
- os . makedirs ( os . path . dirname ( cache_file_path ), exist_ok = True )
118
- with open (cache_file_path , "wb" ) as cache_file :
118
+ cache_file_path . parent . mkdir ( exist_ok = True )
119
+ with cache_file_path . open ("wb" ) as cache_file :
119
120
cache_file .write (page )
120
121
except Exception :
121
122
pass
@@ -124,7 +125,7 @@ def store_page_to_cache(
124
125
def have_recent_cache (command : str , platform : str , language : str ) -> bool :
125
126
try :
126
127
cache_file_path = get_cache_file_path (command , platform , language )
127
- last_modified = datetime .fromtimestamp (os . path . getmtime ( cache_file_path ) )
128
+ last_modified = datetime .fromtimestamp (cache_file_path . stat (). st_mtime )
128
129
hours_passed = (datetime .now () - last_modified ).total_seconds () / 3600
129
130
return hours_passed <= MAX_CACHE_AGE
130
131
except Exception :
@@ -309,12 +310,12 @@ def get_commands(platforms: Optional[List[str]] = None) -> List[str]:
309
310
platforms = get_platform_list ()
310
311
311
312
commands = []
312
- if os . path . exists ( get_cache_dir ()):
313
+ if get_cache_dir (). exists ( ):
313
314
for platform in platforms :
314
- path = os . path . join ( get_cache_dir (), 'pages' , platform )
315
- if not os . path .exists (path ):
315
+ path = get_cache_dir () / 'pages' / platform
316
+ if not path .exists ():
316
317
continue
317
- commands += [file [: - 3 ] for file in os . listdir ( path ) if file .endswith ( " .md" ) ]
318
+ commands += [file . stem for file in path . iterdir ( ) if file .suffix == ' .md' ]
318
319
return commands
319
320
320
321
@@ -513,8 +514,8 @@ def main() -> None:
513
514
print ('\n ' .join (get_commands (options .platform )))
514
515
elif options .render :
515
516
for command in options .command :
516
- if os . path . exists (command ):
517
- with open (command , encoding = 'utf-8' ) as open_file :
517
+ if Path ( command ). exists ():
518
+ with command . open (encoding = 'utf-8' ) as open_file :
518
519
output (open_file .read ().encode ('utf-8' ).splitlines (),
519
520
plain = options .markdown )
520
521
elif options .search :
0 commit comments