@@ -79,6 +79,7 @@ def _get_writable_os_dir(self) -> str:
7979 )
8080 return os_path_join (os_path_expand_user ("~" ), CACHE_DIR )
8181
82+
8283async def write_cache (self , data : dict [str , str ], rewrite : bool = False ) -> None :
8384 """Save information to cache file atomically using aiofiles + temp file."""
8485 if not self ._initialized :
@@ -89,18 +90,18 @@ async def write_cache(self, data: dict[str, str], rewrite: bool = False) -> None
8990 current_data : dict [str , str ] = {}
9091 if not rewrite :
9192 current_data = await self .read_cache ()
92-
93+
9394 processed_keys : list [str ] = []
9495 data_to_write : list [str ] = []
95-
96+
9697 # Prepare data exactly as in original implementation
9798 for _cur_key , _cur_val in current_data .items ():
9899 _write_val = _cur_val
99100 if _cur_key in data :
100101 _write_val = data [_cur_key ]
101102 processed_keys .append (_cur_key )
102103 data_to_write .append (f"{ _cur_key } { CACHE_KEY_SEPARATOR } { _write_val } \n " )
103-
104+
104105 # Write remaining new data
105106 for _key , _value in data .items ():
106107 if _key not in processed_keys :
@@ -109,7 +110,7 @@ async def write_cache(self, data: dict[str, str], rewrite: bool = False) -> None
109110 # Atomic write using aiofiles with temporary file
110111 cache_file_path = Path (self ._cache_file )
111112 temp_path = cache_file_path .with_name (f".{ cache_file_path .name } .tmp.{ os .getpid ()} " )
112-
113+
113114 try :
114115 # Write to temporary file using aiofiles
115116 async with aiofiles_open (
@@ -119,28 +120,28 @@ async def write_cache(self, data: dict[str, str], rewrite: bool = False) -> None
119120 ) as temp_file :
120121 await temp_file .writelines (data_to_write )
121122 await temp_file .flush ()
122-
123+
123124 # Atomic rename
124- if os .name == 'nt' and cache_file_path .exists ():
125+ if os .name == "nt" and cache_file_path .exists ():
125126 cache_file_path .unlink ()
126-
127+
127128 temp_path .replace (cache_file_path )
128129 temp_path = None # Successfully renamed
129-
130+
130131 if not self ._cache_file_exists :
131132 self ._cache_file_exists = True
132-
133+
133134 _LOGGER .debug (
134- "Saved %s lines to cache file %s (aiofiles atomic write)" ,
135- str (len (data )),
136- self ._cache_file
135+ "Saved %s lines to cache file %s (aiofiles atomic write)" ,
136+ str (len (data )),
137+ self ._cache_file ,
137138 )
138-
139+
139140 except OSError as exc :
140141 _LOGGER .warning (
141- "%s while writing data to cache file %s (aiofiles atomic write)" ,
142- exc ,
143- str (self ._cache_file )
142+ "%s while writing data to cache file %s (aiofiles atomic write)" ,
143+ exc ,
144+ str (self ._cache_file ),
144145 )
145146 finally :
146147 # Cleanup on error
0 commit comments