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