Package Skype4Py :: Module application
[frames] | no frames]

Source Code for Module Skype4Py.application

  1  '''APP2APP protocol. 
  2  ''' 
  3   
  4  from utils import * 
  5  from user import * 
  6  import threading 
  7   
  8   
9 -class IApplication(Cached):
10 '''Represents an application in APP2APP protocol. Use L{ISkype.Application<skype.ISkype.Application>} to instatinate. 11 ''' 12
13 - def __repr__(self):
14 return '<%s with Name=%s>' % (Cached.__repr__(self)[1:-1], repr(self.Name))
15
16 - def _Alter(self, AlterName, Args=None):
17 return self._Skype._Alter('APPLICATION', self._Name, AlterName, Args)
18
19 - def _Init(self, Name, Skype):
20 self._Name = unicode(Name) 21 self._Skype = Skype
22
23 - def _Property(self, PropName, Set=None):
24 return self._Skype._Property('APPLICATION', self._Name, PropName, Set)
25
26 - def __Connect_app_streams(self, App, Streams):
27 if App == self: 28 s = [x for x in Streams if x.PartnerHandle == self.__Connect_username] 29 if s: 30 self.__Connect_stream[0] = s[0] 31 self.__Connect_event.set()
32
33 - def Connect(self, Username, WaitConnected=False):
34 '''Connects application to user. 35 36 @param Username: Name of the user to connect to. 37 @type Username: unicode 38 @param WaitConnected: If True, causes the method to wait untill the connection is established. 39 @type WaitConnected: bool 40 @return: If C{WaitConnected} is True, returns the stream which can be used to send the data. 41 Otherwise returns None. 42 @rtype: L{IApplicationStream} or None 43 ''' 44 if WaitConnected: 45 self.__Connect_event = threading.Event() 46 self.__Connect_stream = [None] 47 self.__Connect_username = Username 48 self.__Connect_app_streams(self, self.Streams) 49 self._Skype.RegisterEventHandler('ApplicationStreams', self.__Connect_app_streams) 50 self._Alter('CONNECT', Username) 51 self.__Connect_event.wait() 52 self._Skype.UnregisterEventHandler('ApplicationStreams', self.__Connect_app_streams) 53 try: 54 return self.__Connect_stream[0] 55 finally: 56 del self.__Connect_stream, self.__Connect_event, self.__Connect_username 57 else: 58 self._Alter('CONNECT', Username)
59
60 - def Create(self):
61 '''Creates the APP2APP application in Skype client. 62 ''' 63 self._Skype._DoCommand('CREATE APPLICATION %s' % self._Name)
64
65 - def Delete(self):
66 '''Deletes the APP2APP application in Skype client. 67 ''' 68 self._Skype._DoCommand('DELETE APPLICATION %s' % self._Name)
69
70 - def SendDatagram(self, Text, Streams=None):
71 '''Sends datagram to application streams. 72 73 @param Text: Text to send. 74 @type Text: unicode 75 @param Streams: Streams to send the datagram to or None if all currently connected streams should be used. 76 @type Streams: sequence of L{IApplicationStream} 77 ''' 78 if Streams == None: 79 Streams = self.Streams 80 for s in Streams: 81 s.SendDatagram(Text)
82
83 - def _GetConnectableUsers(self):
84 return tuple([IUser(x, self._Skype) for x in esplit(self._Property('CONNECTABLE'))])
85 86 ConnectableUsers = property(_GetConnectableUsers, 87 doc='''All connectable users. 88 89 @type: tuple of L{IUser} 90 ''') 91
92 - def _GetConnectingUsers(self):
93 return tuple([IUser(x, self._Skype) for x in esplit(self._Property('CONNECTING'))])
94 95 ConnectingUsers = property(_GetConnectingUsers, 96 doc='''All users connecting at the moment. 97 98 @type: tuple of L{IUser} 99 ''') 100
101 - def _GetName(self):
102 return self._Name
103 104 Name = property(_GetName, 105 doc='''Name of the application. 106 107 @type: unicode 108 ''') 109
110 - def _GetReceivedStreams(self):
111 return tuple([IApplicationStream(x.split('=')[0], self) for x in esplit(self._Property('RECEIVED'))])
112 113 ReceivedStreams = property(_GetReceivedStreams, 114 doc='''All streams that received data and can be read. 115 116 @type: tuple of L{IApplicationStream} 117 ''') 118
119 - def _GetSendingStreams(self):
120 return tuple([IApplicationStream(x.split('=')[0], self) for x in esplit(self._Property('SENDING'))])
121 122 SendingStreams = property(_GetSendingStreams, 123 doc='''All streams that send data and at the moment. 124 125 @type: tuple of L{IApplicationStream} 126 ''') 127
128 - def _GetStreams(self):
129 return tuple([IApplicationStream(x, self) for x in esplit(self._Property('STREAMS'))])
130 131 Streams = property(_GetStreams, 132 doc='''All currently connected application streams. 133 134 @type: tuple of L{IApplicationStream} 135 ''')
136 137
138 -class IApplicationStream(Cached):
139 '''Represents an application stream in APP2APP protocol. 140 ''' 141
142 - def __len__(self):
143 return self.DataLength
144
145 - def __repr__(self):
146 return '<%s with Handle=%s, Application=%s>' % (Cached.__repr__(self)[1:-1], repr(self.Handle), repr(self.Application))
147
148 - def _Init(self, Handle, Application):
149 self._Handle = Handle 150 self._Application = Application
151
152 - def Disconnect(self):
153 '''Disconnects the stream. 154 ''' 155 self._Application._Alter('DISCONNECT', self._Handle)
156 157 close = Disconnect 158
159 - def Read(self):
160 '''Reads data from stream. 161 162 @return: Read data or an empty string if none were available. 163 @rtype: unicode 164 ''' 165 return self._Application._Alter('READ', self._Handle)
166 167 read = Read 168
169 - def SendDatagram(self, Text):
170 '''Sends datagram to stream. 171 172 @param Text: Datagram to send. 173 @type Text: unicode 174 ''' 175 self._Application._Alter('DATAGRAM', '%s %s' % (self._Handle, Text))
176
177 - def Write(self, Text):
178 '''Writes data to stream. 179 180 @param Text: Data to send. 181 @type Text: unicode 182 ''' 183 self._Application._Alter('WRITE', '%s %s' % (self._Handle, Text))
184 185 write = Write 186
187 - def _GetApplication(self):
188 return self._Application
189 190 Application = property(_GetApplication, 191 doc='''Application this stream belongs to. 192 193 @type: L{IApplication} 194 ''') 195
196 - def _GetApplicationName(self):
197 return self._Application.Name
198 199 ApplicationName = property(_GetApplicationName, 200 doc='''Name of the application this stream belongs to. Same as C{IApplicationStream.Application.Name}. 201 202 @type: unicode 203 ''') 204
205 - def __GetDataLength_GetStreamLength(self, Type):
206 for s in esplit(self._Application._Property(Type)): 207 h, i = s.split('=') 208 if h == self._Handle: 209 return int(i)
210
211 - def _GetDataLength(self):
212 i = self.__GetDataLength_GetStreamLength('SENDING') 213 if i != None: 214 return i 215 i = self.__GetDataLength_GetStreamLength('RECEIVED') 216 if i != None: 217 return i 218 return 0
219 220 DataLength = property(_GetDataLength, 221 doc='''Number of bytes awaiting in the read buffer. 222 223 @type: int 224 ''') 225
226 - def _GetHandle(self):
227 return self._Handle
228 229 Handle = property(_GetHandle, 230 doc='''Stream handle in u'<Skypename>:<n>' format. 231 232 @type: unicode 233 ''') 234
235 - def _GetPartnerHandle(self):
236 return self._Handle.split(':')[0]
237 238 PartnerHandle = property(_GetPartnerHandle, 239 doc='''Skypename of the user this stream is connected to. 240 241 @type: unicode 242 ''')
243