home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_2750 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  5.5 KB  |  157 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from win32com.client import DispatchWithEvents, Dispatch
  5. import msvcrt
  6. import pythoncom
  7. import time
  8. import sys
  9. import types
  10. import threading
  11. stopEvent = threading.Event()
  12.  
  13. def TestExcel():
  14.     
  15.     class ExcelEvents:
  16.         
  17.         def OnNewWorkbook(self, wb):
  18.             if type(wb) != types.InstanceType:
  19.                 raise RuntimeError, 'The transformer doesnt appear to have translated this for us!'
  20.             type(wb) != types.InstanceType
  21.             self.seen_events['OnNewWorkbook'] = None
  22.  
  23.         
  24.         def OnWindowActivate(self, wb, wn):
  25.             if type(wb) != types.InstanceType or type(wn) != types.InstanceType:
  26.                 raise RuntimeError, 'The transformer doesnt appear to have translated this for us!'
  27.             type(wn) != types.InstanceType
  28.             self.seen_events['OnWindowActivate'] = None
  29.  
  30.         
  31.         def OnWindowDeactivate(self, wb, wn):
  32.             self.seen_events['OnWindowDeactivate'] = None
  33.  
  34.         
  35.         def OnSheetDeactivate(self, sh):
  36.             self.seen_events['OnSheetDeactivate'] = None
  37.  
  38.         
  39.         def OnSheetBeforeDoubleClick(self, Sh, Target, Cancel):
  40.             if Target.Column % 2 == 0:
  41.                 print 'You can double-click there...'
  42.             else:
  43.                 print 'You can not double-click there...'
  44.                 return 1
  45.             return Target.Column % 2 == 0
  46.  
  47.  
  48.     
  49.     class WorkbookEvents:
  50.         
  51.         def OnActivate(self):
  52.             print 'workbook OnActivate'
  53.  
  54.         
  55.         def OnBeforeRightClick(self, Target, Cancel):
  56.             print "It's a Worksheet Event"
  57.  
  58.  
  59.     e = DispatchWithEvents('Excel.Application', ExcelEvents)
  60.     e.seen_events = { }
  61.     e.Visible = 1
  62.     book = e.Workbooks.Add()
  63.     book = DispatchWithEvents(book, WorkbookEvents)
  64.     print 'Have book', book
  65.     print 'Double-click in a few of the Excel cells...'
  66.     print 'Press any key when finished with Excel, or wait 10 seconds...'
  67.     if not _WaitForFinish(e, 10):
  68.         e.Quit()
  69.     
  70.     if not _CheckSeenEvents(e, [
  71.         'OnNewWorkbook',
  72.         'OnWindowActivate']):
  73.         sys.exit(1)
  74.     
  75.  
  76.  
  77. def TestWord():
  78.     
  79.     class WordEvents:
  80.         
  81.         def OnDocumentChange(self):
  82.             self.seen_events['OnDocumentChange'] = None
  83.  
  84.         
  85.         def OnWindowActivate(self, doc, wn):
  86.             self.seen_events['OnWindowActivate'] = None
  87.  
  88.         
  89.         def OnQuit(self):
  90.             self.seen_events['OnQuit'] = None
  91.             stopEvent.set()
  92.  
  93.  
  94.     w = DispatchWithEvents('Word.Application', WordEvents)
  95.     w.seen_events = { }
  96.     w.Visible = 1
  97.     w.Documents.Add()
  98.     print 'Press any key when finished with Word, or wait 10 seconds...'
  99.     if not _WaitForFinish(w, 10):
  100.         w.Quit()
  101.     
  102.     if not _CheckSeenEvents(w, [
  103.         'OnDocumentChange',
  104.         'OnWindowActivate']):
  105.         sys.exit(1)
  106.     
  107.  
  108.  
  109. def _WaitForFinish(ob, timeout):
  110.     end = time.time() + timeout
  111.     while msvcrt.kbhit():
  112.         msvcrt.getch()
  113.         break
  114.     pythoncom.PumpWaitingMessages()
  115.     stopEvent.wait(0.2)
  116.     if stopEvent.isSet():
  117.         stopEvent.clear()
  118.         break
  119.     
  120.     
  121.     try:
  122.         if not ob.Visible:
  123.             return 0
  124.     except pythoncom.com_error:
  125.         pass
  126.  
  127.     if time.time() > end:
  128.         return 0
  129.     continue
  130.     return 1
  131.  
  132.  
  133. def _CheckSeenEvents(o, events):
  134.     rc = 1
  135.     for e in events:
  136.         if not o.seen_events.has_key(e):
  137.             print 'ERROR: Expected event did not trigger', e
  138.             rc = 0
  139.             continue
  140.     
  141.     return rc
  142.  
  143.  
  144. def test():
  145.     import sys
  146.     if 'noword' not in sys.argv[1:]:
  147.         TestWord()
  148.     
  149.     if 'noexcel' not in sys.argv[1:]:
  150.         TestExcel()
  151.     
  152.     print 'Word and Excel event tests passed.'
  153.  
  154. if __name__ == '__main__':
  155.     test()
  156.  
  157.