[2/4] framework: Add filters class

Submitted by Dylan Baker on May 8, 2018, 9:22 p.m.

Details

Message ID 20180508212207.13889-2-dylan@pnwbakers.com
State New
Headers show
Series "Series without cover letter" ( rev: 1 ) in Piglit

Not browsing as part of any series.

Commit Message

Dylan Baker May 8, 2018, 9:22 p.m.
This class works like a list, but with an extra method that allows it to
"reset" a filter. This will allow class based filters that need to be
reset to have deterministic behavior to do so.
---
 framework/profile.py | 42 +++++++++++++++++++++++++++++++++++++++---
 1 file changed, 39 insertions(+), 3 deletions(-)

Patch hide | download patch | download mbox

diff --git a/framework/profile.py b/framework/profile.py
index 44681592a..b0233866b 100644
--- a/framework/profile.py
+++ b/framework/profile.py
@@ -265,6 +265,42 @@  class TestDict(collections.MutableMapping):
         self.__allow_reassignment -= 1
 
 
+class Filters(collections.MutableSequence):
+
+    def __init__(self, iterable=None):
+        if iterable:
+            self.__container = list(iterable)
+        else:
+            self.__container = []
+
+    def __getitem__(self, index):
+        return self.__container[index]
+
+    def __setitem__(self, index, value):
+        self.__container[index] = value
+
+    def __delitem__(self, index):
+        del self.__container[index]
+
+    def __len__(self):
+        return len(self.__container)
+
+    def __add__(self, other):
+        return type(self)(itertools.chain(iter(self), iter(other)))
+
+    def insert(self, index, value):
+        self.__container.insert(index, value)
+
+    def run(self, iterable):
+        for f in self.__container:
+            if hasattr(f, 'reset'):
+                f.reset()
+
+        for k, v in iterable:
+            if all(f(k, v) for f in self.__container):
+                yield k, v
+
+
 def make_test(element):
     """Rebuild a test instance from xml."""
     def process(elem, opt):
@@ -309,7 +345,7 @@  class XMLProfile(object):
     def __init__(self, filename):
         self.filename = filename
         self.forced_test_list = []
-        self.filters = []
+        self.filters = Filters()
         self.options = {
             'dmesg': get_dmesg(False),
             'monitor': Monitoring(False),
@@ -369,7 +405,7 @@  class MetaProfile(object):
 
     def __init__(self, filename):
         self.forced_test_list = []
-        self.filters = []
+        self.filters = Filters()
         self.options = {
             'dmesg': get_dmesg(False),
             'monitor': Monitoring(False),
@@ -436,7 +472,7 @@  class TestProfile(object):
     def __init__(self):
         self.test_list = TestDict()
         self.forced_test_list = []
-        self.filters = []
+        self.filters = Filters()
         self.options = {
             'dmesg': get_dmesg(False),
             'monitor': Monitoring(False),