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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from isapi import isapicon
  5. from isapi.simple import SimpleExtension
  6. import sys
  7. import os
  8. import stat
  9. if hasattr(sys, 'isapidllhandle'):
  10.     import win32traceutil
  11.  
  12. from isapi import InternalReloadException
  13. import win32event
  14. import win32file
  15. import winerror
  16. import win32con
  17. import threading
  18.  
  19. try:
  20.     reload_counter += 1
  21. except NameError:
  22.     reload_counter = 0
  23.  
  24.  
  25. class ReloadWatcherThread(threading.Thread):
  26.     
  27.     def __init__(self):
  28.         self.change_detected = False
  29.         self.filename = __file__
  30.         if self.filename.endswith('c') or self.filename.endswith('o'):
  31.             self.filename = self.filename[:-1]
  32.         
  33.         self.handle = win32file.FindFirstChangeNotification(os.path.dirname(self.filename), False, win32con.FILE_NOTIFY_CHANGE_LAST_WRITE)
  34.         threading.Thread.__init__(self)
  35.  
  36.     
  37.     def run(self):
  38.         last_time = os.stat(self.filename)[stat.ST_MTIME]
  39.         while None:
  40.             
  41.             try:
  42.                 rc = win32event.WaitForSingleObject(self.handle, win32event.INFINITE)
  43.                 win32file.FindNextChangeNotification(self.handle)
  44.             except win32event.error:
  45.                 details = None
  46.                 if details[0] != winerror.ERROR_INVALID_HANDLE:
  47.                     raise 
  48.                 details[0] != winerror.ERROR_INVALID_HANDLE
  49.                 break
  50.  
  51.             this_time = os.stat(self.filename)[stat.ST_MTIME]
  52.             if this_time != last_time:
  53.                 print 'Detected file change - flagging for reload.'
  54.                 self.change_detected = True
  55.                 last_time = this_time
  56.                 continue
  57.             continue
  58.             return None
  59.  
  60.     
  61.     def stop(self):
  62.         win32file.FindCloseChangeNotification(self.handle)
  63.  
  64.  
  65.  
  66. class Extension(SimpleExtension):
  67.     
  68.     def __init__(self):
  69.         self.reload_watcher = ReloadWatcherThread()
  70.         self.reload_watcher.start()
  71.  
  72.     
  73.     def HttpExtensionProc(self, ecb):
  74.         if self.reload_watcher.change_detected:
  75.             print 'Doing reload'
  76.             raise InternalReloadException
  77.         self.reload_watcher.change_detected
  78.         ecb.SendResponseHeaders('200 OK', 'Content-Type: text/html\r\n\r\n', 0)
  79.         print >>ecb, '<HTML><BODY>'
  80.         print >>ecb, 'This module has been imported'
  81.         print >>ecb, '%d times' % (reload_counter,)
  82.         print >>ecb, '</BODY></HTML>'
  83.         ecb.close()
  84.         return isapicon.HSE_STATUS_SUCCESS
  85.  
  86.     
  87.     def TerminateExtension(self, status):
  88.         self.reload_watcher.stop()
  89.  
  90.  
  91.  
  92. def __ExtensionFactory__():
  93.     return Extension()
  94.  
  95.  
  96. def PreInstallDirectory(params, options):
  97.     if options.description:
  98.         params.Description = options.description
  99.     
  100.  
  101.  
  102. def PostInstall(params, options):
  103.     print 
  104.     print 'The sample has been installed.'
  105.     print 'Point your browser to /AdvancedPythonSample'
  106.     print 'If you modify the source file and reload the page,'
  107.     print 'you should see the reload counter increment'
  108.  
  109.  
  110. def status_handler(options, log, arg):
  111.     print 'Everything seems to be fine!'
  112.  
  113. custom_arg_handlers = {
  114.     'status': status_handler }
  115. if __name__ == '__main__':
  116.     from isapi.install import *
  117.     params = ISAPIParameters(PostInstall = PostInstall)
  118.     sm = [
  119.         ScriptMapParams(Extension = '*', Flags = 0)]
  120.     vd = VirtualDirParameters(Name = 'AdvancedPythonSample', Description = Extension.__doc__, ScriptMaps = sm, ScriptMapUpdate = 'replace', PreInstall = PreInstallDirectory)
  121.     params.VirtualDirs = [
  122.         vd]
  123.     from optparse import OptionParser
  124.     parser = OptionParser('')
  125.     parser.add_option('', '--description', action = 'store', help = 'custom description to use for the virtual directory')
  126.     HandleCommandLine(params, opt_parser = parser, custom_arg_handlers = custom_arg_handlers)
  127.  
  128.