Skip to content

Commit 29d49a1

Browse files
author
autoruff
committed
fixup: improve-cache-writing Python code reformatted using Ruff
1 parent 87b4ab2 commit 29d49a1

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

plugwise_usb/helpers/cache.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
8283
async 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

plugwise_usb/network/cache.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ async def save_cache(self) -> None:
3030
mac: node_type.name for mac, node_type in self._nodetypes.items()
3131
}
3232
_LOGGER.debug("Save NodeTypes for %s Nodes", len(cache_data_to_save))
33-
await self.write_cache(cache_data_to_save, rewrite=True) # Make sure the cache-contents is actual
33+
await self.write_cache(
34+
cache_data_to_save, rewrite=True
35+
) # Make sure the cache-contents is actual
3436

3537
async def clear_cache(self) -> None:
3638
"""Clear current cache."""
@@ -54,7 +56,9 @@ async def restore_cache(self) -> None:
5456
node_type = None
5557

5658
if node_type is None:
57-
_LOGGER.warning("Invalid NodeType in cache for mac %s: %s", mac, node_value)
59+
_LOGGER.warning(
60+
"Invalid NodeType in cache for mac %s: %s", mac, node_value
61+
)
5862
continue
5963
self._nodetypes[mac] = node_type
6064
_LOGGER.debug(

0 commit comments

Comments
 (0)