]> jfr.im git - yt-dlp.git/blame - yt_dlp/cache.py
[test] Convert warnings into errors
[yt-dlp.git] / yt_dlp / cache.py
CommitLineData
a0e07d31 1import errno
a0e07d31
PH
2import json
3import os
4import re
5import shutil
6import traceback
7
590bc6f6 8from .compat import compat_getenv
f8271158 9from .utils import expand_path, write_json_file
a0e07d31
PH
10
11
86e5f3ed 12class Cache:
a0e07d31
PH
13 def __init__(self, ydl):
14 self._ydl = ydl
15
16 def _get_root_dir(self):
17 res = self._ydl.params.get('cachedir')
18 if res is None:
92120217 19 cache_root = compat_getenv('XDG_CACHE_HOME', '~/.cache')
7a5c1cfe 20 res = os.path.join(cache_root, 'yt-dlp')
590bc6f6 21 return expand_path(res)
a0e07d31
PH
22
23 def _get_cache_fn(self, section, key, dtype):
674c869a
PH
24 assert re.match(r'^[a-zA-Z0-9_.-]+$', section), \
25 'invalid section %r' % section
26 assert re.match(r'^[a-zA-Z0-9_.-]+$', key), 'invalid key %r' % key
a0e07d31 27 return os.path.join(
86e5f3ed 28 self._get_root_dir(), section, f'{key}.{dtype}')
a0e07d31
PH
29
30 @property
31 def enabled(self):
32 return self._ydl.params.get('cachedir') is not False
33
34 def store(self, section, key, data, dtype='json'):
35 assert dtype in ('json',)
36
37 if not self.enabled:
38 return
39
40 fn = self._get_cache_fn(section, key, dtype)
41 try:
42 try:
43 os.makedirs(os.path.dirname(fn))
44 except OSError as ose:
45 if ose.errno != errno.EEXIST:
46 raise
e6f21b3d 47 self._ydl.write_debug(f'Saving {section}.{key} to cache')
a0e07d31
PH
48 write_json_file(data, fn)
49 except Exception:
50 tb = traceback.format_exc()
86e5f3ed 51 self._ydl.report_warning(f'Writing cache to {fn!r} failed: {tb}')
a0e07d31
PH
52
53 def load(self, section, key, dtype='json', default=None):
54 assert dtype in ('json',)
55
56 if not self.enabled:
57 return default
58
59 cache_fn = self._get_cache_fn(section, key, dtype)
60 try:
61 try:
86e5f3ed 62 with open(cache_fn, encoding='utf-8') as cachef:
e6f21b3d 63 self._ydl.write_debug(f'Loading {section}.{key} from cache')
a0e07d31
PH
64 return json.load(cachef)
65 except ValueError:
66 try:
67 file_size = os.path.getsize(cache_fn)
86e5f3ed 68 except OSError as oe:
a0e07d31 69 file_size = str(oe)
86e5f3ed 70 self._ydl.report_warning(f'Cache retrieval from {cache_fn} failed ({file_size})')
71 except OSError:
a0e07d31
PH
72 pass # No cache available
73
74 return default
75
76 def remove(self):
77 if not self.enabled:
78 self._ydl.to_screen('Cache is disabled (Did you combine --no-cache-dir and --rm-cache-dir?)')
79 return
80
81 cachedir = self._get_root_dir()
82 if not any((term in cachedir) for term in ('cache', 'tmp')):
83 raise Exception('Not removing directory %s - this does not look like a cache dir' % cachedir)
84
85 self._ydl.to_screen(
86 'Removing cache dir %s .' % cachedir, skip_eol=True)
87 if os.path.exists(cachedir):
88 self._ydl.to_screen('.', skip_eol=True)
89 shutil.rmtree(cachedir)
90 self._ydl.to_screen('.')