From: Michael Tremer Date: Mon, 10 Feb 2025 15:05:30 +0000 (+0000) Subject: decorators: Drop the lazy_property decorator X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=9ee191a46f5f387e677a7e3473271f984308f6f0;p=pbs.git decorators: Drop the lazy_property decorator We don't need this to be settable any more, so therefore we can use the functools.cached_property decorator that comes with the standard distribution. Signed-off-by: Michael Tremer --- diff --git a/src/buildservice/builds.py b/src/buildservice/builds.py index a8cb5772..642a71fc 100644 --- a/src/buildservice/builds.py +++ b/src/buildservice/builds.py @@ -954,7 +954,7 @@ class Build(database.Base, database.BackendMixin, database.SoftDeleteMixin): # Monitoring Release - @lazy_property + @functools.cached_property async def monitoring_release(self): """ Returns the Monitoring Release @@ -1066,7 +1066,7 @@ class Build(database.Base, database.BackendMixin, database.SoftDeleteMixin): return False - @lazy_property + @functools.cached_property async def deprecated_by(self): if self.data.deprecated_by: return await self.backend.users.get_by_id(self.data.deprecated_by) @@ -1089,7 +1089,7 @@ class Build(database.Base, database.BackendMixin, database.SoftDeleteMixin): "Build", foreign_keys=[deprecating_build_id], ) - @lazy_property + @functools.cached_property async def deprecated_builds(self): """ Returns a list of builds that were deprecated by this build diff --git a/src/buildservice/decorators.py b/src/buildservice/decorators.py index df04425b..3df525d7 100644 --- a/src/buildservice/decorators.py +++ b/src/buildservice/decorators.py @@ -12,43 +12,3 @@ def run_in_thread(func): return await asyncio.to_thread(func, *args, **kwargs) return wrapper - -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): - property.__init__(self, fget=fget, fset=fset, fdel=fdel, doc=doc) - - # Make a cache key - self._name = "_cache_%s" % self.fget.__name__ - - def __get__(self, instance, owner): - if instance is None: - return self - - if hasattr(instance, self._name): - result = getattr(instance, self._name) - else: - if not self.fget is None: - result = self.fget(instance) - - setattr(instance, self._name, result) - - return result - - def __set__(self, instance, value): - if instance is None: - raise AttributeError - - if self.fset is None: - setattr(instance, self._name, value) - else: - self.fset(instance, value) - - # Remove any cached attributes - try: - delattr(instance, self._name) - except AttributeError: - pass diff --git a/src/buildservice/messages.py b/src/buildservice/messages.py index fb0640d7..1bea167b 100644 --- a/src/buildservice/messages.py +++ b/src/buildservice/messages.py @@ -7,6 +7,7 @@ import email.charset import email.mime.text import email.policy import email.utils +import functools import logging import re import smtplib @@ -68,7 +69,7 @@ class Messages(base.Object): def init(self): self.template_loader = tornado.template.Loader(TEMPLATESDIR, autoescape=None) - @lazy_property + @functools.cached_property def queue(self): """ The message queue diff --git a/src/buildservice/packages.py b/src/buildservice/packages.py index 621365ab..65f78422 100644 --- a/src/buildservice/packages.py +++ b/src/buildservice/packages.py @@ -398,7 +398,7 @@ class Package(database.Base, database.BackendMixin, database.SoftDeleteMixin): # Commit - @lazy_property + @functools.cached_property def commit(self): if self.data.commit_id: return self.backend.sources.get_commit_by_id(self.data.commit_id) diff --git a/src/buildservice/repos.py b/src/buildservice/repos.py index aba08ebb..c1745bb9 100644 --- a/src/buildservice/repos.py +++ b/src/buildservice/repos.py @@ -277,7 +277,7 @@ class Repo(database.Base, database.BackendMixin, database.SoftDeleteMixin): slug = Column(Text, unique=True, nullable=False) - @lazy_property + @property def path(self): parts = [] @@ -510,7 +510,7 @@ class Repo(database.Base, database.BackendMixin, database.SoftDeleteMixin): # Sources - @lazy_property + @functools.cached_property def sources(self): sources = self.backend.sources._get_sources(""" SELECT diff --git a/src/buildservice/users.py b/src/buildservice/users.py index 49a22a64..5584a64e 100644 --- a/src/buildservice/users.py +++ b/src/buildservice/users.py @@ -534,7 +534,7 @@ class User(database.Base, database.BackendMixin, database.SoftDeleteMixin): # Fetch any attributes from LDAP - @lazy_property + @functools.cached_property def attrs(self): # Use the stored attributes (only used in the test environment) #if self.data._attrs: