~activity-log-manager/activity-log-manager/trunk

« back to all changes in this revision

Viewing changes to alm/remote.py

  • Committer: Manish Sinha
  • Date: 2011-09-16 20:08:14 UTC
  • Revision ID: manishsinha@ubuntu.com-20110916200814-kzd8ymnaexw1vvpz
Improved setup.py and renamed the folder to alm

* The activity-log-manager module will now known as alm
* Renamed src folder as alm
* Removed recursive_install function from setup
* Changed the URL field of distutils
* Added scripts and package field in setup method of distutils
* Removed the line where the main executable script and module scripts were being installed bydata_files instead of 'scripts' and 'package'
* Renamed all occourance of 'src' in imports

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -.- coding: utf-8 -.-
 
2
#
 
3
# Activity Log Manager
 
4
#
 
5
# Copyright © 2011 Collabora Ltd.
 
6
#             By Siegfried-Angel Gevatter Pujals <siegfried@gevatter.com>
 
7
#             By Seif Lotfy <seif@lotfy.com>
 
8
# Copyright © 2011 Manish Sinha <manishsinha@ubuntu.com>
 
9
#
 
10
# This program is free software: you can redistribute it and/or modify
 
11
# it under the terms of the GNU General Public License as published by
 
12
# the Free Software Foundation; either version 2 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 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 itertools import imap
 
24
 
 
25
from zeitgeist.client import ZeitgeistClient
 
26
from zeitgeist.datamodel import Event, Subject, ResultType
 
27
 
 
28
__all__ = ['get_interface']
 
29
 
 
30
class ZeitgeistInterface:
 
31
 
 
32
        def __init__(self):
 
33
                self._client = ZeitgeistClient()
 
34
                if self._client.get_version() < [0, 7, 99]:
 
35
                        raise SystemExit, _("Zeitgeist version 0.8 or later required.")
 
36
                
 
37
                self._blacklist = self._client._iface.get_extension('Blacklist',
 
38
                        'blacklist')
 
39
                self._all = None
 
40
                self._listeners = set()
 
41
 
 
42
        @property
 
43
        def all_templates(self):
 
44
                if self._all is None:
 
45
                        self._all = {}
 
46
                        # Connect to signals
 
47
                        self._blacklist.connect('TemplateAdded', self._template_added)
 
48
                        self._blacklist.connect('TemplateRemoved', self._template_removed)
 
49
                        # And fetch the current templates
 
50
                        for blacklist_id, template in self._blacklist.GetTemplates().iteritems():
 
51
                                self._all[blacklist_id] = Event(template)
 
52
                return self._all
 
53
 
 
54
        def _template_added(self, blacklist_id, template):
 
55
                if blacklist_id not in self._all:
 
56
                        self._all[blacklist_id] = Event(template)
 
57
                        for listener in self._listeners:
 
58
                                listener.template_added(blacklist_id, template)
 
59
        
 
60
        def _template_removed(self, blacklist_id, template):
 
61
                if blacklist_id in self._all:
 
62
                        del self._all[blacklist_id]
 
63
                        for listener in self._listeners:
 
64
                                listener.template_removed(blacklist_id, template)
 
65
        
 
66
        def add_blacklist_template(self, blacklist_id, template):
 
67
                """
 
68
                Add a new blacklist template.
 
69
                """
 
70
                self._blacklist.AddTemplate(blacklist_id, template)
 
71
                self._all[blacklist_id] = Event(template)
 
72
 
 
73
        def remove_blacklist_template(self, blacklist_id):
 
74
                """
 
75
                Remove the blacklist template for the provided template id.
 
76
                """
 
77
                self._blacklist.RemoveTemplate(blacklist_id)
 
78
                if blacklist_id in self._all:
 
79
                        del self._all[blacklist_id]
 
80
                        
 
81
        def remove_history(self, timerange, callback):
 
82
                """
 
83
                Delete all events within the given timestamp and call the given
 
84
                method with the removed event IDs upon completion.
 
85
                """
 
86
                last_ids = []
 
87
                def get_ids_callback(ids):
 
88
                        last_ids = ids
 
