Revert "Upload avec script adhoc."
[Portfolio.git] / manipulation.py
index e27ab1e..9f65866 100755 (executable)
@@ -1,21 +1,20 @@
 # -*- coding: utf-8 -*-
 # -*- coding: utf-8 -*-
-####################################################
-# Copyright © 2009 Luxia SAS. All rights reserved. #
-#                                                  #
-# Contributors:                                    #
-#  - Benoît Pin <pinbe@luxia.fr>                   #
-####################################################
+############################################################
+# Copyright © 2005-2010  Benoît PIN <benoit.pin@ensmp.fr>  #
+# Plinn - http://plinn.org                                 #
+#                                                          #
+# This program is free software; you can redistribute it   #
+# and/or modify it under the terms of the Creative Commons #
+# "Attribution-Noncommercial 2.0 Generic"                  #
+# http://creativecommons.org/licenses/by-nc/2.0/           #
+############################################################
 """ Image threaded batch computation module
 """ Image threaded batch computation module
-
-$Id: manipulation.py 1391 2009-09-16 23:36:05Z pin $
-$URL: http://svn.luxia.fr/svn/labo/projects/zope/Portfolio/trunk/manipulation.py $
 """
 
 import threading
 import logging
 import atexit
 """
 
 import threading
 import logging
 import atexit
-import time
-import Zope2
+from types import StringTypes
 from math import ceil
 import transaction
 from ZODB.POSException import ConflictError
 from math import ceil
 import transaction
 from ZODB.POSException import ConflictError
@@ -28,20 +27,16 @@ class ImageQueueProcessorThread(threading.Thread) :
        """
 
        __stopped = False
        """
 
        __stopped = False
-
-       def __init__(self, portal_path, itemPath):
+       
+       
+       def __init__(self, portal_path, itemsPath) :
                threading.Thread.__init__(self)
                threading.Thread.__init__(self)
-               self.app = app = Zope2.app()
-               self.portal = portal = app.unrestrictedTraverse(portal_path)
-               self.imgTool = portal.portal_image_manipulation
+               self.portal_path = portal_path
                self.queue = []
                self.queue = []
-               if itemPath :
-                       self.queueAdd(itemPath)
-               else :
-                       ctool = portal.portal_catalog
-                       brains = ctool.unrestrictedSearchResults(portal_type='Photo', tiles_available=0)
-                       for b in brains :
-                               self.queueAdd(b.getPath())
+               if isinstance(itemsPath, StringTypes) :
+                       itemsPath = [itemsPath]
+               for i in itemsPath :
+                       self.queueAdd(i)
        
        @property
        def queueSize(self) :
        
        @property
        def queueSize(self) :
@@ -52,21 +47,28 @@ class ImageQueueProcessorThread(threading.Thread) :
 
        def run(self) :
                console.info('process started.')
 
        def run(self) :
                console.info('process started.')
-               atexit.register(self.stop)
+               #atexit.register(self.stop)
+               import Zope2
+               app = Zope2.app()
                while not self.__stopped and self.queueSize :
                while not self.__stopped and self.queueSize :
-                       self._process()
+                       self._process(app)
+               
+               con = app._p_jar
+               con.close()
+               #con.close()
                console.info('process finished.')
                console.info('process finished.')
+               #print con
+               #print con.transaction_manager
+               
 
        def stop(self):
                console.info('process stopped.')
                self.__stopped = True
        
 
        def stop(self):
                console.info('process stopped.')
                self.__stopped = True
        
-       def _process(self) :
-               
+       def _process(self, app) :
                path = self.queue.pop(0)
                path = self.queue.pop(0)
-               
                try :
                try :
-                       p = self.app.unrestrictedTraverse(path)
+                       p = app.unrestrictedTraverse(path)
                except KeyError :
                        console.warn('deleted during processing: %s' % path)
                        return
                except KeyError :
                        console.warn('deleted during processing: %s' % path)
                        return
@@ -126,7 +128,8 @@ class ImageQueueProcessorThread(threading.Thread) :
                except ConflictError :
                        console.warn('Resync after ZODB ConflicError')
                        transaction.abort()
                except ConflictError :
                        console.warn('Resync after ZODB ConflicError')
                        transaction.abort()
-                       self.portal._p_jar.sync()
+                       portal = app.unrestrictedTraverse(self.portal_path)
+                       portal._p_jar.sync()
                        self.queueAdd(path)
                        return
                except :
                        self.queueAdd(path)
                        return
                except :