1"""Utility collections or "bricks".
2
3:module: watchdog.utils.bricks
4:author: [email protected] (Yesudeep Mangalapilly)
5:author: [email protected] (Lukáš Lalinský)
6:author: [email protected] (Raymond Hettinger)
7:author: [email protected] (Mickaël Schoentgen)
8
9Classes
10=======
11.. autoclass:: OrderedSetQueue
12   :members:
13   :show-inheritance:
14   :inherited-members:
15
16.. autoclass:: OrderedSet
17
18"""
19
20from __future__ import annotations
21
22import queue
23from typing import TYPE_CHECKING
24
25if TYPE_CHECKING:
26    from typing import Any
27
28
29class SkipRepeatsQueue(queue.Queue):
30    """Thread-safe implementation of an special queue where a
31    put of the last-item put'd will be dropped.
32
33    The implementation leverages locking already implemented in the base class
34    redefining only the primitives.
35
36    Queued items must be immutable and hashable so that they can be used
37    as dictionary keys. You must implement **only read-only properties** and
38    the :meth:`Item.__hash__()`, :meth:`Item.__eq__()`, and
39    :meth:`Item.__ne__()` methods for items to be hashable.
40
41    An example implementation follows::
42
43        class Item:
44            def __init__(self, a, b):
45                self._a = a
46                self._b = b
47
48            @property
49            def a(self):
50                return self._a
51
52            @property
53            def b(self):
54                return self._b
55
56            def _key(self):
57                return (self._a, self._b)
58
59            def __eq__(self, item):
60                return self._key() == item._key()
61
62            def __ne__(self, item):
63                return self._key() != item._key()
64
65            def __hash__(self):
66                return hash(self._key())
67
68    based on the OrderedSetQueue below
69    """
70
71    def _init(self, maxsize: int) -> None:
72        super()._init(maxsize)
73        self._last_item = None
74
75    def _put(self, item: Any) -> None:
76        if self._last_item is None or item != self._last_item:
77            super()._put(item)
78            self._last_item = item
79        else:
80            # `put` increments `unfinished_tasks` even if we did not put
81            # anything into the queue here
82            self.unfinished_tasks -= 1
83
84    def _get(self) -> Any:
85        item = super()._get()
86        if item is self._last_item:
87            self._last_item = None
88        return item
89