]> git.ipfire.org Git - ipfire.org.git/commitdiff
Add decorators
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 10 Oct 2018 11:14:57 +0000 (12:14 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 10 Oct 2018 11:14:57 +0000 (12:14 +0100)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/backend/decorators.py [new file with mode: 0644]

index 7b043ab61fb5248b6c5fe83546479534b614f91b..678cdff2c7311d6ca73cc4cf960f461554915146 100644 (file)
@@ -53,6 +53,7 @@ backend_PYTHON = \
        src/backend/blog.py \
        src/backend/countries.py \
        src/backend/database.py \
+       src/backend/decorators.py \
        src/backend/fireinfo.py \
        src/backend/geoip.py \
        src/backend/iuse.py \
diff --git a/src/backend/decorators.py b/src/backend/decorators.py
new file mode 100644 (file)
index 0000000..f271daf
--- /dev/null
@@ -0,0 +1,33 @@
+#!/usr/bin/python3
+
+class Missing(object):
+       pass
+
+_missing = Missing()
+
+class lazy_property(property):
+       """
+               The property is only computed once and then being
+               cached until the end of the lifetime of the object.
+       """
+       def __init__(self, fget, fset=None, fdel=None, doc=None, name=None):
+               property.__init__(self, fget=fget, fset=fset, fdel=fdel, doc=doc)
+
+               self.__name__   = name or fget.__name__
+               self.__module__ = fget.__module__
+
+       def __get__(self, obj, type=None):
+               if object is None:
+                       return self
+
+               value = obj.__dict__.get(self.__name__, _missing)
+               if value is _missing:
+                       obj.__dict__[self.__name__] = value = self.fget(obj)
+
+               return value
+
+       def __set__(self, obj, value):
+               if self.fset:
+                       self.fset(obj, value)
+
+               obj.__dict__[self.__name__] = value