Skip to content

Commit 260f701

Browse files
committed
Add option to create root folder for cache
1 parent 451f3ef commit 260f701

File tree

5 files changed

+18
-8
lines changed

5 files changed

+18
-8
lines changed

plugwise_usb/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,15 +269,15 @@ async def connect(self, port: str | None = None) -> None:
269269
)
270270

271271
@raise_not_connected
272-
async def initialize(self) -> None:
272+
async def initialize(self, create_root_cache_folder: bool = False) -> None:
273273
"""Initialize connection to USB-Stick."""
274274
await self._controller.initialize_stick()
275275
if self._network is None:
276276
self._network = StickNetwork(self._controller)
277277
self._network.cache_folder = self._cache_folder
278278
self._network.cache_enabled = self._cache_enabled
279279
if self._cache_enabled:
280-
await self._network.initialize_cache()
280+
await self._network.initialize_cache(create_root_cache_folder)
281281

282282
@raise_not_connected
283283
@raise_not_initialized

plugwise_usb/helpers/cache.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ def cache_root_directory(self, cache_root_dir: str = "") -> None:
4545
self._initialized = False
4646
self._root_dir = cache_root_dir
4747

48-
async def initialize_cache(self) -> None:
48+
async def initialize_cache(self, create_root_folder: bool = False) -> None:
4949
"""Set (and create) the plugwise cache directory to store cache file."""
5050
if self._root_dir != "":
51-
if not await ospath.exists(self._root_dir):
51+
if not create_root_folder and not await ospath.exists(self._root_dir):
5252
raise CacheError(f"Unable to initialize caching. Cache folder '{self._root_dir}' does not exists.")
5353
cache_dir = self._root_dir
5454
else:

plugwise_usb/network/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,11 @@ def cache_folder(self, cache_folder: str) -> None:
103103
for node in self._nodes.values():
104104
node.cache_folder = cache_folder
105105

106-
async def initialize_cache(self) -> None:
106+
async def initialize_cache(self, create_root_folder: bool = False) -> None:
107107
"""Initialize the cache folder."""
108108
if not self._cache_enabled:
109109
raise CacheError("Unable to initialize cache, enable cache first.")
110-
await self._register.initialize_cache()
110+
await self._register.initialize_cache(create_root_folder)
111111

112112
@property
113113
def controller_active(self) -> bool:

plugwise_usb/network/registry.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ def cache_enabled(self, enable: bool = True) -> None:
6363
_LOGGER.debug("Disable cache")
6464
self._cache_enabled = enable
6565

66-
async def initialize_cache(self) -> None:
66+
async def initialize_cache(self, create_root_folder: bool = False) -> None:
6767
"""Initialize cache"""
6868
if not self._cache_enabled:
6969
raise CacheError("Unable to initialize cache, enable cache first.")
70-
await self._network_cache.initialize_cache()
70+
await self._network_cache.initialize_cache(create_root_folder)
7171

7272
@property
7373
def cache_folder(self) -> str:

tests/test_usb.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,14 +1350,24 @@ async def aiofiles_os_remove(file) -> None:
13501350
return
13511351
raise pw_exceptions.CacheError("Invalid file")
13521352

1353+
async def makedirs(cache_dir, exist_ok) -> None:
1354+
if cache_dir != "non_existing_folder":
1355+
raise pw_exceptions.CacheError("wrong folder to create")
1356+
13531357
monkeypatch.setattr(pw_helpers_cache, "aiofiles_os_remove", aiofiles_os_remove)
1358+
monkeypatch.setattr(pw_helpers_cache, "makedirs", makedirs)
13541359
monkeypatch.setattr(pw_helpers_cache, "ospath", MockOsPath())
13551360

13561361
pw_cache = pw_helpers_cache.PlugwiseCache("test-file", "non_existing_folder")
13571362
assert not pw_cache.initialized
13581363
assert pw_cache.cache_root_directory == "non_existing_folder"
13591364
with pytest.raises(pw_exceptions.CacheError):
13601365
await pw_cache.initialize_cache()
1366+
assert not pw_cache.initialized
1367+
1368+
# test create folder
1369+
await pw_cache.initialize_cache(create_root_folder=True)
1370+
assert pw_cache.initialized
13611371

13621372
# Windows
13631373
pw_cache = pw_helpers_cache.PlugwiseCache("file_that_exists.ext", "mock_folder_that_exists")

0 commit comments

Comments
 (0)