# -*- coding: utf-8 """Waramu Tool""" import time from waramulib.interfaces import IWaramuClient from AccessControl import ClassSecurityInfo, getSecurityManager from zope.component import getUtility, adapter from zope.app.container.interfaces import IObjectAddedEvent from zope.interface import implements from Globals import Persistent, InitializeClass from OFS import Folder from Products.iva.Cruft import Cruft from interfaces import IWaramuTool from Resource import Resource class WaramuTool(Folder.Folder, Persistent, Cruft): """Waramu tool""" meta_type = 'WaramuTool' implements(IWaramuTool) security = ClassSecurityInfo() security.declareObjectPublic() id = 'waramu_tool' def _getConnection(self, pullconnection=True): """ get waramu """ wu = getUtility(IWaramuClient, name="waramuconnection") if not pullconnection: return wu if wu.isConnected(): return wu self._connect() return wu def isVisible(self): return getattr(self, '_isVisible', False) def _saveOptions(self, req): if req.get('saveWaramu'): self._isVisible = int(req.get('is_visible', 0)) self._waramu_location = req.get('waramu_location', '') self._waramu_user = req.get('waramu_user', '') self._waramu_pass = req.get('waramu_pass', '') return def getWaramuSuffix(self): return self._getConnection()._getSuffix() def getWaramuLocation(self): return getattr(self, '_waramu_location', '') def getWaramuUser(self): return getattr(self, '_waramu_user', '') def getWaramuURL(self): return self._getConnection()._getWebUrl() def isConnected(self): return self._getConnection().isConnected() def _getTemp(self): return getattr(self, 'waramutmp') def _connect(self): w = self._getConnection(pullconnection=False) w._init(self._waramu_location) w.newSession(self._waramu_user, self._waramu_pass) w.Identify() def _disconnect(self): w = self._getConnection(pullconnection=False).closeSession() def _listIdentifiers(self, query): """ list identifiers """ c = self._getConnection() au = c._genAppUser(getSecurityManager().getUser()) res = c.listIdentifiers(au, query) return res def _getResource(self, rid): t = self._getTemp() if hasattr(t, rid): print "from cachee" r = getattr(t, rid) # cache time is about 2.7 hours if r.getCachedTime() + 10000 < time.time(): t._delObject(rid) else: return r c = self._getConnection() au = c._genAppUser(getSecurityManager().getUser()) rxml = c.getResource(au, rid) tr = Resource(c, rxml) t._setObject(rid, tr) return tr def _listAttachments(self, rid): c = self._getConnection() au = c._genAppUser(getSecurityManager().getUser()) return c.listAttachments(au, rid) InitializeClass(WaramuTool) @adapter(IWaramuTool, IObjectAddedEvent) def added(obj, event): """ create a temp storage """ obj.manage_addProduct['TemporaryFolder'].constructTemporaryFolder('waramutmp', 'Local waramu cache')