root/projects/WinDictator/trunk/test/mockxnee.py

Revision 19, 1.5 kB (checked in by edsuom, 1 year ago)

Importing windictator into the global FOSS repo

  • Property svn:executable set to *
Line 
1 #!/usr/bin/python
2 # Copyright (c) 2001-2004 Twisted Matrix Laboratories.
3 # See LICENSE for details.
4 #
5 # Modified by Ed Suominen 2006 for use with WinDictator unit tests.
6
7 import time
8 from twisted.internet import stdio, reactor
9 from twisted.protocols import basic
10
11
12 class KeystrokeReporterProtocol(basic.LineReceiver):
13     """
14     A protocol for a mock I{xnee} keystroke reporter
15     """
16     delimiter = '\n'
17    
18     def connectionMade(self):
19         pass
20    
21     def lineReceived(self, line):
22         """
23         Process a 'back-channel' command to send a line of mock keystroke
24         reporting.
25         """
26         cmd, keycode = line.split()
27         fields = [0]
28         if cmd == 'd':
29             fields.append(2)
30         elif cmd == 'u':
31             fields.append(3)
32         else:
33             raise Exception("Command must be 'd' or 'u'!")
34         fields.extend([0,0,0])
35         fields.append(keycode)
36         fields.append(0)
37         fields.append(self.fakeTime())
38         reportLine = ','.join([str(x) for x in fields])
39         self.transport.write(reportLine + "\n")
40
41     def fakeTime(self):
42         """
43         Returns a faked time for the report that is sort of analogous to what
44         xnee generates.
45         """
46         timeValue = divmod(int(500*time.time()),2**32)[1]
47         return "%d" % timeValue
48
49     def connectionLost(self, reason):
50         """
51         I'm not needed anymore...sniff...
52         """
53         reactor.stop()
54
55
56 if __name__ == '__main__':
57     stdio.StandardIO(KeystrokeReporterProtocol())
58     reactor.run()
Note: See TracBrowser for help on using the browser.