8c057d6312ee393c03865f0cb6526cdf26233291
1 # -*- coding: utf-8 -*-
2 ####################################################
3 # Copyright © 2009 Luxia SAS. All rights reserved. #
6 # - Benoît Pin <pinbe@luxia.fr> #
7 ####################################################
8 """ Image threaded batch computation module
10 $Id: manipulation.py 1391 2009-09-16 23:36:05Z pin $
11 $URL: http://svn.luxia.fr/svn/labo/projects/zope/Portfolio/trunk/manipulation.py $
17 from types
import StringTypes
21 from ZODB
.POSException
import ConflictError
22 from cStringIO
import StringIO
24 console
= logging
.getLogger('[manipulation thread]')
26 class ImageQueueProcessorThread(threading
.Thread
) :
27 """This thread is started at zope startup
33 def __init__(self
, portal_path
, itemsPath
) :
34 threading
.Thread
.__init
__(self
)
35 self
.portal_path
= portal_path
37 if isinstance(itemsPath
, StringTypes
) :
38 itemsPath
= [itemsPath
]
42 # def __init__(self, portal_path, itemPath):
43 # threading.Thread.__init__(self)
44 # self.app = app = Zope2.app()
45 # self.portal = portal = app.unrestrictedTraverse(portal_path)
46 # self.imgTool = portal.portal_image_manipulation
49 # self.queueAdd(itemPath)
51 # ctool = portal.portal_catalog
52 # brains = ctool.unrestrictedSearchResults(portal_type='Photo', tiles_available=0)
54 # self.queueAdd(b.getPath())
57 print "ayé, c'est la fin !"
61 return len(self
.queue
)
63 def queueAdd(self
, itemPath
) :
64 self
.queue
.append(itemPath
)
67 console
.info('process started.')
68 #atexit.register(self.stop)
71 while not self
.__stopped
and self
.queueSize
:
77 console
.info('process finished.')
79 #print con.transaction_manager
83 console
.info('process stopped.')
86 def _process(self
, app
) :
88 path
= self
.queue
.pop(0)
94 p
= app
.unrestrictedTraverse(path
)
96 console
.warn('deleted during processing: %s' % path
)
99 console
.info('%d : %s' % (self
.queueSize
, p
.absolute_url()))
102 if not hasattr(p
, 'thumbnail'):
104 # print 'make thumbnail'
106 for size
in ((500, 500), (600, 600), (800, 800)) :
107 # print 'resize at', size
108 p
._getResizedImage
(size
, True)
111 zMin
= p
.tiles_min_zoom
112 zMax
= p
.tiles_max_zoom
113 zStep
= p
.tiles_step_zoom
114 levels
= range(zMin
, zMax
+ zStep
, zStep
)
115 zooms
= [l
/100. for l
in levels
]
116 todo
= set(zooms
) - set(p
._tiles
.keys())
118 if p
.tileGenerationLock
.locked() :
119 console
.info('skip %s: already tiling.' % p
.absolute_url())
122 p
.tileGenerationLock
.acquire()
130 # print 'tiling at', zoom
132 rppm
= ppm
.resize(ratio
=zoom
)
135 p
._makeTilesAt
(zoom
, rppm
)
140 p
.tileGenerationLock
.release()
143 delattr(p
, '_v__methodResultsCache')
144 except AttributeError:
147 p
.tiles_available
= 1
148 p
.reindexObject(idxs
=['tiles_available'])
151 except ConflictError
:
152 console
.warn('Resync after ZODB ConflicError')
154 portal
= app
.unrestrictedTraverse(portal_path
)
159 p
.tiles_available
= -1
162 traceback
.print_exc(None, out
)
163 console
.error(out
.getvalue())