Back to index

plone3  3.1.7
chain.py
Go to the documentation of this file.
00001 from Products.PageTemplates.PageTemplateFile import PageTemplateFile
00002 from Globals import Persistent
00003 from Globals import InitializeClass
00004 from Acquisition import Implicit
00005 from OFS.SimpleItem import Item
00006 from AccessControl.Role import RoleManager
00007 from AccessControl import ClassSecurityInfo
00008 
00009 from Products.CMFCore.permissions import ManagePortal, ManageProperties
00010 from Products.CMFCore.utils import getToolByName
00011 
00012 from Products.PortalTransforms.utils import _www
00013 from  Products.PortalTransforms.interfaces import ichain
00014 from  Products.PortalTransforms.interfaces import itransform
00015 
00016 from UserList import UserList
00017 
00018 class chain(UserList):
00019     """A chain of transforms used to transform data"""
00020 
00021     __implements__ = (ichain, itransform)
00022 
00023     def __init__(self, name='',*args):
00024         UserList.__init__(self, *args)
00025         self.__name__ = name
00026         if args:
00027             self._update()
00028 
00029     def name(self):
00030         return self.__name__
00031 
00032     def registerTransform(self, transform):
00033         self.append(transform)
00034 
00035     def unregisterTransform(self, name):
00036         for i in range(len(self)):
00037             tr = self[i]
00038             if tr.name() == name:
00039                 self.pop(i)
00040                 break
00041         else:
00042             raise Exception('No transform named %s registered' % name)
00043 
00044     def convert(self, orig, data, **kwargs):
00045         for transform in self:
00046             data = transform.convert(orig, data, **kwargs)
00047             orig = data.getData()
00048         md = data.getMetadata()
00049         md['mimetype'] = self.output
00050         return data
00051 
00052     def __setitem__(self, key, value):
00053         UserList.__setitem__(self, key, value)
00054         self._update()
00055 
00056     def append(self, value):
00057         UserList.append(self, value)
00058         self._update()
00059 
00060     def insert(self, *args):
00061         UserList.insert(*args)
00062         self._update()
00063 
00064     def remove(self, *args):
00065         UserList.remove(*args)
00066         self._update()
00067 
00068     def pop(self, *args):
00069         UserList.pop(*args)
00070         self._update()
00071 
00072     def _update(self):
00073         self.inputs = self[0].inputs
00074         self.output = self[-1].output
00075         for i in range(len(self)):
00076             if hasattr(self[-i-1], 'output_encoding'):
00077                 self.output_encoding = self[-i-1].output_encoding
00078                 break
00079         else:
00080             try:
00081                 del self.output_encoding
00082             except:
00083                 pass
00084 
00085 class TransformsChain(Implicit, Item, RoleManager, Persistent):
00086     """ a transforms chain is suite of transforms to apply in order.
00087     It follows the transform API so that a chain is itself a transform.
00088     """
00089 
00090     module = 'N/A'
00091     meta_type = 'TransformsChain'
00092 
00093     meta_types = all_meta_types = ()
00094 
00095     manage_options = (
00096                       ({'label':'Configure',
00097                        'action':'manage_main'},
00098                        {'label':'Reload',
00099                        'action':'manage_reloadTransform'},) +
00100                       Item.manage_options
00101                       )
00102 
00103     manage_main = PageTemplateFile('editTransformsChain', _www)
00104     manage_reloadTransform = PageTemplateFile('reloadTransform', _www)
00105 
00106     security = ClassSecurityInfo()
00107 
00108     def __init__(self, id, description, ids=()):
00109         self.id = id
00110         self.description = description
00111         self._object_ids = list(ids)
00112         self.inputs = ('application/octet-stream',)
00113         self.output = 'application/octet-stream'
00114         self._chain = None
00115 
00116     def _chain_init(self):
00117         """ build the transforms chain """
00118         tr_tool = getToolByName(self, 'portal_transforms')
00119         self._chain = c = chain()
00120         for id in self._object_ids:
00121             object = getattr(tr_tool, id)
00122             c.registerTransform(object)
00123         self.inputs = c.inputs or ('application/octet-stream',)
00124         self.output = c.output or 'application/octet-stream'
00125 
00126     security.declarePublic('convert')
00127     def convert(self, *args, **kwargs):
00128         """ return apply the transform and return the result """
00129         if self._chain is None:
00130             self._chain_init()
00131         return self._chain.convert(*args, **kwargs)
00132 
00133     security.declarePublic('name')
00134     def name(self):
00135         """return the name of the transform instance"""
00136         return self.id
00137 
00138     security.declarePrivate('manage_beforeDelete')
00139     def manage_beforeDelete(self, item, container):
00140         Item.manage_beforeDelete(self, item, container)
00141         if self is item:
00142             # unregister self from catalog on deletion
00143             tr_tool = getToolByName(self, 'portal_transforms')
00144             tr_tool.unregisterTransform(self.id)
00145 
00146     security.declareProtected(ManagePortal, 'manage_addObject')
00147     def manage_addObject(self, id, REQUEST=None):
00148         """ add a new transform or chain to the chain """
00149         assert id not in self._object_ids
00150         self._object_ids.append(id)
00151         self._chain_init()
00152         if REQUEST is not None:
00153             REQUEST['RESPONSE'].redirect(self.absolute_url()+'/manage_main')
00154 
00155     security.declareProtected(ManagePortal, 'manage_delObjects')
00156     def manage_delObjects(self, ids, REQUEST=None):
00157         """ delete the selected mime types """
00158         for id in ids:
00159             self._object_ids.remove(id)
00160         self._chain_init()
00161         if REQUEST is not None:
00162             REQUEST['RESPONSE'].redirect(self.absolute_url()+'/manage_main')
00163 
00164 
00165     # transforms order handling #
00166 
00167     security.declareProtected(ManagePortal, 'move_object_to_position')
00168     def move_object_to_position(self, id, newpos):
00169         """ overriden from OrderedFolder to store id instead of objects
00170         """
00171         oldpos = self._object_ids.index(id)
00172         if (newpos < 0 or newpos == oldpos or newpos >= len(self._object_ids)):
00173             return 0
00174         self._object_ids.pop(oldpos)
00175         self._object_ids.insert(newpos, id)
00176         self._chain_init()
00177         return 1
00178 
00179     security.declareProtected(ManageProperties, 'move_object_up')
00180     def move_object_up(self, id, REQUEST=None):
00181         """  move object with the given id up in the list """
00182         newpos = self._object_ids.index(id) - 1
00183         self.move_object_to_position(id, newpos)
00184         if REQUEST is not None:
00185             REQUEST['RESPONSE'].redirect(self.absolute_url()+'/manage_main')
00186 
00187     security.declareProtected(ManageProperties, 'move_object_down')
00188     def move_object_down(self, id, REQUEST=None):
00189         """  move object with the given id down in the list """
00190         newpos = self._object_ids.index(id) + 1
00191         self.move_object_to_position(id, newpos)
00192         if REQUEST is not None:
00193             REQUEST['RESPONSE'].redirect(self.absolute_url()+'/manage_main')
00194 
00195 
00196     # Z transform interface #
00197 
00198     security.declareProtected(ManagePortal, 'reload')
00199     def reload(self):
00200         """ reload the module where the transformation class is defined """
00201         for tr in self.objectValues():
00202             tr.reload()
00203         self._chain_init()
00204 
00205     # utilities #
00206 
00207     security.declareProtected(ManagePortal, 'listAddableObjectIds')
00208     def listAddableObjectIds(self):
00209         """ return a list of addable transform """
00210         tr_tool = getToolByName(self, 'portal_transforms')
00211         return [id for id in tr_tool.objectIds() if not (id == self.id or id in self._object_ids)]
00212 
00213     security.declareProtected(ManagePortal, 'objectIds')
00214     def objectIds(self):
00215         """ return a list of addable transform """
00216         return tuple(self._object_ids)
00217 
00218     security.declareProtected(ManagePortal, 'objectValues')
00219     def objectValues(self):
00220         """ return a list of addable transform """
00221         tr_tool = getToolByName(self, 'portal_transforms')
00222         return [getattr(tr_tool, id) for id in self.objectIds()]
00223 
00224 InitializeClass(TransformsChain)