[compat] Add `functools.cached_property`

This commit is contained in:
pukkandan 2022-05-20 20:55:21 +05:30
parent 666c36d58d
commit 2762dbb17e
No known key found for this signature in database
GPG Key ID: 7EEE9E1E817D0A39
5 changed files with 21 additions and 7 deletions

View File

@ -10,3 +10,15 @@ try:
cache # >= 3.9 cache # >= 3.9
except NameError: except NameError:
cache = lru_cache(maxsize=None) cache = lru_cache(maxsize=None)
try:
cached_property # >= 3.8
except NameError:
class cached_property:
def __init__(self, func):
update_wrapper(self, func)
self.func = func
def __get__(self, instance, _):
setattr(instance, self.func.__name__, self.func(instance))
return getattr(instance, self.func.__name__)

View File

@ -11,6 +11,7 @@ from ..minicurses import (
MultilinePrinter, MultilinePrinter,
QuietMultilinePrinter, QuietMultilinePrinter,
) )
from ..compat import functools
from ..utils import ( from ..utils import (
NUMBER_RE, NUMBER_RE,
LockingUnsupportedError, LockingUnsupportedError,
@ -102,7 +103,7 @@ class FileDownloader:
__to_screen = to_screen __to_screen = to_screen
@property @functools.cached_property
def FD_NAME(self): def FD_NAME(self):
return re.sub(r'(?<!^)(?=[A-Z])', '_', type(self).__name__[:-2]).lower() return re.sub(r'(?<!^)(?=[A-Z])', '_', type(self).__name__[:-2]).lower()

View File

@ -5,6 +5,7 @@ import sys
import time import time
from .fragment import FragmentFD from .fragment import FragmentFD
from ..compat import functools
from ..compat import compat_setenv, compat_str from ..compat import compat_setenv, compat_str
from ..postprocessor.ffmpeg import EXT_TO_OUT_FORMATS, FFmpegPostProcessor from ..postprocessor.ffmpeg import EXT_TO_OUT_FORMATS, FFmpegPostProcessor
from ..utils import ( from ..utils import (
@ -74,7 +75,7 @@ class ExternalFD(FragmentFD):
def EXE_NAME(cls): def EXE_NAME(cls):
return cls.get_basename() return cls.get_basename()
@property @functools.cached_property
def exe(self): def exe(self):
return self.EXE_NAME return self.EXE_NAME

View File

@ -11,6 +11,7 @@ import sys
import time import time
import xml.etree.ElementTree import xml.etree.ElementTree
from ..compat import functools, re
from ..compat import ( from ..compat import (
compat_cookiejar_Cookie, compat_cookiejar_Cookie,
compat_cookies_SimpleCookie, compat_cookies_SimpleCookie,
@ -25,7 +26,6 @@ from ..compat import (
compat_urllib_parse_urlencode, compat_urllib_parse_urlencode,
compat_urllib_request, compat_urllib_request,
compat_urlparse, compat_urlparse,
re,
) )
from ..downloader import FileDownloader from ..downloader import FileDownloader
from ..downloader.f4m import get_base_url, remove_encrypted_media from ..downloader.f4m import get_base_url, remove_encrypted_media
@ -3748,7 +3748,7 @@ class InfoExtractor:
def _get_automatic_captions(self, *args, **kwargs): def _get_automatic_captions(self, *args, **kwargs):
raise NotImplementedError('This method must be implemented by subclasses') raise NotImplementedError('This method must be implemented by subclasses')
@property @functools.cached_property
def _cookies_passed(self): def _cookies_passed(self):
"""Whether cookies have been passed to YoutubeDL""" """Whether cookies have been passed to YoutubeDL"""
return self.get_param('cookiefile') is not None or self.get_param('cookiesfrombrowser') is not None return self.get_param('cookiefile') is not None or self.get_param('cookiesfrombrowser') is not None

View File

@ -2,7 +2,6 @@ import base64
import calendar import calendar
import copy import copy
import datetime import datetime
import functools
import hashlib import hashlib
import itertools import itertools
import json import json
@ -16,6 +15,7 @@ import time
import traceback import traceback
from .common import InfoExtractor, SearchInfoExtractor from .common import InfoExtractor, SearchInfoExtractor
from ..compat import functools
from ..compat import ( from ..compat import (
compat_chr, compat_chr,
compat_HTTPError, compat_HTTPError,
@ -534,7 +534,7 @@ class YoutubeBaseInfoExtractor(InfoExtractor):
args, [('VISITOR_DATA', ('INNERTUBE_CONTEXT', 'client', 'visitorData'), ('responseContext', 'visitorData'))], args, [('VISITOR_DATA', ('INNERTUBE_CONTEXT', 'client', 'visitorData'), ('responseContext', 'visitorData'))],
expected_type=str) expected_type=str)
@property @functools.cached_property
def is_authenticated(self): def is_authenticated(self):
return bool(self._generate_sapisidhash_header()) return bool(self._generate_sapisidhash_header())
@ -4402,7 +4402,7 @@ class YoutubeTabBaseInfoExtractor(YoutubeBaseInfoExtractor):
check_get_keys='contents', fatal=False, ytcfg=ytcfg, check_get_keys='contents', fatal=False, ytcfg=ytcfg,
note='Downloading API JSON with unavailable videos') note='Downloading API JSON with unavailable videos')
@property @functools.cached_property
def skip_webpage(self): def skip_webpage(self):
return 'webpage' in self._configuration_arg('skip', ie_key=YoutubeTabIE.ie_key()) return 'webpage' in self._configuration_arg('skip', ie_key=YoutubeTabIE.ie_key())