33from __future__ import annotations
44
55from asyncio import get_running_loop
6- import logging
76from contextlib import suppress
7+ import logging
88from os import getenv as os_getenv , getpid as os_getpid , name as os_name
99from os .path import expanduser as os_path_expand_user , join as os_path_join
1010from pathlib import Path
@@ -109,10 +109,11 @@ async def write_cache(self, data: dict[str, str], rewrite: bool = False) -> None
109109 data_to_write .append (f"{ _key } { CACHE_KEY_SEPARATOR } { _value } \n " )
110110
111111 # Atomic write using aiofiles with temporary file
112- cache_file_path = Path (self ._cache_file )
113- temp_path = cache_file_path .with_name (
114- f".{ cache_file_path .name } .tmp.{ os_getpid ()} "
115- )
112+ if self ._cache_file is not None :
113+ cache_file_path = Path (self ._cache_file )
114+ temp_path = cache_file_path .with_name (
115+ f".{ cache_file_path .name } .tmp.{ os_getpid ()} "
116+ )
116117
117118 try :
118119 # Write to temporary file using aiofiles
@@ -165,19 +166,21 @@ async def read_cache(self) -> dict[str, str]:
165166 self ._cache_file ,
166167 )
167168 return current_data
168- try :
169- async with aiofiles_open (
170- file = self ._cache_file ,
171- encoding = UTF8 ,
172- ) as read_file_data :
173- lines : list [str ] = await read_file_data .readlines ()
174- except OSError as exc :
175- # suppress file errors as this is expected the first time
176- # when no cache file exists yet.
177- _LOGGER .warning (
178- "OS error %s while reading cache file %s" , exc , str (self ._cache_file )
179- )
180- return current_data
169+
170+ if self ._cache_file is not None :
171+ try :
172+ async with aiofiles_open (
173+ file = self ._cache_file ,
174+ encoding = UTF8 ,
175+ ) as read_file_data :
176+ lines : list [str ] = await read_file_data .readlines ()
177+ except OSError as exc :
178+ # suppress file errors as this is expected the first time
179+ # when no cache file exists yet.
180+ _LOGGER .warning (
181+ "OS error %s while reading cache file %s" , exc , str (self ._cache_file )
182+ )
183+ return current_data
181184
182185 for line in lines :
183186 data = line .strip ()
@@ -189,6 +192,7 @@ async def read_cache(self) -> dict[str, str]:
189192 )
190193 break
191194 current_data [data [:index_separator ]] = data [index_separator + 1 :]
195+
192196 return current_data
193197
194198 async def delete_cache (self ) -> None :
0 commit comments