feat: fixed cache plus concisten cache throughout musify
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Hellow
2024-04-26 01:05:22 +02:00
parent a015b8918e
commit 3d94e6e2dd
8 changed files with 38 additions and 127 deletions

View File

@@ -7,6 +7,7 @@ from functools import lru_cache
import logging
from ..utils.config import main_settings
from ..utils.string_processing import fit_to_file_system
@dataclass
@@ -63,7 +64,7 @@ class Cache:
:return: the module path
"""
r = Path(self._dir, module)
r.mkdir(exist_ok=True)
r.mkdir(exist_ok=True, parents=True)
return r
def _write_index(self, indent: int = 4):
@@ -122,13 +123,13 @@ class Cache:
)
self._write_attribute(cache_attribute)
cache_path = Path(module_path, name)
cache_path = fit_to_file_system(Path(module_path, name), hidden_ok=True)
with cache_path.open("wb") as content_file:
self.logger.debug(f"writing cache to {cache_path}")
content_file.write(content)
def get(self, name: str) -> Optional[bytes]:
path = Path(self._dir, self.module, name)
path = fit_to_file_system(Path(self._dir, self.module, name), hidden_ok=True)
if not path.is_file():
return None
@@ -148,7 +149,7 @@ class Cache:
if ca.name == "":
continue
file = Path(self._dir, ca.module, ca.name)
file = fit_to_file_system(Path(self._dir, ca.module, ca.name), hidden_ok=True)
if not ca.is_valid:
self.logger.debug(f"deleting cache {ca.id}")

View File

@@ -15,6 +15,7 @@ from tqdm import tqdm
from .cache import Cache
from .rotating import RotatingProxy
from ..objects import Target
from ..utils import request_trace
from ..utils.config import main_settings
from ..utils.support_classes.download_result import DownloadResult
from ..utils.hacking import merge_args
@@ -148,6 +149,8 @@ class Connection:
exclude_headers: List[str] = None,
**kwargs
) -> Optional[requests.Response]:
trace_string = f"{method} {url} \t{'[stream]' if kwargs.get('stream', False) else ''}"
if method is None:
raise AttributeError("method is not set.")
method = method.upper()
@@ -179,6 +182,8 @@ class Connection:
cached = self.cache.get(name)
if cached is not None:
request_trace(f"{trace_string}\t[cached]")
with responses.RequestsMock() as resp:
resp.add(
method=method,
@@ -199,6 +204,9 @@ class Connection:
if header in headers:
del headers[header]
if try_count <= 0:
request_trace(trace_string)
r = None
connection_failed = False
try: