home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1963 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  5.3 KB  |  135 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. class ReloadWatcherThread(threading.Thread):
  20.     
  21.     def __init__(self):
  22.         self.change_detected = False
  23.         self.filename = __file__
  24.         if self.filename.endswith('c') or self.filename.endswith('o'):
  25.             self.filename = self.filename[:-1]
  26.         
  27.         self.handle = win32file.FindFirstChangeNotification(os.path.dirname(self.filename), False, win32con.FILE_NOTIFY_CHANGE_LAST_WRITE)
  28.         threading.Thread.__init__(self)
  29.  
  30.     
  31.     def run(self):
  32.         last_time = os.stat(self.filename)[stat.ST_MTIME]
  33.         while None:
  34.             
  35.             try:
  36.                 rc = win32event.WaitForSingleObject(self.handle, win32event.INFINITE)
  37.                 win32file.FindNextChangeNotification(self.handle)
  38.             except win32event.error:
  39.                 details = None
  40.                 if details[0] != winerror.ERROR_INVALID_HANDLE:
  41.                     raise 
  42.                 details[0] != winerror.ERROR_INVALID_HANDLE
  43.                 break
  44.  
  45.             this_time = os.stat(self.filename)[stat.ST_MTIME]
  46.             if this_time != last_time:
  47.                 print 'Detected file change - flagging for reload.'
  48.                 self.change_detected = True
  49.                 last_time = this_time
  50.                 continue
  51.             continue
  52.             return None
  53.  
  54.     
  55.     def stop(self):
  56.         win32file.FindCloseChangeNotification(self.handle)
  57.  
  58.  
  59.  
  60. def TransmitFileCallback(ecb, hFile, cbIO, errCode):
  61.     print 'Transmit complete!'
  62.     ecb.close()
  63.  
  64.  
  65. class Extension(SimpleExtension):
  66.     
  67.     def __init__(self):
  68.         self.reload_watcher = ReloadWatcherThread()
  69.         self.reload_watcher.start()
  70.  
  71.     
  72.     def HttpExtensionProc(self, ecb):
  73.         if self.reload_watcher.change_detected:
  74.             print 'Doing reload'
  75.             raise InternalReloadException
  76.         self.reload_watcher.change_detected
  77.         if ecb.GetServerVariable('URL').endswith('test.py'):
  78.             file_flags = win32con.FILE_FLAG_SEQUENTIAL_SCAN | win32con.FILE_FLAG_OVERLAPPED
  79.             hfile = win32file.CreateFile(__file__, win32con.GENERIC_READ, 0, None, win32con.OPEN_EXISTING, file_flags, None)
  80.             flags = isapicon.HSE_IO_ASYNC | isapicon.HSE_IO_DISCONNECT_AFTER_SEND | isapicon.HSE_IO_SEND_HEADERS
  81.             
  82.             try:
  83.                 ecb.TransmitFile(TransmitFileCallback, hfile, int(hfile), '200 OK', 0, 0, None, None, flags)
  84.             hfile.Close()
  85.             raise 
  86.  
  87.         else:
  88.             ecb.SendResponseHeaders('200 OK', 'Content-Type: text/html\r\n\r\n', 0)
  89.             print >>ecb, '<HTML><BODY>'
  90.             print >>ecb, 'The root of this site is at', ecb.MapURLToPath('/')
  91.             print >>ecb, '</BODY></HTML>'
  92.             ecb.close()
  93.         return isapicon.HSE_STATUS_SUCCESS
  94.  
  95.     
  96.     def TerminateExtension(self, status):
  97.         self.reload_watcher.stop()
  98.  
  99.  
  100.  
  101. def __ExtensionFactory__():
  102.     return Extension()
  103.  
  104.  
  105. def PreInstallDirectory(params, options):
  106.     if options.description:
  107.         params.Description = options.description
  108.     
  109.  
  110.  
  111. def PostInstall(params, options):
  112.     print 
  113.     print 'The sample has been installed.'
  114.     print 'Point your browser to /PyISAPITest'
  115.  
  116.  
  117. def status_handler(options, log, arg):
  118.     print 'Everything seems to be fine!'
  119.  
  120. custom_arg_handlers = {
  121.     'status': status_handler }
  122. if __name__ == '__main__':
  123.     from isapi.install import *
  124.     params = ISAPIParameters(PostInstall = PostInstall)
  125.     sm = [
  126.         ScriptMapParams(Extension = '*', Flags = 0)]
  127.     vd = VirtualDirParameters(Name = 'PyISAPITest', Description = Extension.__doc__, ScriptMaps = sm, ScriptMapUpdate = 'replace', PreInstall = PreInstallDirectory)
  128.     params.VirtualDirs = [
  129.         vd]
  130.     from optparse import OptionParser
  131.     parser = OptionParser('')
  132.     parser.add_option('', '--description', action = 'store', help = 'custom description to use for the virtual directory')
  133.     HandleCommandLine(params, opt_parser = parser, custom_arg_handlers = custom_arg_handlers)
  134.  
  135.