summaryrefslogtreecommitdiff
path: root/extensions/fts-python/datamodel.py
blob: defbe7119795b529c858249571f172a268ff5244 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# -.- coding: utf-8 -.-

# Zeitgeist
#
# Copyright © 2009 Mikkel Kamstrup Erlandsen <mikkel.kamstrup@gmail.com>
# Copyright © 2009 Markus Korn <thekorn@gmx.de>
# Copyright © 2009 Seif Lotfy <seif@lotfy.com>
# Copyright © 2009-2010 Siegfried-Angel Gevatter Pujals <rainct@ubuntu.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 2.1 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from zeitgeist.datamodel import Event as OrigEvent, Subject as OrigSubject, \
	DataSource as OrigDataSource
	
class Event(OrigEvent):
	
	@staticmethod
	def _to_unicode(obj):
		"""
		Return an unicode representation of the given object.
		If obj is None, return an empty string.
		"""
		return unicode(obj) if obj is not None else u""
	
	@staticmethod
	def _make_dbus_sendable(obj):
		"""
		Ensure that all fields in the event struct are non-None
		"""
		for n, value in enumerate(obj[0]):
			obj[0][n] = obj._to_unicode(value)
		for subject in obj[1]:
			for n, value in enumerate(subject):
				subject[n] = obj._to_unicode(value)
		# The payload require special handling, since it is binary data
		# If there is indeed data here, we must not unicode encode it!
		if obj[2] is None:
			obj[2] = u""
		elif isinstance(obj[2], unicode):
			obj[2] = str(obj[2])
		return obj
			
	@staticmethod
	def get_plain(ev):
		"""
		Ensure that an Event instance is a Plain Old Python Object (popo),
		without DBus wrappings etc.
		"""
		popo = []
		popo.append(map(unicode, ev[0]))
		popo.append([map(unicode, subj) for subj in ev[1]])
		# We need the check here so that if D-Bus gives us an empty
		# byte array we don't serialize the text "dbus.Array(...)".
		popo.append(str(ev[2]) if ev[2] else u'')
		return popo

class Subject(OrigSubject):
    pass

class DataSource(OrigDataSource):

	@staticmethod
	def get_plain(datasource):
		for plaintype, props in {
				unicode: (DataSource.Name, DataSource.Description),
				lambda x: map(Event.get_plain, x): (DataSource.EventTemplates,),
				bool: (DataSource.Running, DataSource.Enabled),
				int: (DataSource.LastSeen,),
			}.iteritems():
			for prop in props:
				datasource[prop] = plaintype(datasource[prop])
		return tuple(datasource)