Package screenlets :: Module services
[hide private]
[frames] | no frames]

Source Code for Module screenlets.services

  1  # This application is released under the GNU General Public License  
  2  # v3 (or, at your option, any later version). You can find the full  
  3  # text of the license under http://www.gnu.org/licenses/gpl.txt.  
  4  # By using, editing and/or distributing this software you agree to  
  5  # the terms and conditions of this license.  
  6  # Thank you for using free software! 
  7   
  8  # The services-module contains the ScreenletService-class and a set of utility 
  9  # functions to work with Screenlet-services from within other applications. 
 10  # 
 11  # (c) 2007 by RYX (Rico Pfaus) 
 12  # 
 13  # TODO:  
 14  # - add missing default actions and signals (similar for all screenlets) 
 15  # - maybe abstract the dbus-related stuff and create subclasses which implement  
 16  #   different communication methods? Later. 
 17  # - get_available_services() ... get list with names/ids of services 
 18  #  
 19   
 20   
 21  import dbus 
 22  import dbus.service 
 23  if getattr(dbus, 'version', (0,0,0)) >= (0,41,0): 
 24          if getattr(dbus, 'version', (0,0,0)) <= (0,80,0): 
 25                   
 26                  import dbus.glib 
 27          else: 
 28                   
 29                  from dbus.mainloop.glib import DBusGMainLoop 
 30                  DBusGMainLoop(set_as_default=True) 
 31  import gettext 
 32   
 33  gettext.textdomain('screenlets') 
 34  gettext.bindtextdomain('screenlets', '/usr/share/locale') 
 35   
36 -def _(s):
37 return gettext.gettext(s)
38 39 # quick access to dbus decorator-method (to avoid importing dbus in screenlets 40 # and keep the possibility to create custom decorators in the future) 41 action = dbus.service.method 42 signal = dbus.service.signal 43 44 45 # service base-class
46 -class ScreenletService (dbus.service.Object):
47 """The ScreenletService contains the boilerplate code for creating new 48 dbus-objects for Screenlets. Subclasses can easily implement new functions 49 to allow controlling and managing Screenlets through DBus in any way 50 imaginable. This class should implement the default actions available for 51 all screenlets - add, delete, get, set, (move?)""" 52 53 BUS = 'org.screenlets' #'org.freedesktop.Screenlets' 54 PATH = '/org/screenlets/' #'/org/freedesktop/Screenlets/' 55 IFACE = 'org.screenlets.ScreenletService' #'org.freedesktop.ScreenletService' 56
57 - def __init__ (self, screenlet, name, id=None):
58 # check types and vals 59 if name == '': 60 raise Exception(_('No name set in ScreenletService.__init__!')); 61 # init props 62 self.screenlet = screenlet 63 self.name = name 64 self.BUS = self.BUS + '.' + name 65 self.objpath = self.PATH + name 66 if id: 67 self.objpath += '/' + id # add id to path, if set 68 # call super 69 dbus.service.Object.__init__(self, dbus.service.BusName(self.BUS, 70 bus=dbus.SessionBus(), do_not_queue=True), self.objpath)
71 72 @action(IFACE)
73 - def test (self):
74 print _("TEST: %s") % str(self.screenlet)
75 76 @action(IFACE)
77 - def debug (self, string):
78 """Dump a string to the console.""" 79 print _("DEBUG: %s") % string
80 81 @action(IFACE)
82 - def add (self, id):
83 """Ask the assigned Screenlet to add a new instance of itself to 84 its session. The new Screenlet will have the ID defined by 'id'. 85 The ID of the new instance is returned, so you can auto-generate an ID 86 by passing an empty string. The function returns None if adding a 87 new instance failed for some reason.""" 88 sl = self.screenlet.session.create_instance(id) 89 sl.finish_loading() 90 if sl != None: 91 return sl.id 92 return False
93 94 @action(IFACE)
95 - def get (self, id, attrib):
96 """Ask the assigned Screenlet to return the given attribute's value. If 97 'id' is defined, the instance with the given id will be accessed, 98 else the main instance is used. Protected attributes are not returned 99 by this function. 100 TODO: Throw exception on error? ... could be abused to crash the app""" 101 if id: 102 sl = self.screenlet.session.get_instance_by_id(id) 103 if not sl: 104 sl = self.screenlet 105 o = sl.get_option_by_name(attrib) 106 try: 107 if not o.protected: 108 return getattr(sl, attrib) 109 else: 110 print _("Cannot get/set protected options through service.") 111 return None 112 except AttributeError: 113 print 'Error getting attribute' 114 return None
115 @action(IFACE)
116 - def get_first_instance (self):
117 """Get the ID of the first existing instance of the assigned 118 Screenlet (within the screenlet's active session).""" 119 if len(self.screenlet.session.instances): 120 return self.screenlet.session.instances[0].id 121 return None
122 123 @action(IFACE)
124 - def list_instances (self):
125 """Return a list with IDs of all existing instances of the assigned 126 Screenlet (within the screenlet's active session).""" 127 lst = [] 128 for sl in self.screenlet.session.instances: 129 lst.append (sl.id) 130 return lst
131 132 @action(IFACE)
133 - def quit (self):
134 """Quit all instances of the screenlet. Similar to selecting Quit 135 from the menu.""" 136 self.screenlet.destroy(self.screenlet.window)
137 138 @action(IFACE)
139 - def set (self, id, attrib, value):
140 """Ask the assigned Screenlet to set the given attribute to 'value'. The 141 instance with the given id will be accessed. """ 142 sl = self.screenlet.session.get_instance_by_id(id) 143 if sl == None: 144 raise Exception(_('Trying to access invalid instance "%s".') % id) 145 if sl.get_option_by_name(attrib) == None: 146 raise Exception(_('Trying to access invalid option "%s".') % attrib) 147 else: 148 try: 149 o = sl.get_option_by_name(attrib) 150 if not o.protected: 151 setattr(sl, attrib, value) 152 else: 153 print _("Cannot get/set protected options through service.") 154 except: pass
155 156 @signal(IFACE)
157 - def instance_added (self, id):
158 """This signal gets emitted whenever a new instance of the assigned 159 Screenlet gets added."""
160 161 @signal(IFACE)
162 - def instance_removed (self, id):
163 """This signal gets emitted whenever an instance of the assigned 164 Screenlet gets removed."""
165 166
167 -def get_service_by_name (name, interface=ScreenletService.IFACE):
168 """This currently returns a dbus.Interface-object for remote-accessing the 169 ScreenletService through dbus, but that may change in the future to some 170 more abstracted system with support for multiple IPC-backends.""" 171 bus = dbus.SessionBus() 172 if bus: 173 try: 174 path = ScreenletService.PATH + name 175 proxy_obj = bus.get_object(ScreenletService.BUS + '.' + name, path) 176 if proxy_obj: 177 #return dbus.Interface(proxy_obj, ScreenletService.IFACE) 178 return dbus.Interface(proxy_obj, interface) 179 except Exception, ex: 180 print _("Error in screenlets.services.get_service_by_name: %s") % str(ex) 181 return None
182
183 -def service_is_running (name):
184 """Checks if the given service is available (ie. the given Screenlet has at 185 least one running instance) and returns True or False.""" 186 bus = dbus.SessionBus() 187 if bus: 188 try: 189 path = ScreenletService.PATH + name 190 if bus.get_object(ScreenletService.BUS + '.' + name, path): 191 return True 192 except Exception: 193 pass 194 return False
195 196 #print is_service_running('Flower') 197