This repository has been archived on 2020-11-22. You can view files and clone it, but cannot push or open issues or pull requests.
mangareader/disk_cache.py

34 lines
848 B
Python
Raw Normal View History

2019-08-07 22:35:22 +02:00
from io import BytesIO
import os
class DiskCache:
def __init__(self, path=None, enabled=True):
if path is None:
self.path = os.path.join(os.path.dirname(__file__), 'cache')
else:
self.path = path
self.enabled = enabled
if self.enabled is False:
return
os.makedirs(self.path, exist_ok=True)
def set(self, identifier, buf):
if self.enabled is False:
return
with open(self._build_path(identifier), 'wb') as f:
f.write(buf.read())
def get(self, identifier):
if self.enabled is False:
raise FileNotFoundError
with open(self._build_path(identifier), 'rb') as f:
return BytesIO(f.read())
def _build_path(self, identifier):
return os.path.join(self.path, identifier)