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 $
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
32 def __init__(self
, portal_path
, itemPath
):
33 threading
.Thread
.__init
__(self
)
34 self
.app
= app
= Zope2
.app()
35 self
.portal
= portal
= app
.unrestrictedTraverse(portal_path
)
36 self
.imgTool
= portal
.portal_image_manipulation
39 self
.queueAdd(itemPath
)
41 ctool
= portal
.portal_catalog
42 brains
= ctool
.unrestrictedSearchResults(portal_type
='Photo', tiles_available
=0)
44 self
.queueAdd(b
.getPath())
48 return len(self
.queue
)
50 def queueAdd(self
, itemPath
) :
51 self
.queue
.append(itemPath
)
54 console
.info('process started.')
55 atexit
.register(self
.stop
)
56 while not self
.__stopped
and self
.queueSize
:
58 console
.info('process finished.')
61 console
.info('process stopped.')
66 path
= self
.queue
.pop(0)
69 p
= self
.app
.unrestrictedTraverse(path
)
71 console
.warn('deleted during processing: %s' % path
)
74 console
.info('%d : %s' % (self
.queueSize
, p
.absolute_url()))
77 if not hasattr(p
, 'thumbnail'):
79 # print 'make thumbnail'
81 for size
in ((500, 500), (600, 600), (800, 800)) :
82 # print 'resize at', size
83 p
._getResizedImage
(size
, True)
86 zMin
= p
.tiles_min_zoom
87 zMax
= p
.tiles_max_zoom
88 zStep
= p
.tiles_step_zoom
89 levels
= range(zMin
, zMax
+ zStep
, zStep
)
90 zooms
= [l
/100. for l
in levels
]
91 todo
= set(zooms
) - set(p
._tiles
.keys())
93 if p
.tileGenerationLock
.locked() :
94 console
.info('skip %s: already tiling.' % p
.absolute_url())
97 p
.tileGenerationLock
.acquire()
105 # print 'tiling at', zoom
107 rppm
= ppm
.resize(ratio
=zoom
)
110 p
._makeTilesAt
(zoom
, rppm
)
115 p
.tileGenerationLock
.release()
118 delattr(p
, '_v__methodResultsCache')
119 except AttributeError:
122 p
.tiles_available
= 1
123 p
.reindexObject(idxs
=['tiles_available'])
126 except ConflictError
:
127 console
.warn('Resync after ZODB ConflicError')
129 self
.portal
._p
_jar
.sync()
133 p
.tiles_available
= -1
136 traceback
.print_exc(None, out
)
137 console
.error(out
.getvalue())