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