<s> from collections import Mapping , Sized , Iterable <EOL> try : <EOL> from django . utils . module_loading import import_module <EOL> except ImportError : <EOL> from django . utils . importlib import import_module <EOL> from django . utils . module_loading import module_has_submodule <EOL> from django . core . urlresolvers import get_callable <EOL> from django . conf import settings <EOL> class AppRegisterException ( Exception ) : <EOL> "<STR_LIT>" <EOL> class InvalidOperation ( AppRegisterException ) : <EOL> """<STR_LIT>""" <EOL> class AlreadyRegistered ( AppRegisterException ) : <EOL> """<STR_LIT>""" <EOL> class BaseRegistry ( Sized , Iterable ) : <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> self . setup ( ) <EOL> if not callable ( self . base ) : <EOL> self . base_str = self . base <EOL> self . base = None <EOL> self . get_bases ( ) <EOL> def __iter__ ( self ) : <EOL> return iter ( self . _registry ) <EOL> def __len__ ( self ) : <EOL> return len ( self . _registry ) <EOL> def get_bases ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . base is not None : <EOL> return self . base <EOL> self . base = get_callable ( self . base_str ) <EOL> return self . base <EOL> def all ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _registry <EOL> def autodiscover ( self , module = None ) : <EOL> """<STR_LIT>""" <EOL> if not module : <EOL> module = self . discovermodule <EOL> for app in settings . INSTALLED_APPS : <EOL> try : <EOL> import_module ( "<STR_LIT>" % module , app ) <EOL> except ImportError : <EOL> if module_has_submodule ( import_module ( app ) , module ) : <EOL> raise <EOL> continue <EOL> def is_valid ( self , class_ ) : <EOL> """<STR_LIT>""" <EOL> return issubclass ( class_ , self . get_bases ( ) ) <EOL> def is_registered ( self , class_ ) : <EOL> """<STR_LIT>""" <EOL> return class_ in self . _registry <EOL> def setup ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _registry = set ( ) <EOL> def clear ( self ) : <EOL> """<STR_LIT>""" <EOL> self . setup ( ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . __class__ . __name__ , len ( self ) ) <EOL> class Registry ( BaseRegistry ) : <EOL> """<STR_LIT>""" <EOL> def add_class ( self , class_ ) : <EOL> """<STR_LIT>""" <EOL> self . _registry . add ( class_ ) <EOL> def remove_class ( self , class_ ) : <EOL> """<STR_LIT>""" <EOL> self . _registry . remove ( class_ ) <EOL> def register ( self , class_ ) : <EOL> """<STR_LIT>""" <EOL> if not self . is_valid ( class_ ) : <EOL> msg = "<STR_LIT>" % ( class_ . __name__ , <EOL> self . base . __name__ ) <EOL> raise InvalidOperation ( msg ) <EOL> if self . is_registered ( class_ ) : <EOL> msg = "<STR_LIT>" % class_ . __name__ <EOL> raise AlreadyRegistered ( msg ) <EOL> self . add_class ( class_ ) <EOL> return class_ <EOL> def unregister ( self , class_ ) : <EOL> """<STR_LIT>""" <EOL> self . remove_class ( class_ ) <EOL> class NamedRegistry ( BaseRegistry , Mapping ) : <EOL> """<STR_LIT>""" <EOL> def setup ( self ) : <EOL> self . _registry = dict ( ) <EOL> def register ( self , name , class_ = None ) : <EOL> """<STR_LIT>""" <EOL> if self . is_registered ( name ) : <EOL> msg = "<STR_LIT>" % class_ . __name__ <EOL> raise AlreadyRegistered ( msg ) <EOL> if not class_ : <EOL> def inner ( class_ ) : <EOL> return self . register ( name , class_ ) <EOL> return inner <EOL> if not self . is_valid ( class_ ) : <EOL> msg = "<STR_LIT>" % ( class_ . __name__ , <EOL> self . base . __name__ ) <EOL> raise InvalidOperation ( msg ) <EOL> self . _registry [ name ] = class_ <EOL> return class_ <EOL> def unregister ( self , name ) : <EOL> """<STR_LIT>""" <EOL> del self . _registry [ name ] <EOL> def __getitem__ ( self , key ) : <EOL> return self . _registry [ key ] <EOL> class SortedRegistry ( Registry ) : <EOL> """<STR_LIT>""" <EOL> def setup ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _registry = list ( ) <EOL> def add_class ( self , class_ ) : <EOL> """<STR_LIT>""" <EOL> self . _registry . append ( class_ ) </s>
