diff --git a/youtube_dl/utils.py b/youtube_dl/utils.py index 443d2609c..ddec962f4 100644 --- a/youtube_dl/utils.py +++ b/youtube_dl/utils.py @@ -6510,3 +6510,33 @@ def join_nonempty(*values, **kwargs): if from_dict is not None: values = (traverse_obj(from_dict, variadic(v)) for v in values) return delim.join(map(compat_str, filter(None, values))) + + +# from yt-dlp +class classproperty(object): + """property access for class methods with optional caching""" + def __new__(cls, *args, **kwargs): + if 'func' in kwargs: + func = kwargs.pop('func') + elif len(args) > 0: + func = args[0] + args = args[1:] + else: + func = None + if not func: + return functools.partial(cls, *args, **kwargs) + return super(classproperty, cls).__new__(cls) + + def __init__(self, func, **kwargs): + # kw-only arg + cache = kwargs.get('cache', False) + functools.update_wrapper(self, func) + self.func = func + self._cache = {} if cache else None + + def __get__(self, n, cls): + if self._cache is None: + return self.func(cls) + elif cls not in self._cache: + self._cache[cls] = self.func(cls) + return self._cache[cls]