Package Skype4Py :: Package API :: Module posix_dbus
[frames] | no frames]

Source Code for Module Skype4Py.API.posix_dbus

  1  ''' 
  2  Low level Skype for Linux interface implemented 
  3  using python-dbus package. 
  4   
  5  This module handles the options that you can pass to L{ISkype.__init__<skype.ISkype.__init__>} 
  6  for Linux machines when the transport is set to DBus. See below. 
  7   
  8  @newfield option: Option, Options 
  9   
 10  @option: C{Bus} DBus bus object as returned by python-dbus package. 
 11  If not specified, private session bus is created and used. See also C{MainLoop}. 
 12  @option: C{MainLoop} DBus mainloop object. Use only without specifying the C{Bus} 
 13  (if you use C{Bus}, pass the mainloop to the bus constructor). If neither C{Bus} or 
 14  C{MainLoop} is specified, glib mainloop is used. In such case, the mainloop is 
 15  also run on a separate thread upon attaching to Skype. If you want to use glib 
 16  mainloop but you want to run the loop yourself (for example because your GUI toolkit 
 17  does it for you), pass C{dbus.mainloop.glib.DBusGMainLoop()} object as C{MainLoop} 
 18  parameter. 
 19   
 20  @requires: Skype for Linux 2.0 (beta) or newer. 
 21  ''' 
 22   
 23  import threading 
 24  import time 
 25  import weakref 
 26  from Skype4Py.API import ICommand, _ISkypeAPIBase 
 27  from Skype4Py.enums import * 
 28  from Skype4Py.errors import ISkypeAPIError 
 29  from Skype4Py.utils import cndexp 
 30   
 31   
 32  try: 
 33      import dbus, dbus.service 
 34  except ImportError: 
 35      import sys 
 36      if sys.argv == ['(imported)']: 
 37          # we get here if we're building docs on windows, to let the module 
 38          # import without exceptions, we import our faked dbus module 
 39          from faked_dbus import dbus 
 40      else: 
 41          raise 
 42   
 43   
44 -class _SkypeNotifyCallback(dbus.service.Object):
45 '''DBus object which exports a Notify method. This will be called by Skype for all 46 notifications with the notification string as a parameter. The Notify method of this 47 class calls in turn the callable passed to the constructor. 48 ''' 49
50 - def __init__(self, bus, notify):
51 dbus.service.Object.__init__(self, bus, '/com/Skype/Client') 52 self.notify = notify
53 54 @dbus.service.method(dbus_interface='com.Skype.API.Client')
55 - def Notify(self, com):
56 self.notify(unicode(com))
57 58
59 -class _ISkypeAPI(_ISkypeAPIBase):
60 - def __init__(self, handler, opts):
61 _ISkypeAPIBase.__init__(self, opts) 62 self.RegisterHandler(handler) 63 self.skype_in = self.skype_out = self.dbus_name_owner_watch = None 64 self.bus = opts.pop('Bus', None) 65 try: 66 mainloop = opts.pop('MainLoop') 67 if self.bus != None: 68 raise TypeError('Bus and MainLoop cannot be used at the same time!') 69 except KeyError: 70 if self.bus == None: 71 import dbus.mainloop.glib 72 import gobject 73 gobject.threads_init() 74 dbus.mainloop.glib.threads_init() 75 mainloop = dbus.mainloop.glib.DBusGMainLoop() 76 self.mainloop = gobject.MainLoop() 77 if self.bus == None: 78 from dbus import SessionBus 79 self.bus = SessionBus(private=True, mainloop=mainloop) 80 if opts: 81 raise TypeError('Unexpected parameter(s): %s' % ', '.join(opts.keys()))
82
83 - def run(self):
84 self.DebugPrint('thread started') 85 if hasattr(self, 'mainloop'): 86 self.mainloop.run() 87 self.DebugPrint('thread finished')
88
89 - def Close(self):
90 if hasattr(self, 'mainloop'): 91 self.mainloop.quit() 92 self.skype_in = self.skype_out = None 93 if self.dbus_name_owner_watch != None: 94 self.bus.remove_signal_receiver(self.dbus_name_owner_watch) 95 self.dbus_name_owner_watch = None 96 self.DebugPrint('closed')
97
98 - def SetFriendlyName(self, FriendlyName):
99 self.FriendlyName = FriendlyName 100 if self.skype_out: 101 self.SendCommand(ICommand(-1, 'NAME %s' % FriendlyName))
102
103 - def Attach(self, Timeout=30000, Wait=True):
104 if self.skype_out: 105 return 106 try: 107 self.start() 108 except AssertionError: 109 pass 110 try: 111 self.wait = True 112 def ftimeout(): 113 self.wait = False
114 t = threading.Timer(Timeout / 1000.0, ftimeout) 115 if Wait: 116 t.start() 117 while self.wait: 118 try: 119 self.skype_out = self.bus.get_object('com.Skype.API', '/com/Skype') 120 self.skype_in = _SkypeNotifyCallback(self.bus, self.notify) 121 except dbus.DBusException: 122 time.sleep(1.0) 123 else: 124 break 125 else: 126 raise ISkypeAPIError('Skype attach timeout') 127 finally: 128 t.cancel() 129 self.dbus_name_owner_watch = self.bus.add_signal_receiver(self.dbus_name_owner_changed, 130 'NameOwnerChanged', 131 'org.freedesktop.DBus', 132 'org.freedesktop.DBus', 133 '/org/freedesktop/DBus', 134 arg0='com.Skype.API') 135 c = ICommand(-1, 'NAME %s' % self.FriendlyName, '', True, Timeout) 136 self.SendCommand(c) 137 if c.Reply != 'OK': 138 self.skype_in = self.skype_out = None 139 self.SetAttachmentStatus(apiAttachRefused) 140 return 141 self.SendCommand(ICommand(-1, 'PROTOCOL %s' % self.Protocol)) 142 self.SetAttachmentStatus(apiAttachSuccess)
143
144 - def IsRunning(self):
145 try: 146 self.bus.get_object('com.Skype.API', '/com/Skype') 147 return True 148 except dbus.DBusException: 149 return False
150
151 - def Start(self, Minimized=False, Nosplash=False):
152 # options are not supported as of Skype 1.4 Beta for Linux 153 if not self.IsRunning(): 154 import os 155 if os.fork() == 0: # we're child 156 os.setsid() 157 os.execlp('skype')
158
159 - def Shutdown(self):
160 import os 161 from signal import SIGINT 162 fh = os.popen('ps -o %p --no-heading -C skype') 163 pid = fh.readline().strip() 164 fh.close() 165 if pid: 166 os.kill(int(pid), SIGINT) 167 self.skype_in = self.skype_out = None
168
169 - def SendCommand(self, Command):
170 if not self.skype_out: 171 self.Attach(Command.Timeout) 172 self.CommandsStackPush(Command) 173 self.CallHandler('send', Command) 174 com = u'#%d %s' % (Command.Id, Command.Command) 175 self.DebugPrint('->', repr(com)) 176 if Command.Blocking: 177 Command._event = event = threading.Event() 178 else: 179 Command._timer = timer = threading.Timer(Command.Timeout / 1000.0, self.CommandsStackPop, (Command.Id,)) 180 try: 181 result = self.skype_out.Invoke(com) 182 except dbus.DBusException, err: 183 raise ISkypeAPIError(str(err)) 184 if result.startswith(u'#%d ' % Command.Id): 185 self.notify(result) 186 if Command.Blocking: 187 event.wait(Command.Timeout / 1000.0) 188 if not event.isSet(): 189 raise ISkypeAPIError('Skype command timeout') 190 else: 191 timer.start()
192
193 - def notify(self, com):
194 self.DebugPrint('<-', repr(com)) 195 if com.startswith(u'#'): 196 p = com.find(u' ') 197 Command = self.CommandsStackPop(int(com[1:p])) 198 if Command: 199 Command.Reply = com[p + 1:] 200 if Command.Blocking: 201 Command._event.set() 202 del Command._event 203 else: 204 Command._timer.cancel() 205 del Command._timer 206 self.CallHandler('rece', Command) 207 else: 208 self.CallHandler('rece_api', com[p + 1:]) 209 else: 210 self.CallHandler('rece_api', com)
211
212 - def dbus_name_owner_changed(self, owned, old_owner, new_owner):
213 self.DebugPrint('<-', 'dbus_name_owner_changed') 214 self.SetAttachmentStatus(cndexp(new_owner == '', 215 apiAttachNotAvailable, 216 apiAttachAvailable))
217