@@ -81,76 +81,76 @@ def _get_writable_os_dir(self) -> str:
8181 return os_path_join (os_path_expand_user ("~" ), CACHE_DIR )
8282
8383
84- async def write_cache (self , data : dict [str , str ], rewrite : bool = False ) -> None :
85- """Save information to cache file atomically using aiofiles + temp file."""
86- if not self ._initialized :
87- raise CacheError (
88- f"Unable to save cache. Initialize cache file '{ self ._file_name } ' first."
89- )
90-
91- current_data : dict [str , str ] = {}
92- if not rewrite :
93- current_data = await self .read_cache ()
94-
95- processed_keys : list [str ] = []
96- data_to_write : list [str ] = []
97-
98- # Prepare data exactly as in original implementation
99- for _cur_key , _cur_val in current_data .items ():
100- _write_val = _cur_val
101- if _cur_key in data :
102- _write_val = data [_cur_key ]
103- processed_keys .append (_cur_key )
104- data_to_write .append (f"{ _cur_key } { CACHE_KEY_SEPARATOR } { _write_val } \n " )
105-
106- # Write remaining new data
107- for _key , _value in data .items ():
108- if _key not in processed_keys :
109- data_to_write .append (f"{ _key } { CACHE_KEY_SEPARATOR } { _value } \n " )
110-
111- # Atomic write using aiofiles with temporary file
112- cache_file_path = Path (self ._cache_file )
113- temp_path = cache_file_path .with_name (f".{ cache_file_path .name } .tmp.{ os .getpid ()} " )
114-
115- try :
116- # Write to temporary file using aiofiles
117- async with aiofiles_open (
118- file = str (temp_path ),
119- mode = "w" ,
120- encoding = UTF8 ,
121- ) as temp_file :
122- await temp_file .writelines (data_to_write )
123- await temp_file .flush ()
124-
125- # Atomic rename
126- if os .name == "nt" and cache_file_path .exists ():
127- cache_file_path .unlink ()
128-
129- temp_path .replace (cache_file_path )
130- temp_path = None # Successfully renamed
84+ async def write_cache (self , data : dict [str , str ], rewrite : bool = False ) -> None :
85+ """Save information to cache file atomically using aiofiles + temp file."""
86+ if not self ._initialized :
87+ raise CacheError (
88+ f"Unable to save cache. Initialize cache file '{ self ._file_name } ' first."
89+ )
13190
132- if not self ._cache_file_exists :
133- self ._cache_file_exists = True
134-
135- _LOGGER .debug (
136- "Saved %s lines to cache file %s (aiofiles atomic write)" ,
137- str (len (data )),
138- self ._cache_file ,
139- )
140-
141- except OSError as exc :
142- _LOGGER .warning (
143- "%s while writing data to cache file %s (aiofiles atomic write)" ,
144- exc ,
145- str (self ._cache_file ),
146- )
147- finally :
148- # Cleanup on error
149- if temp_path and temp_path .exists ():
150- try :
151- temp_path .unlink ()
152- except OSError :
153- pass
91+ current_data : dict [str , str ] = {}
92+ if not rewrite :
93+ current_data = await self .read_cache ()
94+
95+ processed_keys : list [str ] = []
96+ data_to_write : list [str ] = []
97+
98+ # Prepare data exactly as in original implementation
99+ for _cur_key , _cur_val in current_data .items ():
100+ _write_val = _cur_val
101+ if _cur_key in data :
102+ _write_val = data [_cur_key ]
103+ processed_keys .append (_cur_key )
104+ data_to_write .append (f"{ _cur_key } { CACHE_KEY_SEPARATOR } { _write_val } \n " )
105+
106+ # Write remaining new data
107+ for _key , _value in data .items ():
108+ if _key not in processed_keys :
109+ data_to_write .append (f"{ _key } { CACHE_KEY_SEPARATOR } { _value } \n " )
110+
111+ # Atomic write using aiofiles with temporary file
112+ cache_file_path = Path (self ._cache_file )
113+ temp_path = cache_file_path .with_name (f".{ cache_file_path .name } .tmp.{ os .getpid ()} " )
114+
115+ try :
116+ # Write to temporary file using aiofiles
117+ async with aiofiles_open (
118+ file = str (temp_path ),
119+ mode = "w" ,
120+ encoding = UTF8 ,
121+ ) as temp_file :
122+ await temp_file .writelines (data_to_write )
123+ await temp_file .flush ()
124+
125+ # Atomic rename
126+ if os .name == "nt" and cache_file_path .exists ():
127+ cache_file_path .unlink ()
128+
129+ temp_path .replace (cache_file_path )
130+ temp_path = None # Successfully renamed
131+
132+ if not self ._cache_file_exists :
133+ self ._cache_file_exists = True
134+
135+ _LOGGER .debug (
136+ "Saved %s lines to cache file %s (aiofiles atomic write)" ,
137+ str (len (data )),
138+ self ._cache_file ,
139+ )
140+
141+ except OSError as exc :
142+ _LOGGER .warning (
143+ "%s while writing data to cache file %s (aiofiles atomic write)" ,
144+ exc ,
145+ str (self ._cache_file ),
146+ )
147+ finally :
148+ # Cleanup on error
149+ if temp_path and temp_path .exists ():
150+ try :
151+ temp_path .unlink ()
152+ except OSError :
153+ pass
154154
155155 async def read_cache (self ) -> dict [str , str ]:
156156 """Return current data from cache file."""
0 commit comments