kbr / fritzconnection

Python-Tool to communicate with the AVM Fritz!Box by the TR-064 protocol and the AHA-HTTP-Interface
MIT License
303 stars 59 forks source link

Ensure directory exits when writing cache #183

Closed sonnenscheinchen closed 1 year ago

sonnenscheinchen commented 1 year ago

Making a FritzConnection() with use_cache=True fails on a new installation because of non existing cache-directory:


>>> from fritzconnection import FritzConnection
>>> fc = FritzConnection(address="192.168.178.1", use_cache=True)
Traceback (most recent call last):
  File "/home/stw/.local/lib/python3.10/site-packages/fritzconnection/core/fritzconnection.py", line 454, in _load_router_api
    self._load_api_from_cache(path, cache_format)
  File "/home/stw/.local/lib/python3.10/site-packages/fritzconnection/core/fritzconnection.py", line 533, in _load_api_from_cache
    with open(path, mode) as fobj:
FileNotFoundError: [Errno 2] No such file or directory: '/home/stw/.fritzconnection/192_168_178_1_cache.pcl'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/stw/.local/lib/python3.10/site-packages/fritzconnection/core/fritzconnection.py", line 246, in __init__
    self._load_router_api(
  File "/home/stw/.local/lib/python3.10/site-packages/fritzconnection/core/fritzconnection.py", line 457, in _load_router_api
    reload_api()
  File "/home/stw/.local/lib/python3.10/site-packages/fritzconnection/core/fritzconnection.py", line 449, in reload_api
    self._write_api_to_cache(path, cache_format)
  File "/home/stw/.local/lib/python3.10/site-packages/fritzconnection/core/fritzconnection.py", line 515, in _write_api_to_cache
    with open(path, mode) as fobj:
FileNotFoundError: [Errno 2] No such file or directory: '/home/stw/.fritzconnection/192_168_178_1_cache.pcl'
kbr commented 1 year ago

Thanks for the report. This issue was not covered by the tests because of a dedicated directory. However the path already is a Path-instance and there is no need to use the more low-level os-module. Also it's better to make the check for the cache-directory in the _get_cache_path method.

Because it was just a minor change I have applied and commited the code. Will make it as bugfix into the upcoming 1.12 version.

Thanks again.