89
                        def remove_callback(timerange):
 
90
                                callback(last_ids)
 
91
                        self._client.delete_events(ids, remove_callback)
 
92
                self._client.find_event_ids_for_templates([], get_ids_callback,
 
93
                        timerange, 2, 0)
 
94
 
 
95
        def get_all_applications(self, callback):
 
96
                """
 
97
                Fetch a list of all .desktop files for which Zeitgeist currently
 
98
                has events and pass it on to the given callback method.
 
99
                """
 
100
                def callback_wrapper(events):
 
101
                        actors = set()
 
102
                        for event in events:
 
103
                                actor = Event(event).get_actor()
 
104
                                if actor.startswith('application://'):
 
105
                                        actors.add(actor[14:])
 
106
                        callback(actors)
 
107
                self._client.find_events_for_template(Event.new_for_values(),
 
108
                        callback_wrapper, result_type=ResultType.MostPopularActor)
 
109
 
 
110
        def register_listener(self, obj):
 
111
                """
 
112
                Register an object to be notified whenever a blacklist is added
 
113
                or removed.
 
114
                
 
115
                This will happen by calling its 'template_added' and 'template_removed'
 
116
                methods.
 
117
                """
 
118
                self._listeners.add(obj)
 
119
 
 
120
class GenericBlacklist(object):
 
121
 
 
122
        _add_listeners = None
 
123
        _del_listeners = None
 
124
 
 
125
        def __init__(self):
 
126
                self._zg = get_interface()
 
127
                self._zg.register_listener(self)
 
128
                self._add_listeners = set()
 
129
                self._del_listeners = set()
 
130
 
 
131
        def _parse(self, template_id, template):
 
132
                raise NotImplementedError
 
133
 
 
134
        @staticmethod
 
135
        def _activate_listeners(listeners, *args, **kwargs):
 
136
                for listener in listeners:
 
137
                        listener(*args, **kwargs)
 
138
 
 
139
        def subscribe(self, add=None, remove=None):
 
140
                if add is not None:
 
141
                        self._add_listeners.add(add)
 
142
                if remove is not None:
 
143
                        self._del_listeners.add(remove)
 
144
 
 
145
        def template_added(self, template_id, template):
 
146
                data = self._parse(template_id, Event(template))
 
147
                if data:
 
148
                        self._activate_listeners(self._add_listeners, data)
 
149
 
 
150
        def template_removed(self, template_id, template):
 
151
                data = self._parse(template_id, Event(template))
 
152
                if data:
 
153
                        self._activate_listeners(self._del_listeners, data)
 
154
 
 
155
class IncognitoBlacklist(GenericBlacklist):
 
156
 
 
157
        INCOGNITO = Event.new_for_values()
 
158
        INCOGNITO_TEMPLATE_ID = "block-all"
 
159
 
 
160
        @classmethod
 
161
        def _parse(cls, template_id, template):
 
162
                if cls.INCOGNITO.matches_template(template):
 
163
                        return True
 
164
                return None
 
165
 
 
166
        def get_incognito(self):
 
167
                """
 
168
                Return whether there is a catch-all template or not.
 
169
                
 
170
                This is done by matching the blacklist template for incognito
 
171
                against all the rules in the blacklist.
 
172
                """
 
173
                return any(imap(self.INCOGNITO.matches_template,
 
174
                        self._zg.all_templates.itervalues()))
 
175
 
 
176
        def set_incognito(self, enabled):
 
177
                """
 
178
                Add or remove a blacklist with a catch-all (empty) template that will
 
179
                block everything from being logged.
 
180
                """
 
181
                if enabled:
 
182
                        self._zg.add_blacklist_template(self.INCOGNITO_TEMPLATE_ID,
 
183
                                self.INCOGNITO)
 
184
                else:
 
185
                        self._zg.remove_blacklist_template(self.INCOGNITO_TEMPLATE_ID)
 
186
 
 
187
ZG = None
 
188
def get_interface():
 
189
        global ZG
 
190
        if ZG is None:
 
191
                ZG = ZeitgeistInterface()
 
192
        return ZG