~ubuntu-branches/ubuntu/quantal/zeitgeist/quantal

« back to all changes in this revision

Viewing changes to _zeitgeist/engine/extensions/blacklist.py

  • Committer: Package Import Robot
  • Author(s): Didier Roche
  • Date: 2011-11-15 11:15:56 UTC
  • mfrom: (1.1.13)
  • Revision ID: package-import@ubuntu.com-20111115111556-4lmc5wdvjrsdm0ss
Tags: 0.8.99~alpha1-1ubuntu1
Upload to ubuntu the new zeitgeist rewritten in vala

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -.- coding: utf-8 -.-
2
 
 
3
 
# Zeitgeist
4
 
#
5
 
# Copyright © 2009 Mikkel Kamstrup Erlandsen <mikkel.kamstrup@gmail.com>
6
 
#           © 2011 Manish Sinha <manishsinha@ubuntu.com>
7
 
#
8
 
# This program is free software: you can redistribute it and/or modify
9
 
# it under the terms of the GNU Lesser General Public License as published by
10
 
# the Free Software Foundation, either version 2.1 of the License, or
11
 
# (at your option) any later version.
12
 
#
13
 
# This program is distributed in the hope that it will be useful,
14
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 
# GNU Lesser General Public License for more details.
17
 
#
18
 
# You should have received a copy of the GNU Lesser General Public License
19
 
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 
 
21
 
import os
22
 
import json
23
 
import dbus
24
 
import dbus.service
25
 
from xdg import BaseDirectory
26
 
import logging
27
 
 
28
 
from _zeitgeist.engine.datamodel import Event
29
 
from _zeitgeist.engine.extension import Extension
30
 
from _zeitgeist.engine import constants
31
 
 
32
 
log = logging.getLogger("zeitgeist.blacklist")
33
 
 
34
 
DATA_FILE = os.path.join(constants.DATA_PATH, "blacklist.json")
35
 
BLACKLIST_DBUS_OBJECT_PATH = "/org/gnome/zeitgeist/blacklist"
36
 
BLACKLIST_DBUS_INTERFACE = "org.gnome.zeitgeist.Blacklist"
37
 
 
38
 
class Blacklist(Extension, dbus.service.Object):
39
 
        """
40
 
        The Zeitgeist engine maintains a list of event templates that is known
41
 
        as the blacklist. When inserting events via
42
 
        :meth:`org.gnome.zeitgeist.Log.InsertEvents <_zeitgeist.engine.remote.RemoteInterface.InsertEvents>`
43
 
        they will be checked against the blacklist templates and if they match
44
 
        they will not be inserted in the log, and any matching monitors will not
45
 
        be signalled.
46
 
        
47
 
        The blacklist of the Zeitgeist engine has DBus object path
48
 
        :const:`/org/gnome/zeitgeist/blacklist` under the bus name
49
 
        :const:`org.gnome.zeitgeist.Engine`.
50
 
        """
51
 
        PUBLIC_METHODS = ["add_blacklist", "remove_blacklist", "get_blacklist"]
52
 
        
53
 
        def __init__ (self, engine):
54
 
                Extension.__init__(self, engine)
55
 
                dbus.service.Object.__init__(self, dbus.SessionBus(),
56
 
                        BLACKLIST_DBUS_OBJECT_PATH)
57
 
                if os.path.exists(DATA_FILE):
58
 
                        try:
59
 
                                self._blacklist = {}
60
 
                                with open(DATA_FILE, "r") as data_file:
61
 
                                        blacklist = json.load(data_file)
62
 
                                [self._blacklist.setdefault(key, Event(blacklist[key])) \
63
 
                                        for key in blacklist]
64
 
                                log.debug("Loaded blacklist config from %s" % DATA_FILE)
65
 
                        except Exception, e:
66
 
                                log.warn("Failed to load blacklist config file %s: %s" \
67
 
                                        % (DATA_FILE, e))
68
 
                                self._blacklist = {}
69
 
                else:
70
 
                        log.debug("No existing blacklist config found")
71
 
                        self._blacklist = {}
72
 
        
73
 
        def pre_insert_event(self, event, sender):
74
 
                for tmpl in self._blacklist.itervalues():
