Back to index

plone3  3.1.7
dreamweaver.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2004 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 Dreamweaver Plugin
00015 
00016 Contributed by Manuel Aristarann (relicensed by permission)
00017 $Id: dreamweaver.py 67503 2004-07-12 21:32:44Z caseman $
00018 """
00019 
00020 from time import sleep
00021 import win32process
00022 import win32con
00023 import win32event
00024 import win32api
00025 import os
00026 from win32com.client import GetObject
00027 
00028 
00029 class EditorProcess:
00030     def __init__(self, file):
00031         """Launch editor process"""
00032         # let's see if DW's already running
00033         # kind of a hack, but...
00034         WMI = GetObject('winmgmts:')
00035         dwprocesses = WMI.ExecQuery(
00036             'select * from Win32_Process where Name="Dreamweaver.exe"')
00037        dwPath = getDWPathFromRegistry()
00038         if dwPath is None:
00039            raise RuntimeError('Cannot find dreamweaver.exe')
00040         processinfo = win32process.CreateProcess(
00041                 None,
00042                 '%s %s' % (dwPath, file),
00043                 None,   
00044                 None,
00045                 1,
00046                 win32process.CREATE_NEW_CONSOLE,
00047                 None,
00048                 None,
00049                 win32process.STARTUPINFO())
00050         if len(dwprocesses):
00051            # DW is already running
00052             self.pid = dwprocesses[0].Properties_('ProcessId').Value
00053             self.hProcess = win32api.OpenProcess(
00054                win32con.PROCESS_ALL_ACCESS, 0, self.pid)
00055        else:
00056             self.hProcess, nil, self.pid, nil = processinfo
00057         
00058     def wait(self, timeout):
00059         """Wait for editor to exit or until timeout"""
00060         win32event.WaitForSingleObject(self.hProcess, int(timeout * 1000.0))
00061                     
00062     def isAlive(self):
00063         """Returns true if the editor process is still alive"""
00064         return win32process.GetExitCodeProcess(self.hProcess) == 259
00065 
00066 
00067 # must get Dreamweaver's path from registry
00068 from win32api import RegOpenKey, RegQueryValue, RegCloseKey
00069 from win32con import HKEY_LOCAL_MACHINE
00070 
00071 def getDWPathFromRegistry():
00072     try:
00073         hkey = RegOpenKey(HKEY_LOCAL_MACHINE,
00074                    r'SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths')
00075         value = RegQueryValue(hkey, 'Dreamweaver.exe')
00076     except:
00077         value = None
00078     RegCloseKey(hkey)
00079     return value
00080 
00081 
00082 def test():
00083     print 'Spawining DW Process...'
00084     f = EditorProcess('C:\\test.html')
00085     if f.isAlive():
00086         print 'yes'
00087         print 'Test Passed.'
00088     else:
00089         print 'no'
00090         print 'Test Failed.'
00091     
00092 if __name__ == '__main__':
00093     test()
00094