Back to index

plone3  3.1.7
homesite5.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2001, 2002 Zope Corporation and Contributors.
00004 # All Rights Reserved.
00005 # 
00006 # This software is subject to the provisions of the Zope Public License,
00007 # Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
00008 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00009 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00010 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00011 # FOR A PARTICULAR PURPOSE.
00012 # 
00013 ##############################################################################
00014 """External Editor HomeSite Plugin
00015 
00016 $Id: homesite5.py 67450 2002-09-01 05:03:43Z caseman $
00017 """
00018 
00019 from time import sleep
00020 import win32com
00021 from win32com import client # Initialize Client module
00022 
00023 class EditorProcess:
00024     def __init__(self, file):
00025         """Launch editor process"""
00026         hs = win32com.client.Dispatch('AllaireClientApp.TAllaireClientApp')
00027         # Try to open the file, keep retrying until we succeed or timeout
00028         i = 0
00029         timeout = 45
00030         while i < timeout:
00031             try:
00032                 hs.OpenFile(file)
00033             except:
00034                 i += 1
00035                 if i >= timeout:
00036                     raise RuntimeError('Could not launch Homesite.')
00037                 sleep(1)
00038             else:
00039                 break
00040         self.hs = hs
00041         self.file = file
00042         
00043     def wait(self, timeout):
00044         """Wait for editor to exit or until timeout"""
00045         sleep(timeout)
00046             
00047     def isAlive(self):
00048         """Returns true if the editor process is still alive"""
00049         return self.hs.IsFileOpen(self.file)
00050 
00051 def test():
00052     import os
00053     from time import sleep
00054     from tempfile import mktemp
00055     fn = mktemp('.html')
00056     f = open(fn, 'w')
00057     f.write('<html>\n  <head></head>\n  <body>\n  </body>\n</html>')
00058     f.close()
00059     print 'Connecting to HomeSite...'
00060     f = EditorProcess(fn)
00061     print 'Attached to %s %s' % (`f.hs`, f.hs.VersionText)
00062     print ('%s is open...' % fn),
00063     if f.isAlive():
00064         print 'yes'
00065         print 'Test Passed.'
00066     else:
00067         print 'no'
00068         print 'Test Failed.'
00069     
00070 if __name__ == '__main__':
00071     test()