[repo, entity] move entity cache initialization to a function, and call it before source.add_entity so it may be used in error handler or such
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr## This file is part of CubicWeb.## CubicWeb is free software: you can redistribute it and/or modify it under the# terms of the GNU Lesser General Public License as published by the Free# Software Foundation, either version 2.1 of the License, or (at your option)# any later version.## CubicWeb is distributed in the hope that it will be useful, but WITHOUT# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more# details.## You should have received a copy of the GNU Lesser General Public License along# with CubicWeb. If not, see <http://www.gnu.org/licenses/>."""Pyro RQL server"""__docformat__="restructuredtext en"importosimportsysimportselectimportwarningsfromtimeimportlocaltime,mktimefromcubicweb.cwconfigimportCubicWebConfigurationfromcubicweb.server.repositoryimportRepositoryclassFinished(Exception):"""raise to remove an event from the event loop"""classTimeEvent:"""base event"""# timefunc = staticmethod(localtime)timefunc=localtimedef__init__(self,absolute=None,period=None):# local time tupleifabsoluteisNone:absolute=self.timefunc()self.absolute=absolute# optional period in secondsself.period=perioddefis_ready(self):"""return true if the event is ready to be fired"""now=self.timefunc()ifself.absolute<now:returnTruereturnFalsedeffire(self,server):"""fire the event must be overridden by concrete events """raiseNotImplementedError()defupdate(self):"""update the absolute date for the event or raise a finished exception """ifself.periodisNone:raiseFinishedself.absolute=localtime(mktime(self.absolute)+self.period)classQuitEvent(TimeEvent):"""stop the server"""deffire(self,server):server.repo.shutdown()server.quiting=TrueclassRepositoryServer(object):def__init__(self,config):"""make the repository available as a PyRO object"""self.config=configself.repo=Repository(config)self.ns=Noneself.quiting=None# event queueself.events=[]defadd_event(self,event):"""add an event to the loop"""self.info('adding event %s',event)self.events.append(event)deftrigger_events(self):"""trigger ready events"""foreventinself.events[:]:ifevent.is_ready():self.info('starting event %s',event)event.fire(self)try:event.update()exceptFinished:self.events.remove(event)defrun(self,req_timeout=5.0):"""enter the service loop"""# start repository looping tasksself.repo.start_looping_tasks()whileself.quitingisNone:try:self.daemon.handleRequests(req_timeout)exceptselect.error:continueself.trigger_events()defquit(self):"""stop the server"""self.add_event(QuitEvent())defconnect(self,host='',port=0):"""the connect method on the repository only register to pyro if necessary """self.daemon=self.repo.pyro_register(host)# server utilitities ######################################################definstall_sig_handlers(self):"""install signal handlers"""importsignalself.info('installing signal handlers')signal.signal(signal.SIGINT,lambdax,y,s=self:s.quit())signal.signal(signal.SIGTERM,lambdax,y,s=self:s.quit())fromloggingimportgetLoggerfromcubicwebimportset_log_methodsLOGGER=getLogger('cubicweb.reposerver')set_log_methods(RepositoryServer,LOGGER)