[PATCH osinfo-db 09/10] tests: osinfo: Caching all XML property lookups

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On its own this doesn't save much time, but offers significan time
savings when combined with the next patch

Signed-off-by: Cole Robinson <crobinso@xxxxxxxxxx>
---
 tests/osinfo.py | 47 +++++++++++++++++++++++++++++++++--------------
 1 file changed, 33 insertions(+), 14 deletions(-)

diff --git a/tests/osinfo.py b/tests/osinfo.py
index 0e0eb3d..295bac3 100644
--- a/tests/osinfo.py
+++ b/tests/osinfo.py
@@ -7,83 +7,101 @@ import logging
 import requests
 
 
+def _cache_property(fn):
+    """
+    Decorator to use self._cache to cache property lookup results
+    """
+    def _wrapper(*args):
+        self = args[0]
+        key = str(fn)
+        cache = self._cache  # pylint: disable=protected-access
+        if key not in cache:
+            cache[key] = fn(*args)
+        return cache[key]
+    return property(_wrapper)
+
+
 class Os():
     def __init__(self, root):
         self._root = root
+        self._cache = {}
 
     def _get_images(self):
         images = []
         for image in self._root.findall('image'):
             images.append(Image(image))
         return images
-    images = property(_get_images)
+    images = _cache_property(_get_images)
 
     def _get_medias(self):
         medias = []
         for media in self._root.findall('media'):
             medias.append(Media(media))
         return medias
-    medias = property(_get_medias)
+    medias = _cache_property(_get_medias)
 
     def _get_trees(self):
         trees = []
         for tree in self._root.findall('tree'):
             trees.append(Tree(tree))
         return trees
-    trees = property(_get_trees)
+    trees = _cache_property(_get_trees)
 
     def _get_shortid(self):
         shortid = self._root.find('short-id')
         return shortid.text
-    shortid = property(_get_shortid)
+    shortid = _cache_property(_get_shortid)
 
     def _get_distro(self):
         distro = self._root.find('distro')
         return distro.text
-    distro = property(_get_distro)
+    distro = _cache_property(_get_distro)
 
 
 class Image():
     def __init__(self, root):
         self._root = root
+        self._cache = {}
 
     def _get_url(self):
         url = self._root.find('url')
         if url is not None:
             return URL(url.text)
         return None
-    url = property(_get_url)
+    url = _cache_property(_get_url)
 
 
 class Media():
     def __init__(self, root):
         self._root = root
+        self._cache = {}
 
     def _get_url(self):
         url = self._root.find('url')
         if url is not None:
             return URL(url.text)
         return None
-    url = property(_get_url)
+    url = _cache_property(_get_url)
 
     def _get_iso(self):
         iso = self._root.find('iso')
         if iso is not None:
             return ISO(iso)
         return None
-    iso = property(_get_iso)
+    iso = _cache_property(_get_iso)
 
 
 class Tree():
     def __init__(self, root):
         self._root = root
+        self._cache = {}
 
     def _get_url(self):
         url = self._root.find('url')
         if url is not None:
             return URL(url.text)
         return None
-    url = property(_get_url)
+    url = _cache_property(_get_url)
 
 
 class URL():
@@ -101,6 +119,7 @@ class URL():
 class ISO():
     def __init__(self, root):
         self._root = root
+        self._cache = {}
 
     def _get_value(self, name, return_type=str, default=''):
         entry = self._root.find(name)
@@ -108,20 +127,20 @@ class ISO():
 
     def _get_volumeid(self):
         return self._get_value('volume-id')
-    volumeid = property(_get_volumeid)
+    volumeid = _cache_property(_get_volumeid)
 
     def _get_publisherid(self):
         return self._get_value('publisher-id')
-    publisherid = property(_get_publisherid)
+    publisherid = _cache_property(_get_publisherid)
 
     def _get_applicationid(self):
         return self._get_value('application-id')
-    applicationid = property(_get_applicationid)
+    applicationid = _cache_property(_get_applicationid)
 
     def _get_systemid(self):
         return self._get_value('system-id')
-    systemid = property(_get_systemid)
+    systemid = _cache_property(_get_systemid)
 
     def _get_volumesize(self):
         return self._get_value('volume-size', int, 0)
-    volumesize = property(_get_volumesize)
+    volumesize = _cache_property(_get_volumesize)
-- 
2.21.0

_______________________________________________
Libosinfo mailing list
Libosinfo@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libosinfo



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Fedora Users]     [Fedora Maintainers]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]

  Powered by Linux