75
 
                        if event.matches_template(tmpl): return None
76
 
                return event
77
 
        
78
 
        def _write_to_disk(self):
79
 
                with open(DATA_FILE, "w") as data_file:
80
 
                        json.dump(self._blacklist, data_file)
81
 
        
82
 
        # PUBLIC
83
 
        def add_blacklist(self, blacklist_id, event_template):
84
 
                Event._make_dbus_sendable(event_template)
85
 
                self._blacklist[blacklist_id] = Event(event_template)
86
 
                
87
 
                self._write_to_disk()
88
 
                log.debug("Blacklist added: %s" % self._blacklist)
89
 
        
90
 
        # PUBLIC
91
 
        def remove_blacklist(self, blacklist_id):
92
 
                event_template = self._blacklist[blacklist_id]
93
 
                del self._blacklist[blacklist_id]
94
 
                
95
 
                self._write_to_disk()
96
 
                log.debug("Blacklist deleted: %s" % self._blacklist)
97
 
                
98
 
                return event_template
99
 
        
100
 
        # PUBLIC
101
 
        def get_blacklist(self):
102
 
                return self._blacklist
103
 
        
104
 
        @dbus.service.method(BLACKLIST_DBUS_INTERFACE,
105
 
                             in_signature="s("+constants.SIG_EVENT+")")
106
 
        def AddTemplate(self, blacklist_id, event_template):
107
 
                """
108
 
                Adds a new :const:`event_template` to the blacklist. Events
109
 
                matching the template will be blocked from insertion into the
110
 
                log. It is still possible to find and look up events matching
111
 
                the template which were inserted before it was banned.
112
 
                
113
 
                :param blacklist_id: A string identifier for the blacklist template
114
 
                :param event_template: the :class:`Events <zeitgeist.datamodel.Event>`
115
 
                        template to be blacklisted
116
 
                """
117
 
                self.add_blacklist(blacklist_id, Event(event_template))
118
 
                self.TemplateAdded(blacklist_id, event_template)
119
 
                
120
 
        @dbus.service.method(BLACKLIST_DBUS_INTERFACE,
121
 
                             in_signature="",
122
 
                             out_signature="a{s("+constants.SIG_EVENT+")}")
123
 
        def GetTemplates(self):
124
 
                """
125
 
                Get the current list of blacklist templates.
126
 
                
127
 
                :returns: A dictionary of { string,
128
 
                        :class:`Event <zeitgeist.datamodel.Event>` }
129
 
                """
130
 
                return self.get_blacklist()
131
 
        
132
 
        @dbus.service.method(BLACKLIST_DBUS_INTERFACE,
133
 
                             in_signature="s",
134
 
                             out_signature="")
135
 
        def RemoveTemplate(self, blacklist_id):
136
 
                """
137
 
                Remove the blacklist template identified by the provided blacklist_id.
138
 
                
139
 
                :param blacklist_id: A string identifier for a blacklist template
140
 
                """
141
 
                try:
142
 
                        event_template = self.remove_blacklist(blacklist_id)
143
 
                        self.TemplateRemoved(blacklist_id, event_template)
144
 
                except KeyError:
145
 
                        log.debug('Blacklist template identified as "%s" not found.' % blacklist_id)
146
 
        
147
 
        @dbus.service.signal(BLACKLIST_DBUS_INTERFACE,
148
 
                              signature="s("+constants.SIG_EVENT+")")
149
 
        def TemplateAdded(self, blacklist_id, event_template):
150
 
                """
151
 
                This signal is emmitted when a template is added.
152
 
                
153
 
                :param blacklist_id: The Id of the newly added blacklist template
154
 
                :param event_template: The blacklist template which was added
155
 
                """
156
 
                pass
157
 
 
158
 
        @dbus.service.signal(BLACKLIST_DBUS_INTERFACE,
159
 
                              signature="s("+constants.SIG_EVENT+")")
160
 
        def TemplateRemoved(self, blacklist_id, event_template):
161
 
                """
162
 
                This signal is emmitted when a template is deleted.
163
 
                
164
 
                :param blacklist_id: The Id of the Blacklist template which was removed
165
 
                :param event_template: The blacklist template which was removed 
166
 
                """
167
 
                pass