1
# -.- coding: utf-8 -.-
5
# Copyright © 2010 Randal Barlow
7
# This program is free software: you can redistribute it and/or modify
8
# it under the terms of the GNU General Public License as published by
9
# the Free Software Foundation, either version 3 of the License, or
10
# (at your option) any later version.
12
# This program is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU Lesser General Public License for more details.
17
# You should have received a copy of the GNU General Public License
18
# along with this program. If not, see <http://www.gnu.org/licenses/>.
24
from zeitgeist.client import ZeitgeistClient, ZeitgeistDBusInterface
25
from zeitgeist.datamodel import Event, ResultType, Interpretation
27
import content_objects
29
CLIENT = ZeitgeistClient()
30
INTERFACE = ZeitgeistDBusInterface()
33
#content_object_selector_function = (lambda x: None)
34
content_object_selector_function = content_objects.choose_content_object
36
tdelta = lambda x: datetime.timedelta(days=x)
39
def reduce_dates_by_timedelta(dates, delta):
42
new_dates += [date + delta]
48
Calls emit_update on the methods class
50
def __init__(self, signal):
53
def __call__(self, function):
54
def wrapper(instance, *args, **kwargs):
55
value = function(instance, *args, **kwargs)
56
instance.emit(self.signal)
61
class CachedAttribute(object):
63
runs the method once, finds the value, and replace the descriptor
64
in the instance with the found value
66
def __init__(self, method, name=None):
68
self.attr_name = name or method.__name__
70
def __get__(self, instance, cls):
73
value = self.method(instance)
74
setattr(instance, self.attr_name, value)
78
class ContentStruct(object):
83
def content_object(self):
84
return content_object_selector_function(self.event)
88
events = INTERFACE.GetEvents([self.id])
90
return Event(events[0])
92
def __init__(self, id, event=None, content_object=None, build=False):
97
self.content_object = content_object
99
CLIENT.get_events([self.id], self.set_event)
101
def set_event(self, value):
102
if isinstance(value, tuple) or isinstance(value, list) and len(value):
103
self.event = value[0]
104
elif isinstance(value, Event):
107
self.event = ContentStruct.event
110
CLIENT.get_events([self.id], self.set_event)
113
class Day(gobject.GObject):
115
"update" : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ())
124
def time_range(self):
125
return [self.start*1000, self.end*1000]
129
return self._items.values()
133
return [Event.new_for_values()]
135
def __init__(self, date):
136
super(Day, self).__init__()
138
self._items = {}#id:ContentItem
139
self.start = int(time.mktime(date.timetuple()))
140
self.end = self.start+86399
141
CLIENT.find_event_ids_for_templates(self.templates, self.set_ids, self.time_range, num_events=MAXEVENTS)
142
CLIENT.install_monitor(self.time_range, self.templates, self.insert_events, self.remove_ids)
144
def has_id(self, id_):
145
return self._items.has_key(id_)
147
def __getitem__(self, id_):
148
return self._items[id_]
151
return len(self._items)
154
def set_ids(self, event_ids):
155
for id_ in event_ids:
156
self._items[id_] = ContentStruct(id_)
159
def remove_ids(self, time_range, ids):
167
def insert_events(self, time_range, events):
169
self._items[event.id] = ContentStruct(event.id, event)
171
def next(self, store):
172
date = self.date + datetime.timedelta(days=1)
175
def previous(self, store):
176
date = self.date + datetime.timedelta(days=-1)
179
def filter(self, event_template=None, result_type=None):
180
items = self.filter_event_template(event_template)
181
items = self.filter_result_type(items, result_type)
184
def filter_event_template(self, event_template):
186
if isinstance(event_template, Event):
187
for obj in self._items.values():
188
if obj.event.matches_template(event_template):
191
for template in event_template:
192
items += self.filter(template)
193
items = list(set(items))
196
def filter_result_type(self, items, result_type):
197
if items and result_type is ResultType.MostRecentSubjects:
200
subject_uri = item.event.subjects[0].uri
201
item_dict[subject_uri] = item
202
items = item_dict.values()
205
def get_time_map(self):
208
for item in self.items:
209
uri = item.event.subjects[0].uri
210
if not uri in results:
212
if not item.event.interpretation == Interpretation.CLOSE_EVENT.uri:
213
results[uri].append([item, 0])
215
if not len(results[uri]) == 0:
216
#print "***", results[uri]
217
results[uri][len(results[uri])-1][1] = (int(item.event.timestamp)) - int(results[uri][-1][0].event.timestamp)
219
tend = int(item.event.timestamp)
220
item.event.timestamp = str(start)
221
results[uri].append([item, tend - start])
222
results = list(sorted(results.itervalues(), key=lambda r: \
223
r[0][0].event.timestamp))
227
class Store(gobject.GObject):
229
"update" : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ())
234
return self[datetime.date.today()]
239
return [self._days[date] for date in dates]
243
dates = self._days.keys()
248
super(Store, self).__init__()
250
self._day_connections = {}
251
today = datetime.date.today()
252
t = time.mktime(today.timetuple())
253
for i in range(1,30*10):
254
date = datetime.date.fromtimestamp(t)
256
self.add_day(date, day)
260
def add_day(self, key, day):
261
self._days[key] = day
262
self._day_connections[key] = day.connect("update", lambda *args: self.emit("update"))
264
def __getitem__(self, key):
265
if isinstance(key, datetime.date):
266
# Return day from date
268
return self._days[key]
271
self.add_day(key, day)
273
elif isinstance(key, int):
275
for date, obj in self._days.iteritems():
278
raise KeyError("%s Not found" % key)
282
for item in self._days.itervalues():
286
gobject.type_register(Day)
287
gobject.type_register(Store)