"""This file contains some basic registerers required by application objectsregistry to handle registration at startup time.A registerer is responsible to tell if an object should be registered accordingto the application's schema or to already registered object:organization: Logilab:copyright: 2006-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved.:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr"""__docformat__="restructuredtext en"fromcubicweb.vregistryimportregisterer,yes_registererfromcubicweb.cwvregimportuse_interfacesclasspriority_registerer(registerer):"""systematically kick previous registered class and register the wrapped class (based on the fact that directory containing vobjects are loaded from the most generic to the most specific). This is usually for templates or startup views where we want to keep only the latest in the load path """defdo_it_yourself(self,registered):ifregistered:iflen(registered)>1:self.warning('priority_registerer found more than one registered objects ''(registerer monkey patch ?)')forregobjinregistered[:]:self.kick(registered,regobj)returnself.vobjectdefremove_equivalents(self,registered):for_objinregistered[:]:ifself.equivalent(_obj):self.kick(registered,_obj)breakdefremove_all_equivalents(self,registered):for_objinregistered[:]:if_objisself.vobject:continueifself.equivalent(_obj):self.kick(registered,_obj)defequivalent(self,other):raiseNotImplementedError(self,self.vobject)classaccepts_registerer(priority_registerer):"""register according to the .accepts attribute of the wrapped class, which should be a tuple refering some entity's types * if no type is defined the application'schema, skip the wrapped class * if the class defines a requires attribute, each entity type defined in the requires list must be in the schema * if an object previously registered has equivalent .accepts attribute, kick it out * register """defdo_it_yourself(self,registered):# if object is accepting interface, we have register it now and# remove it later if no object is implementing accepted interfacesifuse_interfaces(self.vobject):returnself.vobjectself.remove_equivalents(registered)returnself.vobjectdefequivalent(self,other):ifuse_interfaces(self.vobject)!=use_interfaces(other):returnFalsetry:newaccepts=list(other.accepts)foretypeinself.vobject.accepts:try:newaccepts.remove(etype)exceptValueError:continueifnewaccepts:other.accepts=tuple(newaccepts)returnFalsereturnTrueexceptAttributeError:returnFalse__all__=[cls.__name__forclsinglobals().values()ifisinstance(cls,type)andissubclass(cls,registerer)andnotclsisregisterer]