Package Skype4Py :: Package api
[frames] | no frames]

Source Code for Package Skype4Py.api

  1  """ 
  2  Low-level Skype API definitions. 
  3   
  4  This subpackage imports one of the: 
  5   
  6  - `Skype4Py.api.darwin` 
  7  - `Skype4Py.api.posix` 
  8  - `Skype4Py.api.windows` 
  9   
 10  modules based on the current platform. 
 11   
 12  Name of the imported module in available in the `platform` variable. 
 13   
 14  The modules implement the low-level Skype API and define options 
 15  for the `Skype.__init__` constructor. 
 16  """ 
 17  __docformat__ = 'restructuredtext en' 
 18   
 19   
 20  import sys 
 21  import threading 
 22  import logging 
 23   
 24  from Skype4Py.utils import * 
 25  from Skype4Py.enums import apiAttachUnknown 
 26  from Skype4Py.errors import SkypeAPIError 
 27   
 28   
 29  __all__ = ['Command', 'SkypeAPINotifier', 'SkypeAPI'] 
 30   
 31   
 32  DEFAULT_PROTOCOL = 5 
 33  DEFAULT_FRIENDLYNAME = u'Skype4Py' 
 34  DEFAULT_TIMEOUT = 30000 
 35   
 36   
37 -class Command(object):
38 """Represents an API command. Use `Skype.Command` to instantiate. 39 40 To send a command to Skype, use `Skype.SendCommand`. 41 """ 42
43 - def __init__(self, Command, Expected=u'', Blocking=False, Timeout=DEFAULT_TIMEOUT, Id=-1):
44 """Use `Skype.Command` to instantiate the object instead of doing it directly. 45 """ 46 47 self.Blocking = Blocking 48 """If set to True, `Skype.SendCommand` will block until the reply is received. 49 50 :type: bool""" 51 52 self.Command = tounicode(Command) 53 """Command string. 54 55 :type: unicode""" 56 57 self.Expected = tounicode(Expected) 58 """Expected reply. 59 60 :type: unicode""" 61 62 self.Id = Id 63 """Command Id. 64 65 :type: int""" 66 67 self.Reply = u'' 68 """Reply after the command has been sent and Skype has replied. 69 70 :type: unicode""" 71 72 self.Timeout = Timeout 73 """Timeout if Blocking == True. 74 75 :type: int"""
76
77 - def __repr__(self):
78 return '<%s with Command=%s, Blocking=%s, Reply=%s, Id=%s>' % \ 79 (object.__repr__(self)[1:-1], repr(self.Command), self.Blocking, repr(self.Reply), self.Id)
80
81 - def timeout2float(self):
82 """A wrapper for `api.timeout2float` function. Returns the converted 83 `Timeout` property. 84 """ 85 return timeout2float(self.Timeout)
86 87
88 -class SkypeAPINotifier(object):
89 - def attachment_changed(self, status):
90 pass
91
92 - def notification_received(self, notification):
93 pass
94
95 - def sending_command(self, command):
96 pass
97
98 - def reply_received(self, command):
99 pass
100 101
102 -class SkypeAPIBase(threading.Thread):
103 - def __init__(self):
104 threading.Thread.__init__(self, name='Skype4Py API thread') 105 self.setDaemon(True) 106 if not hasattr(self, 'logger'): 107 # Create a logger if the subclass hasn't done it already. 108 self.logger = logging.getLogger('Skype4Py.api.SkypeAPIBase') 109 self.friendly_name = DEFAULT_FRIENDLYNAME 110 self.protocol = DEFAULT_PROTOCOL 111 self.commands = {} 112 # This lock is the main mechanism to make Skype4Py thread-safe. 113 self.rlock = threading.RLock() 114 self.notifier = SkypeAPINotifier() 115 self.attachment_status = apiAttachUnknown 116 self.logger.info('opened')
117
118 - def _not_implemented(self):
119 raise SkypeAPIError('Function not implemented')
120
121 - def set_notifier(self, notifier):
122 self.notifier = notifier
123
124 - def push_command(self, command):
125 self.acquire() 126 try: 127 if command.Id < 0: 128 command.Id = 0 129 while command.Id in self.commands: 130 command.Id += 1 131 elif command.Id in self.commands: 132 raise SkypeAPIError('Command Id conflict') 133 self.commands[command.Id] = command 134 finally: 135 self.release()
136
137 - def pop_command(self, id_):
138 self.acquire() 139 try: 140 try: 141 return self.commands.pop(id_) 142 except KeyError: 143 return None 144 finally: 145 self.release()
146
147 - def acquire(self):
148 self.rlock.acquire()
149
150 - def release(self):
151 self.rlock.release()
152
153 - def close(self):
154 self.logger.info('closed')
155
156 - def set_friendly_name(self, friendly_name):
157 self.friendly_name = friendly_name
158
159 - def set_attachment_status(self, attachment_status):
160 if attachment_status != self.attachment_status: 161 self.logger.info('attachment: %s', attachment_status) 162 self.attachment_status = attachment_status 163 self.notifier.attachment_changed(attachment_status)
164
165 - def attach(self, timeout, wait=True):
166 self._not_implemented()
167
168 - def is_running(self):
169 self._not_implemented()
170
171 - def startup(self, minimized, nosplash):
172 self._not_implemented()
173
174 - def shutdown(self):
175 self._not_implemented()
176
177 - def send_command(self, command):
178 self._not_implemented()
179
180 - def security_context_enabled(self, context):
181 self._not_implemented()
182
183 - def enable_security_context(self, context):
184 self._not_implemented()
185
186 - def allow_focus(self, timeout):
187 pass
188 189
190 -def timeout2float(timeout):
191 """Converts a timeout expressed in milliseconds or seconds into a timeout expressed 192 in seconds using a floating point number. 193 194 :Parameters: 195 timeout : int, long or float 196 The input timeout. Assumed to be expressed in number of 197 milliseconds if the type is int or long. For float, assumed 198 to be a number of seconds (or fractions thereof). 199 200 :return: The timeout expressed in number of seconds (or fractions thereof). 201 :rtype: float 202 """ 203 if isinstance(timeout, float): 204 return timeout 205 return timeout / 1000.0
206 207
208 -def finalize_opts(opts):
209 """Convinient function called after popping all options from a dictionary. 210 If there are any items left, a TypeError exception is raised listing all 211 unexpected keys in the error message. 212 """ 213 if opts: 214 raise TypeError('Unexpected option(s): %s' % ', '.join(opts.keys()))
215 216 217 # Select appropriate low-level Skype API module 218 if getattr(sys, 'skype4py_setup', False): 219 # dummy for the setup.py run 220 SkypeAPI = lambda **Options: None 221 platform = '' 222 elif sys.platform.startswith('win'): 223 from windows import SkypeAPI 224 platform = 'windows' 225 elif sys.platform == 'darwin': 226 from darwin import SkypeAPI 227 platform = 'darwin' 228 else: 229 from posix import SkypeAPI 230 platform = 'posix' 231 232 233 # Note. py2exe will include the darwin but not the posix module. This seems to be the case 234 # solely because of the "posix" name. It might be a bug in py2exe or modulefinder caused 235 # by a failed attempt to import a "posix" module by the os module. If this is encountered 236 # during modulefinder scanning, the Skype4Py.api.posix is simply ignored. 237 # 238 # That being said ideally we would like to exclude both of them but I couldn't find a way 239 # to cause py2exe to skip them. I think py2exe should expose mechanisms to cooperate with 240 # extension modules aware of its existence. 241