diff options
Diffstat (limited to 'contrib/python/podman/libs/__init__.py')
-rw-r--r-- | contrib/python/podman/libs/__init__.py | 53 |
1 files changed, 22 insertions, 31 deletions
diff --git a/contrib/python/podman/libs/__init__.py b/contrib/python/podman/libs/__init__.py index 7285921d7..9db5dacf5 100644 --- a/contrib/python/podman/libs/__init__.py +++ b/contrib/python/podman/libs/__init__.py @@ -1,8 +1,8 @@ """Support files for podman API implementation.""" import datetime -import threading -from dateutil.parser import parse as dateutil_parse +import functools +from dateutil.parser import parse as dateutil_parse __all__ = [ 'cached_property', @@ -11,37 +11,28 @@ __all__ = [ ] -class cached_property(object): - """cached_property() - computed once per instance, cached as attribute. +def cached_property(fn): + """Cache return to save future expense.""" + return property(functools.lru_cache(maxsize=8)(fn)) - Maybe this will make a future version of python. - """ - def __init__(self, func): - """Construct context manager.""" - self.func = func - self.__doc__ = func.__doc__ - self.lock = threading.RLock() - - def __get__(self, instance, cls=None): - """Retrieve previous value, or call func().""" - if instance is None: - return self - - attrname = self.func.__name__ - try: - cache = instance.__dict__ - except AttributeError: # objects with __slots__ have no __dict__ - msg = ("No '__dict__' attribute on {}" - " instance to cache {} property.").format( - repr(type(instance).__name__), repr(attrname)) - raise TypeError(msg) from None - - with self.lock: - # check if another thread filled cache while we awaited lock - if attrname not in cache: - cache[attrname] = self.func(instance) - return cache[attrname] +# Cannot subclass collections.UserDict, breaks varlink +class Config(dict): + """Silently ignore None values, only take key once.""" + + def __init__(self, **kwargs): + """Construct dictionary.""" + super(Config, self).__init__(kwargs) + + def __setitem__(self, key, value): + """Store unique, not None values.""" + if value is None: + return + + if super().__contains__(key): + return + + super().__setitem__(key, value) def datetime_parse(string): |