47
48
:const:`/org/gnome/zeitgeist/blacklist` under the bus name
48
49
:const:`org.gnome.zeitgeist.Engine`.
50
PUBLIC_METHODS = ["set_blacklist", "get_blacklist"]
51
PUBLIC_METHODS = ["add_blacklist", "remove_blacklist", "get_blacklist"]
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):
58
raw_blacklist = pickle.load(file(CONFIG_FILE))
59
self._blacklist = map(Event, raw_blacklist)
60
log.debug("Loaded blacklist config from %s"
60
with open(DATA_FILE, "r") as data_file:
61
blacklist = json.load(data_file)
62
[self._blacklist.setdefault(key, Event(blacklist[key])) \
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"\
66
log.warn("Failed to load blacklist config file %s: %s" \
67
70
log.debug("No existing blacklist config found")
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
76
def set_blacklist(self, event_templates):
77
self._blacklist = event_templates
78
map(Event._make_dbus_sendable, self._blacklist)
80
out = file(CONFIG_FILE, "w")
81
pickle.dump(map(Event.get_plain, self._blacklist), out)
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)
83
def add_blacklist(self, blacklist_id, event_template):
84
Event._make_dbus_sendable(event_template)
85
self._blacklist[blacklist_id] = Event(event_template)
88
log.debug("Blacklist added: %s" % self._blacklist)
91
def remove_blacklist(self, blacklist_id):
92
event_template = self._blacklist[blacklist_id]
93
del self._blacklist[blacklist_id]
96
log.debug("Blacklist deleted: %s" % self._blacklist)
86
101
def get_blacklist(self):
87
102
return self._blacklist
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):
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
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.
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
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)
105
120
@dbus.service.method(BLACKLIST_DBUS_INTERFACE,
107
out_signature="a("+constants.SIG_EVENT+")")
108
def GetBlacklist(self):
122
out_signature="a{s("+constants.SIG_EVENT+")}")
123
def GetTemplates(self):
110
Get the current blacklist templates.
125
Get the current list of blacklist templates.
113
:class:`Events <zeitgeist.datamodel.Event>`
127
:returns: A dictionary of { string,
128
:class:`Event <zeitgeist.datamodel.Event>` }
115
130
return self.get_blacklist()
132
@dbus.service.method(BLACKLIST_DBUS_INTERFACE,
135
def RemoveTemplate(self, blacklist_id):
137
Remove the blacklist template identified by the provided blacklist_id.
139
:param blacklist_id: A string identifier for a blacklist template
142
event_template = self.remove_blacklist(blacklist_id)
143
self.TemplateRemoved(blacklist_id, event_template)
145
log.debug('Blacklist template identified as "%s" not found.' % blacklist_id)
147
@dbus.service.signal(BLACKLIST_DBUS_INTERFACE,
148
signature="s("+constants.SIG_EVENT+")")
149
def TemplateAdded(self, blacklist_id, event_template):
151
This signal is emmitted when a template is added.
153
:param blacklist_id: The Id of the newly added blacklist template
154
:param event_template: The blacklist template which was added
158
@dbus.service.signal(BLACKLIST_DBUS_INTERFACE,
159
signature="s("+constants.SIG_EVENT+")")
160
def TemplateRemoved(self, blacklist_id, event_template):
162
This signal is emmitted when a template is deleted.
164
:param blacklist_id: The Id of the Blacklist template which was removed
165
:param event_template: The blacklist template which was removed