From 4d75f415721505e488505ae737e0857e7582c278 Mon Sep 17 00:00:00 2001 From: Stephen Watson Date: Tue, 21 Mar 2006 11:31:18 +0000 Subject: [PATCH] Use XMLRPC if the DBUS version is too old. git-svn-id: https://rox.svn.sourceforge.net/svnroot/rox/trunk/ROX-Lib2@4518 66de3db3-b00d-0410-b41b-f4738ad19bea --- python/rox/session.py | 71 +++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 58 insertions(+), 13 deletions(-) diff --git a/python/rox/session.py b/python/rox/session.py index a12e621..5b1316b 100644 --- a/python/rox/session.py +++ b/python/rox/session.py @@ -1,5 +1,5 @@ -"""Contact ROX-Session via the DBus interface. Using get_session() will -return a proxy object on which you can make remote calls to control +"""Contact ROX-Session via the DBus or XMLRPC interface. Using get_session() +will return a proxy object on which you can make remote calls to control the session. Similarly get_settings() will return an object to control session settings, e.g. @@ -12,20 +12,25 @@ session settings, e.g. In addition the Setting class is provided which derives from rox.options.Option but has two important differences: it is not saved to the options file and it is synchronized with a value of the same name in the ROX-Session settings. - -Remember to trap exceptions from this module, including importing it! """ import os import rox from rox.options import OptionGroup, Option from rox import OptionsBox, g +import rox.xxmlrpc import gobject -# If DBus is not available this will raise an exception, hence the warning -# above -import dbus -bus = dbus.Bus(dbus.Bus.TYPE_SESSION) +try: + import dbus + dbus_ok=(dbus.version>=(0, 42, 0)) +except: + dbus_ok=False + +if dbus_ok: + bus = dbus.Bus(dbus.Bus.TYPE_SESSION) +else: + bus = None session_service = "net.sf.rox.Session" control_object = '/Session' @@ -33,7 +38,8 @@ control_interface = 'net.sf.rox.Session.Control' settings_interface = 'net.sf.rox.Session.Settings' settings_object = '/Settings' -def get_proxy(bus, service_name, object_name, interface_name): +def _dbus_get_proxy(bus, service_name, object_name, interface_name): + """For internal use. Do not call this, call get_proxy() instead.""" try: service = bus.get_service(service_name) obj = service.get_object(object_name, interface_name) @@ -44,20 +50,54 @@ def get_proxy(bus, service_name, object_name, interface_name): proxy = iface return proxy +class _caller: + """For internal use.""" + def __init__(self, method): + self.method=method + + def __call__(self, *params): + client=self.method(*params) + return client.get_response() + +class _RPCProxy: + """For internal use.""" + def __init__(self, obj): + self.obj=obj + + def __getattr__(self, method): + invoke=self.obj.__getattr__(method) + return _caller(invoke) + +def _xxmlrpc_get_proxy(service_name, object_name, interface_name): + """For internal use. Do not call this, call get_proxy() instead.""" + proxy=rox.xxmlrpc.XXMLProxy(service_name) + return _RPCProxy(proxy.get_object(object_name)) + +def get_proxy(service_name, object_name, interface_name): + """Get a proxy object for the required service, object path and interface. + This selects an appropriate transport for you, either DBus or XMLRPC.""" + if dbus_ok: + return _dbus_get_proxy(bus, service_name, object_name, interface_name) + return _xxmlrpc_get_proxy(service_name, object_name, interface_name) + def get_session(): """Return a proxy object for the ROX-Session settings interface""" - return get_proxy(bus, session_service, control_object, + return get_proxy(session_service, control_object, control_interface) def get_settings(): """Return a proxy object for the ROX-Session control interface""" - return get_proxy(bus, session_service, settings_object, + return get_proxy(session_service, settings_object, settings_interface) def running(): """Return True if ROX-Session is detected as running""" + if not dbus_ok: + proxy=_xxmlrpc_get_proxy(session_service, control_object, + control_interface) + return proxy is not None try: - proxy = get_proxy(bus, 'org.freedesktop.DBus', '/org/freedesktop/DBus', + proxy = get_proxy('org.freedesktop.DBus', '/org/freedesktop/DBus', 'org.freedesktop.DBus') except: return False @@ -83,7 +123,7 @@ class Setting(Option): self.proxy = get_settings() try: type, value = self.proxy.GetSetting(name) - except: #XXX: dbus.DBusException: + except: self._set(default) else: self._set(value, notify = False) @@ -109,8 +149,13 @@ class Setting(Option): if __name__=='__main__': print 'Session running? %s' % running() settings=get_settings() + #print 'settings=', settings v='Gtk/KeyThemeName' print '%s = %s' % (v, settings.GetSetting(v)) + #x=settings.GetSetting(v) + #print str(x), `x` + #y=x.get_response() + #print y v='Net/ThemeName' print '%s = %s' % (v, settings.GetSetting(v)) control=get_session() -- 2.11.4.GIT