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

« back to all changes in this revision

Viewing changes to zeitgeist/client.py

* New upstream release. Some of the changes are:
   - Various performance improvements (speed, reduced I/O, etc).
   - Enhancements to the extensions system (eg. feature to ask which
     extensions are active).
   - Various bug fixes (eg. fixed find_event_for_template Python API method).
   - Added new mimetype mappings.
* Updated debian/copyright and debian/zeitgeist-core.install.

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
#
9
9
# This program is free software: you can redistribute it and/or modify
10
10
# it under the terms of the GNU Lesser General Public License as published by
11
 
# the Free Software Foundation, either version 3 of the License, or
 
11
# the Free Software Foundation, either version 2.1 of the License, or
12
12
# (at your option) any later version.
13
13
#
14
14
# This program is distributed in the hope that it will be useful,
26
26
import os.path
27
27
import sys
28
28
import logging
 
29
import inspect
29
30
 
30
31
from xml.dom.minidom import parseString as minidom_parse
31
32
 
36
37
 
37
38
SIG_EVENT = "asaasay"
38
39
 
39
 
logging.basicConfig(level=logging.DEBUG)
40
40
log = logging.getLogger("zeitgeist.client")
41
41
 
42
42
class _DBusInterface(object):
147
147
                                dbus_interface=dbus.PROPERTIES_IFACE)(self.INTERFACE_NAME,
148
148
                                        "version"))
149
149
        
150
 
        def get_extension(cls, name, path):
 
150
        def extensions(self):
 
151
                """Returns active extensions"""
 
152
                dbus_interface = self.__shared_state["dbus_interface"]
 
153
                return dbus_interface._disconnection_safe(lambda:
 
154
                        dbus_interface.proxy.get_dbus_method("Get",
 
155
                                dbus_interface=dbus.PROPERTIES_IFACE)(self.INTERFACE_NAME,
 
156
                                        "extensions"))
 
157
                                        
 
158
        def get_extension(cls, name, path, busname=None):
151
159
                """ Returns an interface to the given extension.
152
160
                
153
161
                Example usage:
154
162
                        >> reg = get_extension("DataSourceRegistry", "data_source_registry")
155
163
                        >> reg.RegisterDataSource(...)
156
164
                """
 
165
                if busname:
 
166
                        busname = "org.gnome.zeitgeist.%s" % busname
 
167
                else:
 
168
                        busname = cls.BUS_NAME
157
169
                if not name in cls.__shared_state["extension_interfaces"]:
158
170
                        interface_name = "org.gnome.zeitgeist.%s" % name
159
171
                        object_path = "/org/gnome/zeitgeist/%s" % path
160
 
                        proxy = dbus.SessionBus().get_object(cls.BUS_NAME, object_path)
 
172
                        proxy = dbus.SessionBus().get_object(busname, object_path)
161
173
                        iface = _DBusInterface(proxy, interface_name, object_path)
162
 
                        iface.BUS_NAME = cls.BUS_NAME
 
174
                        iface.BUS_NAME = busname
163
175
                        iface.INTERFACE_NAME = interface_name
164
176
                        iface.OBJECT_PATH = object_path
165
177
                        cls.__shared_state["extension_interfaces"][name] = iface
287
299
        always the right thing to do. If you really want to do synchronous
288
300
        DBus calls use the raw DBus API found in the ZeitgeistDBusInterface class.
289
301
        """
 
302
        
 
303
        @staticmethod
 
304
        def get_event_and_extra_arguments(arguments):
 
305
                """ some methods of :class:`ZeitgeistClient` take a variable
 
306
                number of arguments, where one part of the arguments are used
 
307
                to build one :class:`Event` instance and the other part
 
308
                is forwarded to another method. This function returns an event
 
309
                and the remaining arguments."""
 
310
                kwargs = {}
 
311
                for arg in _FIND_EVENTS_FOR_TEMPLATES_ARGS:
 
312
                        if arg in arguments:
 
313
                                kwargs[arg] = arguments.pop(arg)
 
314
                ev = Event.new_for_values(**arguments)
 
315
                return ev, kwargs
 
316
        
290
317
        def __init__ (self):
291
318
                self._iface = ZeitgeistDBusInterface()
292
319
                self._registry = self._iface.get_extension("DataSourceRegistry",
308
335
                                "Reply handler not callable, found %s" % reply_handler)
309
336
                return self._void_reply_handler
310
337
        
 
338
        # Properties
 
339
        
311
340
        def get_version(self):
312
341
                return [int(i) for i in self._iface.version()]
 
342
                
 
343
        def get_extensions(self):
 
344
                return [unicode(i) for i in self._iface.extensions()]
 
345
        
 
346
        # Methods
313
347
        
314
348
        def insert_event (self, event, ids_reply_handler=None, error_handler=None):
315
349
                """
493
527
                allowed keywords are the same as the ones allowed by
494
528
                :meth:`Event.new_for_values() <zeitgeist.datamodel.Event.new_for_values>`.
495
529
                """
496
 
                ev = Event.new_for_values(**kwargs)
497
 
                
 
530
                ev, arguments = self.get_event_and_extra_arguments(kwargs)
498
531
                self.find_event_ids_for_templates([ev],
499
532
                                                ids_reply_handler,
500
 
                                                **kwargs)
 
533
                                                **arguments)
501
534
        
502
535
        def find_events_for_templates (self,
503
536
                                        event_templates,
591
624
                allowed keywords are the same as the ones allowed by
592
625
                :meth:`Event.new_for_values() <zeitgeist.datamodel.Event.new_for_values>`.
593
626
                """
594
 
                ev = Event.new_for_values(**kwargs)
595
 
                # we need a clean dict of arguments to find_event_for_templates
596
 
                # (LP: #510804)
597
 
                arguments = {}
598
 
                for arg in ("timerange", "storage_state", "num_events",
599
 
                                        "result_type", "error_handler"):
600
 
                        if arg in kwargs:
601
 
                                arguments[arg] = kwargs[arg]
602
 
                
 
627
                ev, arguments = self.get_event_and_extra_arguments(kwargs)
603
628
                self.find_events_for_templates([ev],
604
629
                                                events_reply_handler,
605
630
                                                **arguments)
892
917
                
893
918
                if callable(normal_reply_handler):
894
919
                        normal_reply_handler(normal_reply_data)
 
920
 
 
921
_FIND_EVENTS_FOR_TEMPLATES_ARGS = inspect.getargspec(
 
922
        ZeitgeistClient.find_events_for_templates)[0]