home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / python2.4 / site-packages / dbus / _dbus.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-08-31  |  10.5 KB  |  294 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. """Module for high-level communication over the FreeDesktop.org Bus (DBus)
  5.  
  6. DBus allows you to share and access remote objects between processes
  7. running on the desktop, and also to access system services (such as
  8. the print spool).
  9.  
  10. To use DBus, first get a Bus object, which provides a connection to one
  11. of a few standard dbus-daemon instances that might be running. From the
  12. Bus you can get a RemoteService. A service is provided by an application or
  13. process connected to the Bus, and represents a set of objects. Once you
  14. have a RemoteService you can get a RemoteObject that implements a specific interface
  15. (an interface is just a standard group of member functions). Then you can call
  16. those member functions directly.
  17.  
  18. You can think of a complete method call as looking something like:
  19.  
  20. Bus:SESSION -> Service:org.gnome.Evolution -> Object:/org/gnome/Evolution/Inbox -> Interface: org.gnome.Evolution.MailFolder -> Method: Forward('message1', 'seth@gnome.org')
  21.  
  22. This communicates over the SESSION Bus to the org.gnome.Evolution process to call the
  23. Forward method of the /org/gnome/Evolution/Inbox object (which provides the
  24. org.gnome.Evolution.MailFolder interface) with two string arguments.
  25.  
  26. For example, the dbus-daemon itself provides a service and some objects:
  27.  
  28. # Get a connection to the desktop-wide SESSION bus
  29. bus = dbus.Bus(dbus.Bus.TYPE_SESSION)
  30.  
  31. # Get the service provided by the dbus-daemon named org.freedesktop.DBus
  32. dbus_service = bus.get_service('org.freedesktop.DBus')
  33.  
  34. # Get a reference to the desktop bus' standard object, denoted
  35. # by the path /org/freedesktop/DBus. The object /org/freedesktop/DBus
  36. # implements the 'org.freedesktop.DBus' interface
  37. dbus_object = dbus_service.get_object('/org/freedesktop/DBus',
  38.                                        'org.freedesktop.DBus')
  39.  
  40. # One of the member functions in the org.freedesktop.DBus interface
  41. # is ListServices(), which provides a list of all the other services
  42. # registered on this bus. Call it, and print the list.
  43. print(dbus_object.ListServices())
  44. """
  45. import dbus
  46. import dbus_bindings
  47. import weakref
  48. from proxies import *
  49. from exceptions import *
  50. from matchrules import *
  51.  
  52. class Bus(object):
  53.     '''A connection to a DBus daemon.
  54.  
  55.     One of three possible standard buses, the SESSION, SYSTEM,
  56.     or STARTER bus
  57.     '''
  58.     TYPE_SESSION = dbus_bindings.BUS_SESSION
  59.     TYPE_SYSTEM = dbus_bindings.BUS_SYSTEM
  60.     TYPE_STARTER = dbus_bindings.BUS_STARTER
  61.     ProxyObjectClass = ProxyObject
  62.     START_REPLY_SUCCESS = dbus_bindings.DBUS_START_REPLY_SUCCESS
  63.     START_REPLY_ALREADY_RUNNING = dbus_bindings.DBUS_START_REPLY_ALREADY_RUNNING
  64.     _shared_instances = weakref.WeakValueDictionary()
  65.     
  66.     def __new__(cls, bus_type = TYPE_SESSION, use_default_mainloop = True, private = False):
  67.         if not private and bus_type in cls._shared_instances:
  68.             return cls._shared_instances[bus_type]
  69.         
  70.         if bus_type == cls.TYPE_SESSION:
  71.             subclass = SessionBus
  72.         elif bus_type == cls.TYPE_SYSTEM:
  73.             subclass = SystemBus
  74.         elif bus_type == cls.TYPE_STARTER:
  75.             subclass = StarterBus
  76.         else:
  77.             raise ValueError('invalid bus_type %s' % bus_type)
  78.         bus = object.__new__(subclass)
  79.         bus._bus_type = bus_type
  80.         bus._bus_names = weakref.WeakValueDictionary()
  81.         bus._match_rule_tree = SignalMatchTree()
  82.         bus._connection = dbus_bindings.bus_get(bus_type, private)
  83.         bus._connection.add_filter(bus._signal_func)
  84.         if use_default_mainloop:
  85.             func = getattr(dbus, '_dbus_mainloop_setup_function', None)
  86.             if func:
  87.                 func(bus)
  88.             
  89.         
  90.         if not private:
  91.             cls._shared_instances[bus_type] = bus
  92.         
  93.         return bus
  94.  
  95.     
  96.     def __init__(self, *args, **keywords):
  97.         pass
  98.  
  99.     
  100.     def close(self):
  101.         self._connection.close()
  102.  
  103.     
  104.     def get_connection(self):
  105.         return self._connection
  106.  
  107.     
  108.     def get_session(private = False):
  109.         '''Static method that returns the session bus'''
  110.         return SessionBus(private = private)
  111.  
  112.     get_session = staticmethod(get_session)
  113.     
  114.     def get_system(private = False):
  115.         '''Static method that returns the system bus'''
  116.         return SystemBus(private = private)
  117.  
  118.     get_system = staticmethod(get_system)
  119.     
  120.     def get_starter(private = False):
  121.         '''Static method that returns the starter bus'''
  122.         return StarterBus(private = private)
  123.  
  124.     get_starter = staticmethod(get_starter)
  125.     
  126.     def get_object(self, named_service, object_path):
  127.         '''Get a proxy object to call over the bus'''
  128.         return self.ProxyObjectClass(self, named_service, object_path)
  129.  
  130.     
  131.     def _create_args_dict(self, keywords):
  132.         args_dict = None
  133.         for key, value in keywords.iteritems():
  134.             if key.startswith('arg'):
  135.                 
  136.                 try:
  137.                     snum = key[3:]
  138.                     num = int(snum)
  139.                     if not args_dict:
  140.                         args_dict = { }
  141.                     
  142.                     args_dict[num] = value
  143.                 except ValueError:
  144.                     raise TypeError('Invalid arg index %s' % snum)
  145.                 except:
  146.                     None<EXCEPTION MATCH>ValueError
  147.                 
  148.  
  149.             None<EXCEPTION MATCH>ValueError
  150.             if key in ('sender_keyword', 'path_keyword'):
  151.                 continue
  152.             raise TypeError('Unknown keyword %s' % key)
  153.         
  154.         return args_dict
  155.  
  156.     
  157.     def add_signal_receiver(self, handler_function, signal_name = None, dbus_interface = None, named_service = None, path = None, **keywords):
  158.         args_dict = self._create_args_dict(keywords)
  159.         if named_service and named_service[0] != ':':
  160.             bus_object = self.get_object('org.freedesktop.DBus', '/org/freedesktop/DBus')
  161.             named_service = bus_object.GetNameOwner(named_service, dbus_interface = 'org.freedesktop.DBus')
  162.         
  163.         match_rule = SignalMatchRule(signal_name, dbus_interface, named_service, path)
  164.         for kw in ('sender_keyword', 'path_keyword'):
  165.             if kw in keywords:
  166.                 setattr(match_rule, kw, keywords[kw])
  167.                 continue
  168.             setattr(match_rule, kw, None)
  169.         
  170.         if args_dict:
  171.             match_rule.add_args_match(args_dict)
  172.         
  173.         match_rule.add_handler(handler_function)
  174.         self._match_rule_tree.add(match_rule)
  175.         dbus_bindings.bus_add_match(self._connection, repr(match_rule))
  176.  
  177.     
  178.     def remove_signal_receiver(self, handler_function, signal_name = None, dbus_interface = None, named_service = None, path = None, **keywords):
  179.         args_dict = self._create_args_dict(keywords)
  180.         if named_service and named_service[0] != ':':
  181.             bus_object = self.get_object('org.freedesktop.DBus', '/org/freedesktop/DBus')
  182.             named_service = bus_object.GetNameOwner(named_service, dbus_interface = 'org.freedesktop.DBus')
  183.         
  184.         match_rule = SignalMatchRule(signal_name, dbus_interface, named_service, path)
  185.         if args_dict:
  186.             match_rule.add_args_match(args_dict)
  187.         
  188.         if handler_function:
  189.             match_rule.add_handler(handler_function)
  190.         
  191.         self._match_rule_tree.remove(match_rule)
  192.  
  193.     
  194.     def get_unix_user(self, named_service):
  195.         '''Get the unix user for the given named_service on this Bus'''
  196.         return dbus_bindings.bus_get_unix_user(self._connection, named_service)
  197.  
  198.     
  199.     def _signal_func(self, connection, message):
  200.         if message.get_type() != dbus_bindings.MESSAGE_TYPE_SIGNAL:
  201.             return dbus_bindings.HANDLER_RESULT_NOT_YET_HANDLED
  202.         
  203.         dbus_interface = message.get_interface()
  204.         named_service = message.get_sender()
  205.         path = message.get_path()
  206.         signal_name = message.get_member()
  207.         match_rule = SignalMatchRule(signal_name, dbus_interface, named_service, path)
  208.         self._match_rule_tree.exec_matches(match_rule, message)
  209.  
  210.     
  211.     def start_service_by_name(self, named_service):
  212.         return dbus_bindings.bus_start_service_by_name(self._connection, named_service)
  213.  
  214.     
  215.     def __repr__(self):
  216.         if self._bus_type == self.TYPE_SESSION:
  217.             name = 'SESSION'
  218.         elif self._bus_type == self.TYPE_SYSTEM:
  219.             name = 'SYSTEM'
  220.         elif self._bus_type == self.TYPE_STARTER:
  221.             name = 'STARTER'
  222.         elif not False:
  223.             raise AssertionError, 'Unable to represent unknown bus type.'
  224.         return '<dbus.Bus on %s at %#x>' % (name, id(self))
  225.  
  226.     __str__ = __repr__
  227.  
  228.  
  229. class SystemBus(Bus):
  230.     '''The system-wide message bus
  231.     '''
  232.     
  233.     def __new__(cls, use_default_mainloop = True, private = False):
  234.         return Bus.__new__(cls, Bus.TYPE_SYSTEM, use_default_mainloop, private)
  235.  
  236.  
  237.  
  238. class SessionBus(Bus):
  239.     '''The session (current login) message bus
  240.     '''
  241.     
  242.     def __new__(cls, use_default_mainloop = True, private = False):
  243.         return Bus.__new__(cls, Bus.TYPE_SESSION, use_default_mainloop, private)
  244.  
  245.  
  246.  
  247. class StarterBus(Bus):
  248.     '''The bus that activated this process (if
  249.     this process was launched by DBus activation)
  250.     '''
  251.     
  252.     def __new__(cls, use_default_mainloop = True, private = False):
  253.         return Bus.__new__(cls, Bus.TYPE_STARTER, use_default_mainloop, private)
  254.  
  255.  
  256.  
  257. class Interface:
  258.     '''An interface into a remote object
  259.  
  260.     An Interface can be used to wrap ProxyObjects
  261.     so that calls can be routed to their correct
  262.     dbus interface
  263.     '''
  264.     
  265.     def __init__(self, object, dbus_interface):
  266.         self._obj = object
  267.         self._dbus_interface = dbus_interface
  268.  
  269.     
  270.     def connect_to_signal(self, signal_name, handler_function, dbus_interface = None, **keywords):
  271.         if not dbus_interface:
  272.             dbus_interface = self._dbus_interface
  273.         
  274.         self._obj.connect_to_signal(signal_name, handler_function, dbus_interface, **keywords)
  275.  
  276.     
  277.     def __getattr__(self, member, **keywords):
  278.         if keywords.has_key('dbus_interface'):
  279.             _dbus_interface = keywords['dbus_interface']
  280.         else:
  281.             _dbus_interface = self._dbus_interface
  282.         if member == '__call__':
  283.             return object.__call__
  284.         else:
  285.             ret = self._obj.__getattr__(member, dbus_interface = _dbus_interface)
  286.             return ret
  287.  
  288.     
  289.     def __repr__(self):
  290.         return '<Interface %r implementing %r at %#x>' % (self._obj, self._dbus_interface, id(self))
  291.  
  292.     __str__ = __repr__
  293.  
  294.