From 08f2942beca821c2c84dfadaeaa5ed7a9eb51d44 Mon Sep 17 00:00:00 2001
From: David Lehman <dlehman@redhat.com>
Date: Tue, 12 May 2015 14:27:19 -0500
Subject: [PATCH 39/51] Rewrite the event queue's mask functionality.

---
 blivet/event.py | 55 +++++++++++++++++++++++++++----------------------------
 1 file changed, 27 insertions(+), 28 deletions(-)

diff --git a/blivet/event.py b/blivet/event.py
index 2f2ac49..ef3d8cc 100644
--- a/blivet/event.py
+++ b/blivet/event.py
@@ -72,6 +72,11 @@ class UdevEvent(Event):
     def device(self):
         return udev.device_get_name(self.info)
 
+class EventMask(util.ObjectID):
+    def __init__(self, device=None, action=None):
+        self.device = device
+        self.action = action
+
 ##
 ## EventQueue
 ##
@@ -79,37 +84,23 @@ class EventQueue(object):
     def __init__(self):
         self._queue = deque()
         self._lock = RLock()
-
-        # list of (device, action, count) tuples
         self._mask_list = []
 
     def _mask_list_match(self, event):
         """ Return True if this event should be ignored """
         match = None
-        for (device, action, count) in self._mask_list:
-            if ((device is None or event.device == device) and
-                (action is None or event.action == action)):
-                match = (device, action, count)
+        for em in self._mask_list:
+            if ((em.device is None or event.device == em.device) and
+                (em.action is None or event.action == em.action)):
+                match = em
                 break
 
         return match
 
-    def _mask_list_update(self, entry):
-        """ Update event mask after a hit at the specified index. """
-        count = entry[2]
-        if count == 1:
-            self._mask_list.remove(entry)
-        elif count > 1:
-            idx = self._mask_list.index(entry)
-            self._mask_list[idx] = (entry[0], entry[1], count - 1)
-
     def enqueue(self, event):
         with self._lock:
-            bl_entry = self._mask_list_match(event)
-            if bl_entry is None:
+            if not self._mask_list_match(event):
                 self._queue.append(event)
-            else:
-                self._mask_list_update(bl_entry)
 
     def dequeue(self):
         """ Dequeue and return the next event.
@@ -124,18 +115,24 @@ class EventQueue(object):
 
             return self._queue.popleft()
 
-    def mask_add(self, device=None, action=None, count=1):
-        """ Ignore future events.
+    def mask_add(self, device=None, action=None):
+        """ Add an event mask and return the new :class:`EventMask`.
 
             :keyword str device: ignore events on the named device
             :keyword str action: ignore events of the specified type
-            :keyword int count: number of events to ignore
 
             device of None means mask events on all devices
             action of None means mask all event types
-            count of 0 means permanently mask this device/action pair
         """
-        self._mask_list.append((device, action, count))
+        em = EventMask(device=device, action=action)
+        self._mask_list.append(em)
+        return em
+
+    def mask_remove(self, mask):
+        try:
+            self._mask_list.remove(mask)
+        except ValueError:
+            pass
 
     def __list__(self):
         return list(self._queue)
@@ -223,14 +220,16 @@ class EventManager(object):
         """ Disable monitoring and handling of events. """
         pass
 
-    def mask_event(self, device=None, action=None, count=1):
-        """ Ignore future events.
+    def mask_add(self, device=None, action=None):
+        """ Add and return a new event mask.
 
             :keyword str device: ignore events on the named device
             :keyword str action: ignore events of the specified type
-            :keyword int count: number of events to ignore
         """
-        self._queue.mask_add(device=device, action=action, count=count)
+        return self._queue.mask_add(device=device, action=action)
+
+    def mask_remove(self, mask):
+        return self._queue.mask_remove(mask)
 
     def next_event(self):
         return self._queue.dequeue()
-- 
2.1.0

