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