Commit 43c9db6d authored by Mattia Rizzolo's avatar Mattia Rizzolo
Browse files

rblib/modles: add from_row() methods to Build and Package



to be able to instantiate those objects providing some rows coming from
the db.
Useful for those cases where the database has to be queried anyway to
gather the list of packages, at that point one might as well gather all
the required data already.

Signed-off-by: Mattia Rizzolo's avatarMattia Rizzolo <mattia@debian.org>
parent 84bc8d95
...@@ -139,6 +139,18 @@ class Build: ...@@ -139,6 +139,18 @@ class Build:
def __bool__(self): def __bool__(self):
return bool(self.status) return bool(self.status)
@classmethod
def from_row(cls, row):
"""
Instantiate a Build object already populed. The row needs to be:
(pkgname, suite, arch, status, version, build_date)
"""
build = cls(row[0], row[1], row[2])
build._l_status = row[3]
build._l_version = row[4]
build._l_build_date = str(row[5]) + ' UTC'
return build
@lazyproperty @lazyproperty
def status(self): def status(self):
self._get_package_status() self._get_package_status()
...@@ -224,12 +236,35 @@ class _Package_cache: ...@@ -224,12 +236,35 @@ class _Package_cache:
self._cache[pkgname] = {} self._cache[pkgname] = {}
return self._cache[pkgname] return self._cache[pkgname]
def contains(self, pkgname):
return pkgname in self._cache
class Package: class Package:
def __init__(self, name, no_notes=False): def __init__(self, name, no_notes=False):
self.__dict__ = _Package_cache().get(name) self.__dict__ = _Package_cache().get(name)
self.name = name self.name = name
@classmethod
def from_row(cls, name, rows):
"""
Instantiate a Build object already populed. `rows` is an iterable of
tuples that need to be:
(suite, arch, status, version, build_date)
It's not possible to instantiate a new object if there is one already
cached for the same package, this method will return that one instead.
Further builds cannot be added later and will cause KeyError.
"""
if _Package_cache.contains(name):
pkg = cls(name) # will pick up the cached object
else:
pkg = cls(name)
pkg._l_builds = {s: {} for s in SUITES}
for r in rows:
b = Build.from_row((pkg, *r))
pkg._l_builds[b.suite][b.arch] = b
return pkg
@lazyproperty @lazyproperty
def builds(self): def builds(self):
self._l_builds = {} self._l_builds = {}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment