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

« back to all changes in this revision

Viewing changes to _zeitgeist/engine/datamodel.py

  • Committer: Package Import Robot
  • Author(s): Didier Roche
  • Date: 2011-11-15 11:15:56 UTC
  • mto: (6.2.2 experimental) (1.3.1)
  • mto: This revision was merged to the branch mainline in revision 19.
  • Revision ID: package-import@ubuntu.com-20111115111556-so7cmhfbqongw7hf
Tags: upstream-0.8.99~alpha1
Import upstream version 0.8.99~alpha1

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
 
# Copyright © 2009 Markus Korn <thekorn@gmx.de>
7
 
# Copyright © 2009 Seif Lotfy <seif@lotfy.com>
8
 
# Copyright © 2009-2010 Siegfried-Angel Gevatter Pujals <rainct@ubuntu.com>
9
 
#
10
 
# This program is free software: you can redistribute it and/or modify
11
 
# it under the terms of the GNU Lesser General Public License as published by
12
 
# the Free Software Foundation, either version 2.1 of the License, or
13
 
# (at your option) any later version.
14
 
#
15
 
# This program is distributed in the hope that it will be useful,
16
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 
# GNU Lesser General Public License for more details.
19
 
#
20
 
# You should have received a copy of the GNU Lesser General Public License
21
 
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 
 
23
 
from zeitgeist.datamodel import Event as OrigEvent, Subject as OrigSubject, \
24
 
        DataSource as OrigDataSource
25
 
 
26
 
class Event(OrigEvent):
27
 
        
28
 
        @staticmethod
29
 
        def _to_unicode(obj):
30
 
                """
31
 
                Return an unicode representation of the given object.
32
 
                If obj is None, return an empty string.
33
 
                """
34
 
                return unicode(obj) if obj is not None else u""
35
 
 
36
 
        def _make_dbus_sendable(self):
37
 
                """
38
 
                Ensure that all fields in the event struct are non-None
39
 
                """
40
 
                for n, value in enumerate(self[0]):
41
 
                        self[0][n] = self._to_unicode(value)
42
 
                for subject in self[1]:
43
 
                        for n, value in enumerate(subject):
44
 
                                subject[n] = self._to_unicode(value)
45
 
                # The payload require special handling, since it is binary data
46
 
                # If there is indeed data here, we must not unicode encode it!
47
 
                if self[2] is None:
48
 
                        self[2] = u""
49
 
                elif isinstance(self[2], unicode):
50
 
                        self[2] = str(self[2])
51
 
                        
52
 
        @staticmethod
53
 
        def get_plain(ev):
54
 
                """
55
 
                Ensure that an Event instance is a Plain Old Python Object (popo),
56
 
                without DBus wrappings etc.
57
 
                """
58
 
                popo = []
59
 
                popo.append(map(unicode, ev[0]))
60
 
                popo.append([map(unicode, subj) for subj in ev[1]])
61
 
                # We need the check here so that if D-Bus gives us an empty
62
 
                # byte array we don't serialize the text "dbus.Array(...)".
63
 
                popo.append(str(ev[2]) if ev[2] else u'')
64
 
                return popo
65
 
 
66
 
class Subject(OrigSubject):
67
 
    pass
68
 
 
69
 
class DataSource(OrigDataSource):
70
 
 
71
 
        @staticmethod
72
 
        def get_plain(datasource):
73
 
                for plaintype, props in {
74
 
                                unicode: (DataSource.Name, DataSource.Description),
75
 
                                lambda x: map(Event.get_plain, x): (DataSource.EventTemplates,),
76
 
                                bool: (DataSource.Running, DataSource.Enabled),
77
 
                                int: (DataSource.LastSeen,),
78
 
                        }.iteritems():
79
 
                        for prop in props:
80
 
                                datasource[prop] = plaintype(datasource[prop])
81
 
                return tuple(datasource)