1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
|
#############################################################################
#
# Linux Desktop Testing Project http://ldtp.freedesktop.org
#
# Author:
# Nagappan Alagappan (nagappan@gmail.com)
#
# Copyright 2004 - 2007 Novell, Inc.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
# Boston, MA 02110, USA.
#
#############################################################################
import os
import re
import sys
import time
import types
import select
import struct
import socket
import random
import thread
import signal
import threading
import traceback
from xml.dom.minidom import *
from xml.parsers.expat import ExpatError
from xml.sax import saxutils
#Wildcard Specific Globals
appList = []
APPHOME = "%s/.ldtp" % os.environ['HOME']
APPROOT = "/usr/share/ldtp"
#ooldtp support Global
contextList = []
# Let us not register our application under at-spi application list
os.environ ['GTK_MODULES'] = ''
from ldtplibutils import *
_ldtpDebug = os.getenv ('LDTP_DEBUG')
_sockFdPool = sockFdPool
_socketPath = '/tmp/ldtp-record-' + os.getenv ('USER') + '-' + os.getenv ('DISPLAY')
# Default GUI timeout 30 seconds
_ldtpGuiTimeout = 30
class ConnectionLost (Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr (self.value)
# generate XML content
def generatexml (commandId, _requestId, name = None, application = None, argument = None):
_xml = '<?xml version=\"1.0\"?>'
_xml += '<REQUEST>'
# Fill action name
_xml = _xml + '<ID>' + _requestId + '</ID>'
_xml = _xml + '<COMMAND>' + str (commandId) + '</COMMAND>'
if application != None:
_xml = _xml + '<APPLICATION>' + saxutils.escape (application) + '</APPLICATION>'
if name is not None:
_xml = _xml + '<NAME>' + saxutils.escape (name) + '</NAME>'
_xml = _xml + '<ARGUMENTS>'
_xml = _xml + '<ARGUMENT>ALL</ARGUMENT>'
_xml = _xml + '</ARGUMENTS>'
_xml += '</REQUEST>'
return _xml
def connect2LdtpCodegen ():
_display = os.getenv ('DISPLAY')
if _display == None:
raise LdtpExecutionError ('Missing DISPLAY environment variable. Running in text mode ?')
_sockFd = None
_ldtpUseTcp = False
_ldtpServerAddr = None
_ldtpServerPort = None
if os.environ.has_key("LDTP_SERVER_ADDR"):
_ldtpServerAddr = os.environ ["LDTP_SERVER_ADDR"]
if os.environ.has_key ("LDTP_SERVER_PORT"):
_ldtpServerPort = int (os.environ ["LDTP_SERVER_PORT"])
else:
_ldtpServerPort = 23457
_ldtpUseTcp = True
try:
# Create a client socket
_sockFd = None
if _ldtpUseTcp:
_sockFd = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
else:
_sockFd = socket.socket (socket.AF_UNIX, socket.SOCK_STREAM)
except socket.error,msg:
if _ldtpUseTcp:
raise LdtpExecutionError ('Error while creating socket ' + str (msg))
else:
raise LdtpExecutionError ('Error while creating UNIX socket ' + str (msg))
# Let us retry connecting to the server for 3 times
_retryCount = 0
while True:
try:
try:
# Connect to server socket
if _ldtpUseTcp:
_sockFd.connect((_ldtpServerAddr, _ldtpServerPort))
else:
_sockFd.connect (_socketPath)
return _sockFd, _ldtpUseTcp
except TypeError:
raise ConnectionLost ('Environment LDTP_AUTH_SOCK variable not set')
except socket.error, msg:
if _retryCount == 3:
raise ConnectionLost ('Could not establish connection ' + str (msg))
_retryCount += 1
#If we are not trying to connect to a remote server then we can attempt to
#startup the ldtp server and then try to re-connect to it.
if not _ldtpUseTcp:
_pid = os.fork ()
if _pid == 0:
try:
os.execvpe ('ldtpcodegen', [''], os.environ)
except OSError:
raise LdtpExecutionError ('ldtp executable not in PATH')
else:
# Let us wait for 1 second, let the server starts
time.sleep (1)
def getText (nodelist):
rc = ""
for node in nodelist:
if node.nodeType == node.TEXT_NODE:
rc = rc + node.data
return rc
def getCData (nodelist):
rc = ""
for node in nodelist:
if node.nodeType == node.CDATA_SECTION_NODE:
rc = rc + node.data
return rc
def parsexml (xmldata):
"""Returns the value obtained from the server's return LDTP packet"""
_statusMsg = None
_statusCode = None
_responseType = None
_requestId = None
_responseObj = None
_data = None
try:
dom = parseString (xmldata)
try:
_responseObj = dom.getElementsByTagName ('RESPONSE')[0]
_responseType = 'response'
except IndexError:
try:
_responseObj = dom.getElementsByTagName ('NOTIFICATION')[0]
#_responseType = 'notification'
return 'notification', None, None
except IndexError:
try:
_responseObj = dom.getElementsByTagName ('KEYBOARD')[0]
#_responseType = 'keyboard'
return 'keyboard', None, None
except IndexError:
return None
try:
_responseStatusObj = _responseObj.getElementsByTagName ('STATUS')[0]
_statusCode = int (getText (_responseStatusObj.getElementsByTagName ('CODE')[0].childNodes))
except ValueError:
return None
except IndexError:
return None
try:
_statusMsg = getText (_responseStatusObj.getElementsByTagName ('MESSAGE')[0].childNodes)
except ValueError:
pass
except IndexError:
pass
try:
_requestId = getText (_responseObj.getElementsByTagName ('ID')[0].childNodes)
except IndexError:
# On notification _requestId will be empty
pass
try:
_data = getText (_responseObj.getElementsByTagName ('DATA')[0].childNodes).encode ('utf-8')
except ValueError:
pass
except IndexError:
# Data tag may not be present
pass
except ExpatError, msg:
if xml.parsers.expat.ErrorString (msg.code) == xml.parsers.expat.errors.XML_ERROR_NO_ELEMENTS:
return None
raise LdtpExecutionError ('Parsing XML error: ' + str (msg))
return _responseType, (_statusCode, _statusMsg, _requestId), _data
def parsenotificationxml (xmldata):
"""Returns the value obtained from the server's return LDTP packet"""
_windowName = None
_objectName = None
_objectType = None
_eventType = None
_key = None
_data = None
_detail1 = None
_detail2 = None
_timeElapsed = None
_notificationObj = None
try:
dom = parseString (xmldata)
try:
_notificationObj = dom.getElementsByTagName ('NOTIFICATION')[0]
except IndexError:
return None
try:
_windowName = getText (_notificationObj.getElementsByTagName ('WINDOWNAME')[0].childNodes)
_objectName = getText (_notificationObj.getElementsByTagName ('OBJECTNAME')[0].childNodes)
_objectType = getText (_notificationObj.getElementsByTagName ('OBJECTTYPE')[0].childNodes)
_eventType = getText (_notificationObj.getElementsByTagName ('EVENTTYPE')[0].childNodes)
_timeElapsed = getText (_notificationObj.getElementsByTagName ('TIMEELAPSED')[0].childNodes)
except ValueError:
return None
except IndexError:
return None
try:
_key = getText (_notificationObj.getElementsByTagName ('KEY')[0].childNodes)
except ValueError:
pass
except IndexError:
pass
try:
_data = getText (_notificationObj.getElementsByTagName ('DATA')[0].childNodes)
except ValueError:
pass
except IndexError:
pass
try:
_detail1 = getText (_notificationObj.getElementsByTagName ('DETAIL1')[0].childNodes)
except ValueError:
pass
except IndexError:
pass
try:
_detail2 = getText (_notificationObj.getElementsByTagName ('DETAIL2')[0].childNodes)
except ValueError:
pass
except IndexError:
pass
except ExpatError, msg:
if xml.parsers.expat.ErrorString (msg.code) == xml.parsers.expat.errors.XML_ERROR_NO_ELEMENTS:
return None
raise LdtpExecutionError ('Parsing XML error: ' + str (msg))
return _windowName, _objectName, _objectType, _eventType, _timeElapsed, _key, _data, _detail1, _detail2
def parsekeyboardxml (xmldata):
"""Returns the value obtained from the server's return LDTP packet"""
_keyboardData = None
_timeElapsed = None
_keyboardObj = None
try:
dom = parseString (xmldata)
try:
_keyboardObj = dom.getElementsByTagName ('KEYBOARD')[0]
except IndexError:
return None
try:
_keyboardData = getText (_keyboardObj.getElementsByTagName ('DATA')[0].childNodes)
except ValueError:
return None
except IndexError:
return None
except ExpatError, msg:
if xml.parsers.expat.ErrorString (msg.code) == xml.parsers.expat.errors.XML_ERROR_NO_ELEMENTS:
return None
raise LdtpExecutionError ('Parsing XML error: ' + str (msg))
return _keyboardData
class packetInfo:
def __init__ (self, packetType, windowName, objectName, objectType, eventType,
timeElapsed, key = None, data = None, detail1 = None,
detail2 = None):
self.packetType = packetType
self.windowName = windowName
self.objectName = objectName
self.objectType = objectType
self.eventType = eventType
self.timeElapsed = timeElapsed
self.key = key
self.data = data
self.detail1 = detail1
self.detail2 = detail2
class packet:
def __init__ (self):
self._packet = {}
self._endPacketId = 0
self._startPacketId = 0
self._pushPacketEvent = threading.Event ()
self._popPacketEvent = threading.Event ()
self._pushPacketEvent.set ()
self._popPacketEvent.set ()
def pushPacket (self, packetType, windowName = None, objectName = None, objectType = None,
eventType = None, timeElapsed = None, key = None, data = None,
detail1 = None, detail2 = None):
if self._pushPacketEvent.isSet ():
self._pushPacketEvent.wait ()
self._pushPacketEvent.clear ()
_pcktInfo = packetInfo (packetType, windowName, objectName, objectType, eventType,
timeElapsed, key, data, detail1, detail2)
self._packet [self._endPacketId] = _pcktInfo
self._endPacketId += 1
self._pushPacketEvent.set ()
def fetchPacket (self):
if self._popPacketEvent.isSet ():
self._popPacketEvent.wait ()
self._popPacketEvent.clear ()
_pcktInfo = None
if self._startPacketId in self._packet:
_pcktInfo = self._packet.pop (self._startPacketId)
else:
self._popPacketEvent.set ()
return None
self._startPacketId += 1
self._popPacketEvent.set ()
return _pcktInfo
def peekPacket (self, pcktId = None):
_pcktInfo = None
if pcktId is not None:
if pcktId in self._packet:
_pcktInfo = self._packet [pcktId]
else:
if self._startPacketId in self._packet:
_pcktInfo = self._packet [self._startPacketId]
return _pcktInfo
def getresponse (packetId = None, sockfd = None, timeOut = True):
global _ldtpGuiTimeout
count = 0
# Let us initialize with empty string, as we are going to check for None later
# returned by recvpacket later
_responsePacket = ''
while count < _ldtpGuiTimeout:
try:
_responsePacket = recvpacket (sockfd = sockfd)
break
except LdtpExecutionError, msg:
if str(msg) == 'Timeout':
if timeOut is False:
# don't timeout, so don't increase the count !
continue
# If timeout, let us retry for _ldtpGuiTimeout times
count += 1
continue
raise
if _responsePacket == None:
return None, None
try:
_responseType, _responseStatus, _responseData = parsexml (_responsePacket)
if _responseType == 'response':
# If response type is response, then let us enforce checking packetId
if _responseStatus [2] == packetId:
return _responseStatus, _responseData
else:
if _ldtpDebug:
print 'Invalid response packet', _responseStatus [2], packetId
return (-1, "Invalid response packet", packetId), (0, None)
else:
# If notification, then return directly, don't check
_windowName, _objectName, _objectType, _eventType, \
_timeElapsed, _key, _data, _detail1, \
_detail2 = parsenotificationxml (_responsePacket)
pckt.pushPacket (_responseType, _windowName, _objectName,
_objectType, _eventType, _timeElapsed, _key,
_data, _detail1, _detail2)
if _ldtpDebug:
print 'notification', _responseType, _responseStatus
return _responseType, _responseStatus
except TypeError, msg:
if _ldtpDebug:
if hasattr (traceback, 'format_exc'):
print traceback.format_exc ()
else:
print traceback.print_exc ()
if _ldtpDebug != None and _ldtpDebug == '2':
print 'TypeError', msg
return -1, "Invalid response packet"
except:
if _ldtpDebug:
if hasattr (traceback, 'format_exc'):
print traceback.format_exc ()
else:
print traceback.print_exc ()
return None, None
def doesWindowExist (windowName):
try:
# global _pollThread
_clientFd, _ldtpUseTcp = connect2LdtpCodegen ()
_requestId = str (random.randint (0, sys.maxint))
_message = generatexml (RecordCommand.WINDOWEXIST, _requestId, windowName)
sendpacket (_message, _clientFd, recorder = True)
_responseStatus, _responseData = getresponse (_requestId, _clientFd)
if _responseStatus [0] == 0:
return True
else:
return False
except:
return False
def getObjectName (objName):
try:
# global _pollThread
_clientFd, _ldtpUseTcp = connect2LdtpCodegen ()
_requestId = str (random.randint (0, sys.maxint))
_message = generatexml (RecordCommand.GETOBJECTNAME, _requestId, objName)
sendpacket (_message, _clientFd, recorder = True)
_responseStatus, _responseData = getresponse (_requestId, _clientFd)
if _responseStatus [0] == 0:
return _responseData
else:
return None
except:
if hasattr (traceback, 'format_exc'):
print traceback.format_exc ()
else:
print traceback.print_exc ()
return None
def addWait (callbackFunc, timeElapsed):
global generatedCode
if _ldtpDebug:
print 'Time elapsed', int (timeElapsed)
try:
if int (timeElapsed) > 0:
codeToBeAdded = 'wait (' + timeElapsed + ')\n'
generatedCode += codeToBeAdded
callbackFunc (codeToBeAdded)
except ValueError:
pass
#Wildcard specific function... Get applist
def getapps ():
appList = []
path = "%s/appdata" % APPHOME
if os.path.exists (path):
appListPath = path
else:
if _ldtpDebug:
print "Home copy not available"
path = "%s/appdata" % APPROOT
if os.path.exists (path):
appListPath = path
else:
if _ldtpDebug:
print "Even root copy not available"
return appList
f = open (appListPath)
for item in f:
appList.append (item.strip ())
f.close ()
return appList
#Wildcard specific function... Replace with wildcards
def wildcard (str):
global appList
if len (appList) == 0:
appList = getapps ()
for item in appList:
regex= '\"\S*?%s\S*?\"' % item
item = '\"*%s*\"' % item
str = re.compile (regex).sub (item, str, 1)
return str
#Wildcard specific function... Save applist
def saveAppList (newAppList):
global appList
appList = newAppList
path = "%s/appdata" % APPHOME
if os.path.exists (APPHOME) == False:
os.mkdir (APPHOME, 0755)
f = open (path, 'w')
for item in appList:
item = "%s\n" % item
f.write (item)
f.close ()
#oldtp support specific function... initContext
def initContext():
global contextList
contextList = []
#ooldtp support specific function... objectOrient
def objectOrient (line):
global contextList
re1 = re.compile ('\".*?\"')
shrink = re1.findall (line)[0]
re3 = re.compile ('\W|^\d*')
context = re3.sub ('', shrink)
re2 = re.compile ('\".*?\"\,\s')
if re2.search (line) != None:
parts = re2.split (line, 1)
else:
parts = re1.split (line, 1)
for all in contextList:
if all == context:
line = '%s.%s%s' % (context, parts[0], parts[1])
return line
contextList.append (context)
#last = len(context) - 1
line = '%s = context(%s)\n\t' % (context, shrink)
line = '%s%s.%s%s' % (line, context, parts[0], parts[1])
return line
# Class Prefs to manage user preferences
class Prefs:
def __init__ (self):
self.prefList = []
self.prefDic = {}
self.APPHOME = APPHOME
self.APPROOT = APPROOT
self.hash = re.compile ('^#')
self.equalTo = re.compile ('\s*\=\s*')
self.nonWhiteSpace = re.compile ('\S')
def loadPrefs (self):
path = "%s/editor.conf" % self.APPHOME
if os.path.exists (path):
prefListPath = path
else:
if _ldtpDebug:
print "Home copy not available"
path = "%s/editor.conf" % self.APPROOT
if os.path.exists (path):
prefListPath = path
else:
if _ldtpDebug:
print "Even root copy not available"
return
f = open (prefListPath)
for item in f:
self.prefList.append (item.strip ())
f.close ()
self.makePrefDic ()
return self.prefDic
def makePrefDic (self):
for item in self.prefList:
if self.hash.match (item) == None and self.nonWhiteSpace.search (item) != None:
valList = self.equalTo.split (item)
if valList [1] == 'True':
self.prefDic [valList [0]] = True
else:
self.prefDic [valList [0]] = False
def savePrefDic (self, dic):
self.prefDic = dic
for i in range (len (self.prefList)):
if self.hash.match (self.prefList[i]) == None and self.nonWhiteSpace.search (self.prefList[i]) != None:
valList = self.equalTo.split (self.prefList[i])
for param, value in self.prefDic.iteritems ():
if param == valList[0]:
if value == True:
self.prefList[i] = "%s = True" % param
else:
self.prefList[i] = "%s = False" % param
break
self.savePrefs()
def savePrefs (self):
path = "%s/editor.conf" % self.APPHOME
if os.path.exists (self.APPHOME) == False:
os.mkdir (self.APPHOME, 0755)
f = open (path, 'w')
for item in self.prefList:
item = "%s\n" % item
f.write (item)
f.close ()
def callback (guiCallbackFunc, data, timeElapsed = None):
global generatedCode
if timeElapsed is not None:
addWait (guiCallbackFunc, timeElapsed)
if data is None:
return
newlineIndex = data.find ('\n')
if newlineIndex > 0 and data [newlineIndex - 1] != ')':
tmpLines = data.split ("\n")
data = ""
# Chances that one more more line can be part of single command
# So let us use the above logic for searching \n without )
for tmpLine in tmpLines:
tmpData = ""
if tmpLine != "" and tmpLine [-1:] != ')':
tmpData = "\\n"
data = "%s%s%s" % (data, tmpLine, tmpData)
data += "\n"
#get Windownames replaced by wildcards here
data = wildcard (data)
generatedCode += data
guiCallbackFunc (data)
def processData (pckt, callbackFunc):
try:
processDataDebug (pckt, callbackFunc)
except:
if hasattr (traceback, 'format_exc'):
print traceback.format_exc ()
else:
print traceback.print_exc ()
def processDataDebug (pckt, callbackFunc):
global generatedCode
# Its a hack, please remove it, once debugging is done
while True:
try:
_pcktInfo = pckt.peekPacket ()
if _pcktInfo:
if pckt._startPacketId == pckt._endPacketId:
# If both are same, let us wait till we receive the next packet
pckt._pushPacketEvent.wait ()
if _ldtpDebug and _ldtpDebug == '2':
print 'libldtpcodegen', _pcktInfo.windowName, _pcktInfo.objectName,
print _pcktInfo.objectType, _pcktInfo.eventType,
print _pcktInfo.key, _pcktInfo.data
if _pcktInfo.packetType == 'keyboard':
# FIXME: Manipulate the keyboard strings with the text box
_data = re.sub ("\"", "\\\"", _pcktInfo.data)
callback (callbackFunc, 'enterstring (\"'+ _data + '\")\n', None)
_pcktInfo = pckt.fetchPacket ()
continue
if _pcktInfo.objectType == 'push button':
if _ldtpDebug:
print pckt._startPacketId, pckt._endPacketId
if _pcktInfo.eventType.startswith ('object:state-changed:armed') and \
pckt._startPacketId != pckt._endPacketId:
_tmpPckt = pckt.peekPacket (pckt._startPacketId + 1)
if _ldtpDebug and _ldtpDebug == 2 and _tmpPckt != None:
print '*** tmpPckt', _tmpPckt
if _tmpPckt == None or (_tmpPckt.objectType == _pcktInfo.objectType and \
_tmpPckt.windowName == _pcktInfo.windowName and \
_tmpPckt.objectName == _pcktInfo.objectName and \
_tmpPckt.eventType.startswith ('focus:')):
_data = re.sub ("\"", "\\\"", _pcktInfo.objectName)
callback (callbackFunc, 'click (\"'+ _pcktInfo.windowName + \
'\", \"' + _data + '\")\n', _pcktInfo.timeElapsed)
_pcktInfo = pckt.fetchPacket ()
_pcktInfo = pckt.fetchPacket () # Note we are fetching two times
continue
elif _pcktInfo.eventType.startswith ('focus:') and doesWindowExist (_pcktInfo.windowName):
if _ldtpDebug:
print '*** Window exist'
_pcktInfo = pckt.fetchPacket ()
continue
elif _pcktInfo.eventType.startswith ('focus:') and \
pckt._startPacketId != pckt._endPacketId:
_tmpPckt = pckt.peekPacket (pckt._startPacketId + 1)
if _ldtpDebug and _ldtpDebug == 2:
print '*** tmpPckt', _tmpPckt
if _tmpPckt == None or (_tmpPckt.objectType == _pcktInfo.objectType and \
_tmpPckt.windowName == _pcktInfo.windowName and \
_tmpPckt.objectName == _pcktInfo.objectName and \
_tmpPckt.eventType.startswith ('object:state-changed:armed')):
_data = re.sub ("\"", "\\\"", _pcktInfo.objectName)
callback (callbackFunc, 'click (\"'+ _pcktInfo.windowName + \
'\", \"' + _data + '\")\n',
_pcktInfo.timeElapsed)
_pcktInfo = pckt.fetchPacket ()
_pcktInfo = pckt.fetchPacket () # Note we are fetching two times
continue
_data = re.sub ("\"", "\\\"", _pcktInfo.objectName)
callback (callbackFunc, 'click (\"'+ _pcktInfo.windowName + \
'\", \"' + _data + '\")\n', _pcktInfo.timeElapsed)
_pcktInfo = pckt.fetchPacket ()
continue
elif _pcktInfo.eventType.startswith ('object:state-changed:checked'):
if _pcktInfo.objectType == 'check box':
_data = re.sub ("\"", "\\\"", _pcktInfo.objectName)
if _pcktInfo.detail1 == '1':
callback (callbackFunc, 'check (\"'+ _pcktInfo.windowName + \
'\", \"' + _data + '\")\n',
_pcktInfo.timeElapsed)
else:
callback (callbackFunc, 'uncheck (\"'+ _pcktInfo.windowName + \
'\", \"' + _data + '\")\n',
_pcktInfo.timeElapsed)
_pcktInfo = pckt.fetchPacket ()
continue
elif _pcktInfo.objectType == 'radio button' or \
_pcktInfo.objectType == 'toggle button':
_data = re.sub ("\"", "\\\"", _pcktInfo.objectName)
callback (callbackFunc, 'click (\"'+ _pcktInfo.windowName + \
'\", \"' + _data + '\")\n',
_pcktInfo.timeElapsed)
_pcktInfo = pckt.fetchPacket ()
continue
else:
_pcktInfo = pckt.fetchPacket ()
if _ldtpDebug:
print 'Invalid packet with object:state-changed:checked event type ??'
continue
elif _pcktInfo.eventType.startswith ('object:state-changed:selected'):
if _pcktInfo.objectType == 'page tab':
_responseData = getObjectName (_pcktInfo.objectName)
if _responseData is not None:
_data = re.sub ("\"", "\\\"", _pcktInfo.data)
callback (callbackFunc, 'selecttab (\"'+ _pcktInfo.windowName + \
'\", \"' + _responseData + '\", \"' + \
_data + '\")\n',
_pcktInfo.timeElapsed)
_pcktInfo = pckt.fetchPacket ()
continue
elif _pcktInfo.objectType == 'table cell':
_responseData = getObjectName (_pcktInfo.objectName)
if _responseData is not None:
# Escape " as \" in data argument
_data = re.sub ("\"", "\\\"", _pcktInfo.data)
callback (callbackFunc, 'selectrow (\"'+ _pcktInfo.windowName + \
'\", \"' + _responseData + '\", \"' + \
_data + '\")\n',
_pcktInfo.timeElapsed)
_pcktInfo = pckt.fetchPacket ()
continue
else:
_pcktInfo = pckt.fetchPacket ()
if _ldtpDebug:
print 'Invalid packet with object:state-changed:selected event type ??'
continue
elif _pcktInfo.eventType.startswith ('focus:') and \
_pcktInfo.objectType == 'table cell':
_tmpPckt = None
if pckt._startPacketId != pckt._endPacketId:
_tmpPckt = pckt.peekPacket (pckt._startPacketId + 1)
if _tmpPckt is not None and str (_tmpPckt.eventType).startswith ('focus:') == False and \
_tmpPckt.objectType != _pcktInfo.objectType:
_tmpPckt = None
if _tmpPckt == None or (_tmpPckt.objectType == _pcktInfo.objectType and \
_tmpPckt.windowName == _pcktInfo.windowName and \
_tmpPckt.objectName == _pcktInfo.objectName and \
_tmpPckt.eventType.startswith ('focus:')):
_responseData = getObjectName (_pcktInfo.objectName)
if _responseData is not None:
_data = re.sub ("\"", "\\\"", _pcktInfo.data)
callback (callbackFunc, 'selectrow (\"'+ _pcktInfo.windowName + \
'\", \"' + _responseData + '\", \"' + \
_data + '\")\n', _pcktInfo.timeElapsed)
_pcktInfo = pckt.fetchPacket ()
if _tmpPckt is not None:
_pcktInfo = pckt.fetchPacket ()
continue
elif _pcktInfo.eventType.startswith ('window:create'):
callback (callbackFunc, 'waittillguiexist (\"' + _pcktInfo.windowName + '\")\n')
_pcktInfo = pckt.fetchPacket ()
continue
elif _pcktInfo.eventType.startswith ('window:destroy'):
callback (callbackFunc, 'waittillguinotexist (\"' + _pcktInfo.windowName + '\")\n')
_pcktInfo = pckt.fetchPacket ()
continue
elif _pcktInfo.objectType == 'spin button':
_data = re.sub ("\"", "\\\"", _pcktInfo.data)
callback (callbackFunc, 'setvalue (\"'+ _pcktInfo.windowName + \
'\", \"' + _pcktInfo.objectName + '\", \"' + \
_data + '\")\n',
_pcktInfo.timeElapsed)
_pcktInfo = pckt.fetchPacket ()
# if pckt._startPacketId != pckt._endPacketId:
# _tmpPckt = pckt.peekPacket (pckt._startPacketId + 1)
# if _ldtpDebug and _ldtpDebug == 2:
# print '*** tmpPckt', _tmpPckt
# if _tmpPckt == None or (_tmpPckt.objectType == _pcktInfo.objectType and \
# _tmpPckt.windowName == _pcktInfo.windowName and \
# _tmpPckt.objectName == _pcktInfo.objectName):
# addWait (generatedCode, callbackFunc, _pcktInfo.timeElapsed)
# generatedCode += 'setvalue (\"'+ _pcktInfo.windowName + \
# '\", \"' + _pcktInfo.objectName + '\", \"' + \
# _pcktInfo.data + '\")\n'
# callbackFunc ('setvalue (\"'+ _pcktInfo.windowName + \
# '\", \"' + _pcktInfo.objectName + '\", \"' + \
# _pcktInfo.data + '\")\n')
# if _tmpPckt is not None:
# _pcktInfo = pckt.fetchPacket ()
# _pcktInfo = pckt.fetchPacket ()
# else:
# addWait (generatedCode, callbackFunc, _pcktInfo.timeElapsed)
# generatedCode += 'setvalue (\"'+ _pcktInfo.windowName + \
# '\", \"' + _pcktInfo.objectName + '\", \"' + \
# _pcktInfo.data + '\")\n'
# callbackFunc ('setvalue (\"'+ _pcktInfo.windowName + \
# '\", \"' + _pcktInfo.objectName + '\", \"' + \
# _pcktInfo.data + '\")\n')
# _pcktInfo = pckt.fetchPacket ()
continue
elif _pcktInfo.objectType == 'text':
_objName = _pcktInfo.objectName
if re.search ('|', _objName) is not None:
_responseData = getObjectName (_pcktInfo.objectName)
if _responseData is not None:
_objName = _responseData
_data = re.sub ("\"", "\\\"", _pcktInfo.data)
callback (callbackFunc, 'settextvalue (\"'+ _pcktInfo.windowName + \
'\", \"' + _objName + '\", \"' + \
_data + '\")\n',
_pcktInfo.timeElapsed)
_pcktInfo = pckt.fetchPacket ()
continue
elif _pcktInfo.objectType == 'combo box':
_data = re.sub ("\"", "\\\"", _pcktInfo.data)
callback (callbackFunc, 'comboselect (\"'+ _pcktInfo.windowName + \
'\", \"' + _pcktInfo.objectName + '\", \"' + \
_data + '\")\n',
_pcktInfo.timeElapsed)
_pcktInfo = pckt.fetchPacket ()
continue
elif _pcktInfo.objectType == 'menu item':
_data = re.sub ("\"", "\\\"", _pcktInfo.objectName)
callback (callbackFunc, 'selectmenuitem (\"'+ _pcktInfo.windowName + \
'\", \"' + _data + '\")\n',
_pcktInfo.timeElapsed)
_pcktInfo = pckt.fetchPacket ()
continue
elif _pcktInfo.objectType == 'right click':
_objName = _pcktInfo.objectName
if re.search ('|', _objName) is not None:
_responseData = getObjectName (_pcktInfo.objectName)
if _responseData is not None:
_objName = _responseData
_data = re.sub ("\"", "\\\"", _pcktInfo.data)
callback (callbackFunc, 'rightclick (\"'+ _pcktInfo.windowName + \
'\", \"' + _objName + '\", \"' + \
_data + '\")\n',
_pcktInfo.timeElapsed)
_pcktInfo = pckt.fetchPacket ()
continue
elif _pcktInfo.objectType == 'panel' and \
_pcktInfo.eventType.startswith ('object:state-changed:focused'):
_objName = _pcktInfo.objectName
if re.search ('|', _objName) is not None:
_responseData = getObjectName (_pcktInfo.objectName)
if _responseData is not None:
_objName = _responseData
_data = re.sub ("\"", "\\\"", _pcktInfo.data)
callback (callbackFunc, 'selectpanelname (\"'+ _pcktInfo.windowName + \
'\", \"' + _objName + '\", \"' + \
_data + '\")\n',
_pcktInfo.timeElapsed)
_pcktInfo = pckt.fetchPacket ()
continue
else:
if _ldtpDebug:
print 'Unknown object type', _pcktInfo.objectType
_pcktInfo = pckt.fetchPacket ()
continue
else:
time.sleep (1)
except KeyboardInterrupt:
break
except:
if hasattr (traceback, 'format_exc'):
print traceback.format_exc ()
else:
print traceback.print_exc ()
pckt.fetchPacket ()
raise
def stoprecord (filename = None):
try:
# global _pollThread
_clientFd, _ldtpUseTcp = connect2LdtpCodegen ()
_requestId = str (random.randint (0, sys.maxint))
_message = generatexml (RecordCommand.STOP, _requestId)
sendpacket (_message, _clientFd, recorder = True)
_responseStatus, _responseData = getresponse (_requestId, _clientFd)
except:
pass
def shutdown ():
if calledFromGui == True:
return
if threading.activeCount () > 1:
thread.exit ()
sys.exit ()
def __shutdownAndExit (signum, frame):
global generatedCode, _mainSock
if _mainSock:
_mainSock.close ()
_mainSock = None
stoprecord ()
shutdown ()
if _ldtpDebug:
print ''
print ''
print ''
print generatedCode.encode ('utf8')
print ''
print ''
print ''
def stop ():
__shutdownAndExit (0, 0)
# Create read flag
_readFlag = threading.Event ()
# Clear the flag by default
_readFlag.clear ()
# Create notification flag
_notificationFlag = threading.Event ()
# Set the flag by default
_notificationFlag.set ()
# Create keyboard flag
_keyboardFlag = threading.Event ()
# Set the flag by default
_keyboardFlag.set ()
# Contains poll fd's
# _serverPoll = None
# Send lock
_sendLck = threading.Lock ()
# Recieve lock
_recvLck = threading.Lock ()
_mainSock = None
# _pollThread = None
calledFromGui = False
generatedCode = ''
pckt = packet ()
def invokecallback (_clientFd):
global _mainSock
_mainSock = _clientFd
try:
_requestId = str (random.randint (0, sys.maxint))
_message = generatexml (RecordCommand.NOTIFICATION, _requestId)
sendpacket (_message, _clientFd, recorder = True)
except KeyboardInterrupt:
if _ldtpDebug:
print 'Keyboard interrupt'
return
except socket.error:
if _ldtpDebug:
print 'Socket error'
return
except:
if _ldtpDebug:
if hasattr (traceback, 'format_exc'):
print traceback.format_exc ()
else:
print traceback.print_exc ()
while True:
try:
_responseStatus, _responseData = getresponse (sockfd = _clientFd,
timeOut = False)
if _responseStatus == None:
if _mainSock:
_mainSock.close ()
_mainSock = None
# Server closed connection
if _ldtpDebug:
print 'Server closed connection'
break
except KeyboardInterrupt:
if _ldtpDebug:
print 'Keyboard interrupt'
break
except LdtpExecutionError, error:
if _ldtpDebug:
print 'Ldtp execution error %s' % str (error)
break
except socket.error:
if _ldtpDebug:
print 'Socket error'
break
except:
if _ldtpDebug:
if hasattr (traceback, 'format_exc'):
print traceback.format_exc ()
else:
print traceback.print_exc ()
break
#All default parameters, and checking whether action is stoprecord, are to be removed
def start (callbackFunc, appName = 'ALL', args = None):
if callbackFunc == None or callable (callbackFunc) == False:
raise LdtpExecutionError ('Callback function is not callable')
global generatedCode
generatedCode = 'from ldtp import *\n\n'
_clientFd, _ldtpUseTcp = connect2LdtpCodegen ()
_sockFdPool = sockFdPool
_sockFdPool [threading.currentThread ()] = _clientFd
thread.start_new_thread (invokecallback, (_clientFd, ))
thread.start_new_thread (processData, (pckt, callbackFunc))
|