Back to index

plone3  3.1.7
photoshp.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 Photoshop Plugin
00015 
00016 $Id: photoshp.py 67472 2003-03-31 22:26:18Z caseman $
00017 """
00018 
00019 # Note that Photoshop's com API is not terribly rich and external editor
00020 # cannot discern from it when a Photoshop file has been closed.
00021 # Therefore Photoshop should probably be used without DAV locks or
00022 # with always_borrow_locks enabled
00023 
00024 from time import sleep
00025 import win32com
00026 from win32com import client # Initialize Client module
00027 
00028 class EditorProcess:
00029     def __init__(self, file):
00030         """Launch editor process"""
00031         ps = win32com.client.Dispatch('Photoshop.Application')
00032         # Try to open the file, keep retrying until we succeed or timeout
00033         i = 0
00034         timeout = 45
00035         while i < timeout:
00036             try:
00037                 fileconn = ps.Open(file)
00038             except:
00039                 i += 1
00040                 if i >= timeout:
00041                     raise RuntimeError('Could not launch Photoshop.')
00042                 sleep(1)
00043             else:
00044                 break
00045         self.fileconn = fileconn
00046         self.file = file
00047         
00048     def wait(self, timeout):
00049         """Wait for editor to exit or until timeout"""
00050         sleep(timeout)
00051             
00052     def isAlive(self):
00053         """Returns true if the editor process is still alive"""
00054         # Photoshop has no API for checking if a file is still open
00055         # This workaround just checks if the file connection is
00056         # still accessible. It will be until Photoshop itself is closed 8^/
00057         try:
00058             self.fileconn.Title # See if the file is still accessible
00059         except:
00060             return 0
00061         return 1
00062 
00063 def test():
00064     print 'Connecting to Photoshop...'
00065     f = EditorProcess('C:\\Windows\\Cloud.gif')
00066     print ('%s is open...' % f.fileconn.Title),
00067     if f.isAlive():
00068         print 'yes'
00069         print 'Test Passed.'
00070     else:
00071         print 'no'
00072         print 'Test Failed.'
00073     
00074 if __name__ == '__main__':
00075     test()