<s> import os <EOL> import sys <EOL> import pkg_resources <EOL> from weakref import ref as weakref <EOL> from inifile import IniFile <EOL> from lektor . context import get_ctx <EOL> def get_plugin ( plugin_id_or_class , env = None ) : <EOL> """<STR_LIT>""" <EOL> if env is None : <EOL> ctx = get_ctx ( ) <EOL> if ctx is None : <EOL> raise RuntimeError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> env = ctx . env <EOL> plugin_id = env . plugin_ids_by_class . get ( plugin_id_or_class , <EOL> plugin_id_or_class ) <EOL> try : <EOL> return env . plugins [ plugin_id ] <EOL> except KeyError : <EOL> raise LookupError ( '<STR_LIT>' % plugin_id ) <EOL> class Plugin ( object ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> description = '<STR_LIT>' <EOL> def __init__ ( self , env , id ) : <EOL> self . _env = weakref ( env ) <EOL> self . id = id <EOL> @ property <EOL> def env ( self ) : <EOL> rv = self . _env ( ) <EOL> if rv is None : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> return rv <EOL> @ property <EOL> def version ( self ) : <EOL> from pkg_resources import get_distribution <EOL> return get_distribution ( '<STR_LIT>' + self . id ) . version <EOL> @ property <EOL> def path ( self ) : <EOL> mod = sys . modules [ self . __class__ . __module__ . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] ] <EOL> path = os . path . abspath ( os . path . dirname ( mod . __file__ ) ) <EOL> if not path . startswith ( self . env . project . get_package_cache_path ( ) ) : <EOL> return path <EOL> @ property <EOL> def import_name ( self ) : <EOL> return self . __class__ . __module__ + '<STR_LIT::>' + self . __class__ . __name__ <EOL> def get_lektor_config ( self ) : <EOL> """<STR_LIT>""" <EOL> ctx = get_ctx ( ) <EOL> if ctx is not None : <EOL> cfg = ctx . pad . db . config <EOL> else : <EOL> cfg = self . env . load_config ( ) <EOL> return cfg <EOL> @ property <EOL> def config_filename ( self ) : <EOL> """<STR_LIT>""" <EOL> return os . path . join ( self . env . root_path , '<STR_LIT>' , self . id + '<STR_LIT>' ) <EOL> def get_config ( self , fresh = False ) : <EOL> """<STR_LIT>""" <EOL> ctx = get_ctx ( ) <EOL> if ctx is not None and not fresh : <EOL> cache = ctx . cache . setdefault ( __name__ + '<STR_LIT>' , { } ) <EOL> cfg = cache . get ( self . id ) <EOL> if cfg is None : <EOL> cfg = IniFile ( self . config_filename ) <EOL> cache [ self . id ] = cfg <EOL> else : <EOL> cfg = IniFile ( self . config_filename ) <EOL> if ctx is not None : <EOL> ctx . record_dependency ( self . config_filename ) <EOL> return cfg <EOL> def emit ( self , event , ** kwargs ) : <EOL> return self . env . pluginsystem . emit ( self . id + '<STR_LIT:->' + event , ** kwargs ) <EOL> def to_json ( self ) : <EOL> return { <EOL> '<STR_LIT:id>' : self . id , <EOL> '<STR_LIT:name>' : self . name , <EOL> '<STR_LIT:version>' : self . version , <EOL> '<STR_LIT:description>' : self . description , <EOL> '<STR_LIT:path>' : self . path , <EOL> '<STR_LIT>' : self . import_name , <EOL> } <EOL> def load_plugins ( ) : <EOL> """<STR_LIT>""" <EOL> rv = { } <EOL> for ep in pkg_resources . iter_entry_points ( '<STR_LIT>' ) : <EOL> match_name = '<STR_LIT>' + ep . name . lower ( ) <EOL> if match_name != ep . dist . project_name . lower ( ) : <EOL> raise RuntimeError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> % ( ep . name , ep . dist . project_name [ <NUM_LIT:7> : ] , <EOL> ep . dist . project_name ) ) <EOL> rv [ ep . name ] = ep . load ( ) <EOL> return rv <EOL> def initialize_plugins ( env ) : <EOL> """<STR_LIT>""" <EOL> plugins = load_plugins ( ) <EOL> for plugin_id , plugin_cls in plugins . iteritems ( ) : <EOL> env . plugin_controller . instanciate_plugin ( plugin_id , plugin_cls ) <EOL> env . plugin_controller . emit ( '<STR_LIT>' ) <EOL> class PluginController ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , env ) : <EOL> self . _env = weakref ( env ) <EOL> @ property <EOL> def env ( self ) : <EOL> rv = self . _env ( ) <EOL> if rv is None : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> return rv <EOL> def instanciate_plugin ( self , plugin_id , plugin_cls ) : <EOL> env = self . env <EOL> if plugin_id in env . plugins : <EOL> raise RuntimeError ( '<STR_LIT>' <EOL> % plugin_id ) <EOL> env . plugins [ plugin_id ] = plugin_cls ( env , plugin_id ) <EOL> env . plugin_ids_by_class [ plugin_cls ] = plugin_id <EOL> def iter_plugins ( self ) : <EOL> return self . env . plugins . itervalues ( ) <EOL> def emit ( self , event , ** kwargs ) : <EOL> rv = { } <EOL> funcname = '<STR_LIT>' + event . replace ( '<STR_LIT:->' , '<STR_LIT:_>' ) <EOL> for plugin in self . iter_plugins ( ) : <EOL> handler = getattr ( plugin , funcname , None ) <EOL> if handler is not None : <EOL> rv [ plugin . id ] = handler ( ** kwargs ) <EOL> return rv </s>
