from__future__importunicode_literalsfromselectable.baseimportLookupBase,ModelLookupfromselectable.compatimportforce_textfromselectable.exceptionsimport(LookupAlreadyRegistered,LookupNotRegistered,LookupInvalid)classLookupRegistry(object):def__init__(self):self._registry={}defvalidate(self,lookup):ifnotissubclass(lookup,LookupBase):raiseLookupInvalid('Registered lookups must inherit from the LookupBase class')defregister(self,lookup):self.validate(lookup)name=force_text(lookup.name())ifnameinself._registry:raiseLookupAlreadyRegistered('The name %s is already registered'%name)self._registry[name]=lookupdefunregister(self,lookup):self.validate(lookup)name=force_text(lookup.name())ifnamenotinself._registry:raiseLookupNotRegistered('The name %s is not registered'%name)delself._registry[name]defget(self,key):returnself._registry.get(key,None)registry=LookupRegistry()defautodiscover():importcopyfromdjango.confimportsettingsfromdjango.utils.importlibimportimport_modulefromdjango.utils.module_loadingimportmodule_has_submoduleforappinsettings.INSTALLED_APPS:mod=import_module(app)# Attempt to import the app's lookups module.try:before_import_registry=copy.copy(registry._registry)import_module('%s.lookups'%app)except:registry._registry=before_import_registryifmodule_has_submodule(mod,'lookups'):raise