1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
"""
Cache Handler module.
Copyright (c) 2023 Proton AG
This file is part of Proton VPN.
Proton VPN is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Proton VPN is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with ProtonVPN. If not, see <https://www.gnu.org/licenses/>.
"""
import json
import os
from pathlib import Path
from proton.vpn import logging
logger = logging.getLogger(__name__)
class CacheHandler:
"""Used to save, load, and remove cache files."""
def __init__(self, filepath: str):
self._fp = Path(filepath)
@property
def exists(self):
"""True if the cache file exists and False otherwise."""
return self._fp.is_file()
def save(self, newdata: dict):
"""Save data to cache file."""
self._fp.parent.mkdir(parents=True, exist_ok=True)
with open(self._fp, "w", encoding="utf-8") as f: # pylint: disable=C0103
json.dump(newdata, f, indent=4) # pylint: disable=C0103
def load(self):
"""Load data from cache file, if it exists."""
if not self.exists:
return None
try:
with open(self._fp, "r", encoding="utf-8") as f: # pylint: disable=C0103
return json.load(f) # pylint: disable=C0103
except (json.decoder.JSONDecodeError, UnicodeDecodeError):
filename = os.path.basename(self._fp)
logger.warning(
msg=f"Unable to decode JSON file \"{filename}\"",
category="cache", event="load", exc_info=True
)
return None
def remove(self):
""" Remove cache from disk."""
if self.exists:
os.remove(self._fp)
|