aboutsummaryrefslogtreecommitdiffstats
path: root/yt_dlp/cache.py
diff options
context:
space:
mode:
authorJesús <heckyel@hyperbola.info>2022-05-17 10:10:39 +0800
committerJesús <heckyel@hyperbola.info>2022-05-17 10:10:39 +0800
commit4bbf329feb5a820ac21269fa426c95ca14d7af25 (patch)
tree2c147a162b4bddc7862ed5895f1f66edd9a675e8 /yt_dlp/cache.py
parente21342911839b7796a5c788a7c3f13b06d975c64 (diff)
parent5faf6528fb701724ac32e0a487f92281c7800bda (diff)
downloadhypervideo-pre-4bbf329feb5a820ac21269fa426c95ca14d7af25.tar.lz
hypervideo-pre-4bbf329feb5a820ac21269fa426c95ca14d7af25.tar.xz
hypervideo-pre-4bbf329feb5a820ac21269fa426c95ca14d7af25.zip
updated from upstream | 17/05/2022 at 10:10
Diffstat (limited to 'yt_dlp/cache.py')
-rw-r--r--yt_dlp/cache.py27
1 files changed, 9 insertions, 18 deletions
diff --git a/yt_dlp/cache.py b/yt_dlp/cache.py
index e5cb193bc..e3f8a7dab 100644
--- a/yt_dlp/cache.py
+++ b/yt_dlp/cache.py
@@ -1,7 +1,5 @@
-from __future__ import unicode_literals
-
+import contextlib
import errno
-import io
import json
import os
import re
@@ -9,13 +7,10 @@ import shutil
import traceback
from .compat import compat_getenv
-from .utils import (
- expand_path,
- write_json_file,
-)
+from .utils import expand_path, write_json_file
-class Cache(object):
+class Cache:
def __init__(self, ydl):
self._ydl = ydl
@@ -31,7 +26,7 @@ class Cache(object):
'invalid section %r' % section
assert re.match(r'^[a-zA-Z0-9_.-]+$', key), 'invalid key %r' % key
return os.path.join(
- self._get_root_dir(), section, '%s.%s' % (key, dtype))
+ self._get_root_dir(), section, f'{key}.{dtype}')
@property
def enabled(self):
@@ -54,8 +49,7 @@ class Cache(object):
write_json_file(data, fn)
except Exception:
tb = traceback.format_exc()
- self._ydl.report_warning(
- 'Writing cache to %r failed: %s' % (fn, tb))
+ self._ydl.report_warning(f'Writing cache to {fn!r} failed: {tb}')
def load(self, section, key, dtype='json', default=None):
assert dtype in ('json',)
@@ -64,20 +58,17 @@ class Cache(object):
return default
cache_fn = self._get_cache_fn(section, key, dtype)
- try:
+ with contextlib.suppress(OSError):
try:
- with io.open(cache_fn, 'r', encoding='utf-8') as cachef:
+ with open(cache_fn, encoding='utf-8') as cachef:
self._ydl.write_debug(f'Loading {section}.{key} from cache')
return json.load(cachef)
except ValueError:
try:
file_size = os.path.getsize(cache_fn)
- except (OSError, IOError) as oe:
+ except OSError as oe:
file_size = str(oe)
- self._ydl.report_warning(
- 'Cache retrieval from %s failed (%s)' % (cache_fn, file_size))
- except IOError:
- pass # No cache available
+ self._ydl.report_warning(f'Cache retrieval from {cache_fn} failed ({file_size})')
return default