1 # -*- coding: utf-8 -*-
2 ############################################################
3 # Copyright © 2005-2008 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 manipulation tool
14 from AccessControl
import ClassSecurityInfo
15 from AccessControl
.requestmethod
import postonly
16 from Products
.PageTemplates
.PageTemplateFile
import PageTemplateFile
17 from Products
.CMFCore
.permissions
import ManagePortal
18 from AccessControl
import getSecurityManager
19 from Globals
import InitializeClass
20 from OFS
.OrderedFolder
import OrderedFolder
21 from Products
.CMFCore
.utils
import UniqueObject
22 from Products
.CMFCore
.utils
import getToolByName
23 from utils
import Message
as _
24 from manipulation
import ImageQueueProcessorThread
25 from Products
.MailHost
.decorator
import synchronized
27 from logging
import getLogger
28 from threading
import Lock
30 console
= getLogger('[portal_image_manipulation]')
31 queue_threads
= weakref
.WeakValueDictionary()
34 class ImageManipulationTool( UniqueObject
, OrderedFolder
) :
35 """ Contains image manipulation plugins
38 id = 'portal_image_manipulation'
39 meta_type
= 'Image Manipulation Tool'
40 manage_options
= ({'label' : _('Processor'), 'action' : 'manage_processor'}, ) + \
41 OrderedFolder
.manage_options
43 security
= ClassSecurityInfo()
46 security
.declareProtected( ManagePortal
, 'manage_processor' )
47 manage_processor
= PageTemplateFile('www/manageProcessor', globals(),
48 __name__
='manage_processor')
50 security
.declareProtected(ManagePortal
, 'manage_startProcess')
52 def manage_startProcess(self
, REQUEST
) :
53 ' start processor thread '
54 self
._startQueueProcessorThread
()
55 return self
.manage_processor(self
, REQUEST
,
56 manage_tabs_message
=_('Process started.'))
58 security
.declareProtected(ManagePortal
, 'manage_stopProcess')
60 def manage_stopProcess(self
, REQUEST
) :
61 ' stop processor thread '
62 self
._stopQueueProcessorThread
()
63 return self
.manage_processor(self
, REQUEST
,
64 manage_tabs_message
=_('Process stopped.'))
66 security
.declareProtected(ManagePortal
, 'isRunning')
68 """ returns boolean telling if the processor thread is running.
70 path
= self
.absolute_url(1)
71 if queue_threads
.has_key(path
):
72 thread
= queue_threads
[path
]
73 return thread
.isAlive()
77 security
.declareProtected(ManagePortal
, 'getQueueSize')
78 def getQueueSize(self
):
82 path
= self
.absolute_url(1)
83 if queue_threads
.has_key(path
):
84 thread
= queue_threads
[path
]
85 return thread
.queueSize
89 def _queueAdd(self
, itemPath
) :
90 if not self
.isRunning() :
91 self
._startQueueProcessorThread
(itemPath
)
93 path
= self
.absolute_url(1)
94 thread
= queue_threads
[path
]
95 thread
.queueAdd(itemPath
)
98 def _startQueueProcessorThread(self
, itemPath
=None):
99 """ Start thread for processing image manipulation """
101 if not self
.isRunning() :
102 utool
= getToolByName(self
, 'portal_url')
103 ctool
= getToolByName(self
, 'portal_catalog')
104 portal
= utool
.getPortalObject()
105 brains
= ctool
.unrestrictedSearchResults(portal_type
='Photo', tiles_available
=0)
106 paths
= [b
.getPath() for b
in brains
]
107 if itemPath
is not None :
108 paths
.insert(0, itemPath
)
109 thread
= ImageQueueProcessorThread(portal
.getPhysicalPath(), paths
)
111 path
= self
.absolute_url(1)
112 queue_threads
[path
] = thread
113 console
.info('Thread for %s started' % path
)
116 def _stopQueueProcessorThread(self
):
117 """ Stop thread for processing image manipulation """
119 path
= self
.absolute_url(1)
120 if queue_threads
.has_key(path
):
121 thread
= queue_threads
[path
]
123 while thread
.isAlive():
124 # wait until thread is really dead
126 del queue_threads
[path
]
127 console
.info('Thread for %s stopped' % path
)
129 InitializeClass( ImageManipulationTool
)