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

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Siegfried-Angel Gevatter Pujals
  • Date: 2011-05-07 20:45:57 UTC
  • mfrom: (1.1.9 upstream) (6.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20110507204557-31lr591fyj9ha83j
Tags: 0.8.0-1
* New upstream release. Some of the changes are:
 - Fixed secondary sorting by timestamp for most ResultTypes (LP: #772041).
 - Enabled filtering by availability in FindEvents.
 - Added event origin and current_uri properties (LP: #425258, ...) with
   corresponding ResultTypes and MoveEvent handling (LP: #602211).
 - Fixed inconsistencies caused by the internal cache not being updated when
   events were deleted (LP: #598666).
 - Added a Storage Monitor extension which tracks network connectivity and
   removable devices (LP: #489194).
 - Fixed datahub launching to avoid zombie processes (LP: #739780).
 - Replaced the Blacklist extension giving it a more capable API
   (LP: #612344).
 - Simplified log output (LP: #744818, ...).
 - Fixed bug in the connection (to Zeitgeist) recovery code (LP: #771970).
* debian/control:
 - Bump Standards-Version to 3.9.2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
# Zeitgeist
4
4
#
5
5
# Copyright © 2009 Mikkel Kamstrup Erlandsen <mikkel.kamstrup@gmail.com>
 
6
#           © 2011 Manish Sinha <manishsinha@ubuntu.com>
6
7
#
7
8
# This program is free software: you can redistribute it and/or modify
8
9
# it under the terms of the GNU Lesser General Public License as published by
18
19
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20
 
20
21
import os
21
 
import pickle
 
22
import json
22
23
import dbus
23
24
import dbus.service
24
25
from xdg import BaseDirectory
30
31
 
31
32
log = logging.getLogger("zeitgeist.blacklist")
32
33
 
33
 
CONFIG_FILE = os.path.join(constants.DATA_PATH, "blacklist.pickle")
 
34
DATA_FILE = os.path.join(constants.DATA_PATH, "blacklist.json")
34
35
BLACKLIST_DBUS_OBJECT_PATH = "/org/gnome/zeitgeist/blacklist"
35
36
BLACKLIST_DBUS_INTERFACE = "org.gnome.zeitgeist.Blacklist"
36
37
 
47
48
        :const:`/org/gnome/zeitgeist/blacklist` under the bus name
48
49
        :const:`org.gnome.zeitgeist.Engine`.
49
50
        """
50
 
        PUBLIC_METHODS = ["set_blacklist", "get_blacklist"]
 
51
        PUBLIC_METHODS = ["add_blacklist", "remove_blacklist", "get_blacklist"]
51
52
        
52
 
        def __init__ (self, engine):            
 
53
        def __init__ (self, engine):
53
54
                Extension.__init__(self, engine)
54
55
                dbus.service.Object.__init__(self, dbus.SessionBus(),
55
 
                                             BLACKLIST_DBUS_OBJECT_PATH)
56
 
                if os.path.exists(CONFIG_FILE):
 
56
                        BLACKLIST_DBUS_OBJECT_PATH)
 
57
                if os.path.exists(DATA_FILE):
57
58
                        try:
58
 
                                raw_blacklist = pickle.load(file(CONFIG_FILE))
59
 
                                self._blacklist = map(Event, raw_blacklist)
60
 
                                log.debug("Loaded blacklist config from %s"
61
 
                                          % CONFIG_FILE)
 
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)
62
65
                        except Exception, e:
63
 
                                log.warn("Failed to load blacklist config file %s: %s"\
64
 
                                         % (CONFIG_FILE, e))
65
 
                                self._blacklist = []
 
66
                                log.warn("Failed to load blacklist config file %s: %s" \
 
67
                                        % (DATA_FILE, e))
 
68
                                self._blacklist = {}
66
69
                else:
67
70
                        log.debug("No existing blacklist config found")
68
 
                        self._blacklist = []
 
71
                        self._blacklist = {}
69
72
        
70
73
        def pre_insert_event(self, event, sender):
71
 
                for tmpl in self._blacklist:
 
74
                for tmpl in self._blacklist.itervalues():
72
75
                        if event.matches_template(tmpl): return None
73
76
                return event
74
77
        
75
 
        # PUBLIC
76
 
        def set_blacklist(self, event_templates):
77
 
                self._blacklist = event_templates
78
 
                map(Event._make_dbus_sendable, self._blacklist)
79
 
                
80
 
                out = file(CONFIG_FILE, "w")
81
 
                pickle.dump(map(Event.get_plain, self._blacklist), out)         
82
 
                out.close()
83
 
                log.debug("Blacklist updated: %s" % self._blacklist)
 
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
84
99
        
85
100
        # PUBLIC
86
101
        def get_blacklist(self):
87
102
                return self._blacklist
88
103
        
89
104
        @dbus.service.method(BLACKLIST_DBUS_INTERFACE,
90
 
                             in_signature="a("+constants.SIG_EVENT+")")
91
 
        def SetBlacklist(self, event_templates):
 
105
                             in_signature="s("+constants.SIG_EVENT+")")
 
106
        def AddTemplate(self, blacklist_id, event_template):
92
107
                """
93
 
                Set the blacklist to :const:`event_templates`. Events
94
 
                matching any these templates will be blocked from insertion
95
 
                into the log. It is still possible to find and look up events
96
 
                matching the blacklist which was inserted before the blacklist
97
 
                banned them.
 
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.
98
112
                
99
 
                :param event_templates: A list of
100
 
                    :class:`Events <zeitgeist.datamodel.Event>`
 
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
101
116
                """
102
 
                tmp = map(Event, event_templates)
103
 
                self.set_blacklist(tmp)
 
117
                self.add_blacklist(blacklist_id, Event(event_template))
 
118
                self.TemplateAdded(blacklist_id, event_template)
104
119
                
105
120
        @dbus.service.method(BLACKLIST_DBUS_INTERFACE,
106
121
                             in_signature="",
107
 
                             out_signature="a("+constants.SIG_EVENT+")")
108
 
        def GetBlacklist(self):
 
122
                             out_signature="a{s("+constants.SIG_EVENT+")}")
 
123
        def GetTemplates(self):
109
124
                """
110
 
                Get the current blacklist templates.
 
125
                Get the current list of blacklist templates.
111
126
                
112
 
                :returns: A list of
113
 
                    :class:`Events <zeitgeist.datamodel.Event>`
 
127
                :returns: A dictionary of { string,
 
128
                        :class:`Event <zeitgeist.datamodel.Event>` }
114
129
                """
115
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