<s> from abc import ABCMeta , abstractmethod <EOL> from flask . _compat import with_metaclass <EOL> from flask_factory . utils import import_string , get_nearest_package_name <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> class Initializer ( with_metaclass ( ABCMeta ) ) : <EOL> @ abstractmethod <EOL> def init_app ( self , app ) : <EOL> raise NotImplementedError <EOL> class Action ( Initializer ) : <EOL> def __init__ ( self , f ) : <EOL> self . _func = f <EOL> def init_app ( self , app ) : <EOL> self . _func ( app ) <EOL> class DeferredAction ( Initializer ) : <EOL> def __init__ ( self , path ) : <EOL> self . _path = path <EOL> def init_app ( self , app ) : <EOL> package = get_nearest_package_name ( app . import_name ) <EOL> func = import_string ( self . _path , package = package ) <EOL> func ( app ) <EOL> class Extension ( Initializer ) : <EOL> def __init__ ( self , ext_cls , name = None , args = ( ) , kwargs = None , <EOL> init_func = None ) : <EOL> self . ext_cls = ext_cls <EOL> if name is None : <EOL> name = discover_ext_name ( ext_cls . __module__ ) <EOL> self . ext_name = name <EOL> self . init_args = args <EOL> self . init_kwargs = kwargs or { } <EOL> self . init_func = init_func <EOL> def init_app ( self , app ) : <EOL> inst = self . ext_cls ( app , * self . init_args , ** self . init_kwargs ) <EOL> if callable ( self . init_func ) : <EOL> self . init_func ( app , inst ) <EOL> ext_instances = app . extensions [ '<STR_LIT>' ] . setdefault ( '<STR_LIT>' , { } ) <EOL> ext_instances [ self . ext_name ] = inst <EOL> def discover_ext_name ( name ) : <EOL> """<STR_LIT>""" <EOL> if name . startswith ( '<STR_LIT>' ) : <EOL> top_level_pkg_name = name . split ( '<STR_LIT:.>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> prefix , ext_name = top_level_pkg_name . split ( '<STR_LIT:_>' , <NUM_LIT:1> ) <EOL> return ext_name <EOL> elif name . startswith ( '<STR_LIT>' ) : <EOL> mod_path = name . split ( '<STR_LIT:.>' , <NUM_LIT:3> ) <EOL> ext_name = mod_path [ <NUM_LIT:2> ] <EOL> return ext_name <EOL> elif name . startswith ( '<STR_LIT>' ) : <EOL> mod_path = name . split ( '<STR_LIT:.>' , <NUM_LIT:2> ) <EOL> ext_name = mod_path [ <NUM_LIT:1> ] <EOL> return ext_name <EOL> else : <EOL> raise ValueError ( name ) </s>
