Back to index

plone3  3.1.7
msohtmed.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 msohtmed Plugin
00015 
00016 $Id: msohtmed.py 67472 2003-03-31 22:26:18Z caseman $
00017 """
00018 
00019 import os
00020 from time import sleep
00021 import win32com
00022 from win32com import client # Initialize Client module
00023 
00024 class EditorProcess:
00025     def __init__(self, file):
00026         """Launch editor process"""
00027         word = win32com.client.Dispatch('Word.Application')
00028         # Try to open the file, keep retrying until we succeed or timeout
00029         i = 0
00030         timeout = 45
00031         while i < timeout:
00032             try:
00033                 word.Documents.Open(file, Format=8)
00034               print 'Opening...'
00035             except:
00036                 i += 1
00037                 if i >= timeout:
00038                     raise RuntimeError('Could not launch Word.')
00039                 sleep(1)
00040             else:
00041                 break
00042         word.Visible = 1
00043         self.wordapp = word
00044         self.file = file
00045         
00046     def wait(self, timeout):
00047         """Wait for editor to exit or until timeout"""
00048         sleep(timeout)
00049             
00050     def isAlive(self):
00051         """Returns true if the editor process is still alive"""
00052               try:
00053             self.wordapp.Documents(self.file)
00054         except:
00055             return 0
00056         else:
00057             return 1
00058         
00059 def test():
00060     import os
00061     from time import sleep
00062     from tempfile import mktemp
00063     fn = mktemp('.html')
00064     f = open(fn, 'w')
00065     #f.write('<html>\n  <head></head>\n'
00066     #        '<body><h1>A Test Doc</h1></body>\n</html>')
00067     f.write('<p>hmmm</p>')
00068     f.close()
00069     print 'Connecting to Word...'
00070     f = EditorProcess(fn)
00071     print 'Attached to %s %s' % (`f.wordapp`, f.wordapp.Version)
00072     print ('%s is open...' % fn),
00073     if f.isAlive():
00074         print 'yes'
00075         print 'Test Passed.'
00076     else:
00077         print 'no'
00078         print 'Test Failed.'
00079     
00080 if __name__ == '__main__':
00081     test()