~ubuntu-branches/ubuntu/oneiric/ubuntuone-client/oneiric

« back to all changes in this revision

Viewing changes to ubuntuone/syncdaemon/mute_filter.py

  • Committer: Bazaar Package Importer
  • Author(s): Rodney Dawes
  • Date: 2010-12-14 14:57:03 UTC
  • mto: This revision was merged to the branch mainline in revision 61.
  • Revision ID: james.westby@ubuntu.com-20101214145703-6rqksi59jbeb4xpy
Tags: upstream-1.5.1
ImportĀ upstreamĀ versionĀ 1.5.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# ubuntuone.syncdaemon.mute_filter - Mute Filter
 
2
#
 
3
# Author: Facundo Batista <facundo@canonical.com>
 
4
#
 
5
# Copyright 2009, 2010 Canonical Ltd.
 
6
#
 
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.
 
10
#
 
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.
 
15
#
 
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/>.
 
18
 
 
19
"""Class that filters and mutes some events on some paths."""
 
20
 
 
21
import logging
 
22
 
 
23
class MuteFilter(object):
 
24
    """Stores what needs to be muted."""
 
25
    def __init__(self):
 
26
        self._cnt = {}
 
27
        self.log = logging.getLogger('ubuntuone.SyncDaemon.MuteFilter')
 
28
 
 
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
 
33
 
 
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
 
38
        if new_val == 0:
 
39
            del self._cnt[element]
 
40
        else:
 
41
            self._cnt[element] = new_val
 
42
 
 
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:
 
46
            return False
 
47
 
 
48
        self._cnt[element] = self._cnt.get(element, 0) - 1
 
49
        if not self._cnt[element]:
 
50
            # reached zero
 
51
            del self._cnt[element]
 
52
 
 
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)
 
56
 
 
57
        return True