aboutsummaryrefslogtreecommitdiffstats
path: root/hypervideo_dl/cache.py
diff options
context:
space:
mode:
Diffstat (limited to 'hypervideo_dl/cache.py')
-rw-r--r--hypervideo_dl/cache.py16
1 files changed, 5 insertions, 11 deletions
diff --git a/hypervideo_dl/cache.py b/hypervideo_dl/cache.py
index 2e9c1ef..fa72814 100644
--- a/hypervideo_dl/cache.py
+++ b/hypervideo_dl/cache.py
@@ -1,10 +1,10 @@
import contextlib
-import errno
import json
import os
import re
import shutil
import traceback
+import urllib.parse
from .utils import expand_path, traverse_obj, version_tuple, write_json_file
from .version import __version__
@@ -22,11 +22,9 @@ class Cache:
return expand_path(res)
def _get_cache_fn(self, section, key, dtype):
- assert re.match(r'^[a-zA-Z0-9_.-]+$', section), \
- '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, f'{key}.{dtype}')
+ assert re.match(r'^[\w.-]+$', section), f'invalid section {section!r}'
+ key = urllib.parse.quote(key, safe='').replace('%', ',') # encode non-ascii characters
+ return os.path.join(self._get_root_dir(), section, f'{key}.{dtype}')
@property
def enabled(self):
@@ -40,11 +38,7 @@ class Cache:
fn = self._get_cache_fn(section, key, dtype)
try:
- try:
- os.makedirs(os.path.dirname(fn))
- except OSError as ose:
- if ose.errno != errno.EEXIST:
- raise
+ os.makedirs(os.path.dirname(fn), exist_ok=True)
self._ydl.write_debug(f'Saving {section}.{key} to cache')
write_json_file({'hypervideo_version': __version__, 'data': data}, fn)
except Exception: