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

Source Code for Module Skype4Py.application

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