1
# ubuntuone.syncdaemon.mute_filter - Mute Filter
3
# Author: Facundo Batista <facundo@canonical.com>
5
# Copyright 2009, 2010 Canonical Ltd.
7
# This program is free software: you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License version 3, as published
9
# by the Free Software Foundation.
11
# This program is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranties of
13
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
14
# PURPOSE. See the GNU General Public License for more details.
16
# You should have received a copy of the GNU General Public License along
17
# with this program. If not, see <http://www.gnu.org/licenses/>.
19
"""Class that filters and mutes some events on some paths."""
23
class MuteFilter(object):
24
"""Stores what needs to be muted."""
27
self.log = logging.getLogger('ubuntuone.SyncDaemon.MuteFilter')
29
def add(self, element):
30
"""Add and element to the filter."""
31
self.log.debug("Adding: %s", element)
32
self._cnt[element] = self._cnt.get(element, 0) + 1
34
def rm(self, element):
35
"""Remove an element from the filter."""
36
self.log.debug("Removing: %s", element)
37
new_val = self._cnt[element] - 1
39
del self._cnt[element]
41
self._cnt[element] = new_val
43
def pop(self, element):
44
"""Pops an element from the filter, if there, and returns if it was."""
45
if element not in self._cnt:
48
self._cnt[element] = self._cnt.get(element, 0) - 1
49
if not self._cnt[element]:
51
del self._cnt[element]
53
# log what happened and how many items we have left
54
q = sum(self._cnt.itervalues())
55
self.log.debug("Blocking %s (%d left)", element, q)