diff --git a/sigal/gallery.py b/sigal/gallery.py index ed76ae9..f91cc5e 100644 --- a/sigal/gallery.py +++ b/sigal/gallery.py @@ -147,16 +147,16 @@ class Image(Media): type = 'image' extensions = ('.jpg', '.jpeg', '.png') - @cached_property() + @cached_property def date(self): return self.exif and self.exif.get('dateobj', None) or None - @cached_property() + @cached_property def exif(self): return (get_exif_tags(self.raw_exif) if self.raw_exif and self.ext in ('.jpg', '.jpeg') else None) - @cached_property() + @cached_property def raw_exif(self): try: return (get_exif_data(self.src_path) diff --git a/sigal/utils.py b/sigal/utils.py index 3d3bff8..6bca14f 100644 --- a/sigal/utils.py +++ b/sigal/utils.py @@ -99,36 +99,21 @@ def call_subprocess(cmd): class cached_property(object): - '''Decorator for read-only properties evaluated only once. - - © 2011 Christopher Arndt, MIT License - https://wiki.python.org/moin/PythonDecoratorLibrary#Cached_Properties - - The value is cached in the '_cache' attribute of the object instance that - has the property getter method wrapped by this decorator. The '_cache' - attribute value is a dictionary which has a key for every property of the - object which is wrapped by this decorator. Each entry in the cache is - created only when the property is accessed for the first time and is a - two-element tuple with the last computed property value and the last time - it was updated in seconds since the epoch. - - ''' - - def __call__(self, fget, doc=None): - self.fget = fget - self.__doc__ = doc or fget.__doc__ - self.__name__ = fget.__name__ - self.__module__ = fget.__module__ - return self - - def __get__(self, inst, owner): - try: - value = inst._cache[self.__name__] - except (KeyError, AttributeError): - value = self.fget(inst) - try: - cache = inst._cache - except AttributeError: - cache = inst._cache = {} - cache[self.__name__] = value + """ A property that is only computed once per instance and then replaces + itself with an ordinary attribute. Deleting the attribute resets the + property. + Source: + https://github.com/pydanny/cached-property (BSD Licensed) + https://github.com/bottlepy/bottle/commit/fa7733e075da0d790d809aa3d2f53071897e6f76 + + """ + + def __init__(self, func): + self.__doc__ = getattr(func, '__doc__') + self.func = func + + def __get__(self, obj, cls): + if obj is None: + return self + value = obj.__dict__[self.func.__name__] = self.func(obj) return value