1 # -*- coding: utf-8 -*-
2 ############################################################
3 # Copyright © 2005-2010 Benoît PIN <benoit.pin@ensmp.fr> #
4 # Plinn - http://plinn.org #
6 # This program is free software; you can redistribute it #
7 # and/or modify it under the terms of the Creative Commons #
8 # "Attribution-Noncommercial 2.0 Generic" #
9 # http://creativecommons.org/licenses/by-nc/2.0/ #
10 ############################################################
11 """ Image threaded batch computation module
17 from types
import StringTypes
20 from ZODB
.POSException
import ConflictError
21 from zope
.site
.hooks
import setSite
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
]
44 return len(self
.queue
)
46 def queueAdd(self
, itemPath
) :
47 self
.queue
.append(itemPath
)
50 console
.info('process started.')
51 #atexit.register(self.stop)
54 portal
= app
.unrestrictedTraverse(self
.portal_path
)
56 while not self
.__stopped
and self
.queueSize
:
62 console
.info('process finished.')
64 #print con.transaction_manager
68 console
.info('process stopped.')
71 def _process(self
, app
) :
72 path
= self
.queue
.pop(0)
74 p
= app
.unrestrictedTraverse(path
)
76 console
.warn('deleted during processing: %s' % path
)
79 console
.info('%d : %s' % (self
.queueSize
, p
.absolute_url()))
82 if not hasattr(p
, 'thumbnail'):
84 # print 'make thumbnail'
86 for size
in ((500, 500), (600, 600), (800, 800)) :
87 # print 'resize at', size
88 p
._getResizedImage
(size
, True)
91 zMin
= p
.tiles_min_zoom
92 zMax
= p
.tiles_max_zoom
93 zStep
= p
.tiles_step_zoom
94 levels
= range(zMin
, zMax
+ zStep
, zStep
)
95 zooms
= [l
/100. for l
in levels
]
96 todo
= set(zooms
) - set(p
._tiles
.keys())
98 if p
.tileGenerationLock
.locked() :
99 console
.info('skip %s: already tiling.' % p
.absolute_url())
102 p
.tileGenerationLock
.acquire()
110 # print 'tiling at', zoom
112 rppm
= ppm
.resize(ratio
=zoom
)
115 p
._makeTilesAt
(zoom
, rppm
)
120 p
.tileGenerationLock
.release()
123 delattr(p
, '_v__methodResultsCache')
124 except AttributeError:
127 p
.tiles_available
= 1
129 portal
= app
.unrestrictedTraverse(self
.portal_path
)
130 ctool
= portal
.portal_catalog
131 uid
= '/'.join(p
.getPhysicalPath())
132 ctool
.reindexObject(p
, idxs
=['tiles_available'], uid
=uid
)
135 except ConflictError
:
136 console
.warn('Resync after ZODB ConflicError')
138 portal
= app
.unrestrictedTraverse(self
.portal_path
)
143 p
.tiles_available
= -1
146 traceback
.print_exc(None, out
)
147 console
.error(out
.getvalue())