<s> """<STR_LIT>""" <EOL> from telex . plugin . IPlugin import IPlugin <EOL> from telex . plugin . PluginManagerDecorator import PluginManagerDecorator <EOL> from telex . plugin . PluginManager import PLUGIN_NAME_FORBIDEN_STRING <EOL> class ConfigurablePluginManager ( PluginManagerDecorator ) : <EOL> """<STR_LIT>""" <EOL> CONFIG_SECTION_NAME = "<STR_LIT>" <EOL> def __init__ ( self , <EOL> configparser_instance = None , <EOL> config_change_trigger = lambda x : True , <EOL> decorated_manager = None , <EOL> categories_filter = None , <EOL> directories_list = None , <EOL> plugin_info_ext = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> if categories_filter is None : <EOL> categories_filter = { "<STR_LIT>" : IPlugin } <EOL> PluginManagerDecorator . __init__ ( self , decorated_manager , <EOL> categories_filter , <EOL> directories_list , <EOL> plugin_info_ext ) <EOL> self . setConfigParser ( configparser_instance , config_change_trigger ) <EOL> def setConfigParser ( self , configparser_instance , config_change_trigger ) : <EOL> """<STR_LIT>""" <EOL> self . config_parser = configparser_instance <EOL> self . config_has_changed = config_change_trigger <EOL> def __getCategoryPluginsListFromConfig ( self , plugin_list_str ) : <EOL> """<STR_LIT>""" <EOL> return plugin_list_str . strip ( "<STR_LIT:U+0020>" ) . split ( "<STR_LIT:%s>" % PLUGIN_NAME_FORBIDEN_STRING ) <EOL> def __getCategoryPluginsConfigFromList ( self , plugin_list ) : <EOL> """<STR_LIT>""" <EOL> return PLUGIN_NAME_FORBIDEN_STRING . join ( plugin_list ) <EOL> def __getCategoryOptionsName ( self , category_name ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT>" % category_name . replace ( "<STR_LIT:U+0020>" , "<STR_LIT:_>" ) <EOL> def __addPluginToConfig ( self , category_name , plugin_name ) : <EOL> """<STR_LIT>""" <EOL> if not self . config_parser . has_section ( self . CONFIG_SECTION_NAME ) : <EOL> self . config_parser . add_section ( self . CONFIG_SECTION_NAME ) <EOL> option_name = self . __getCategoryOptionsName ( category_name ) <EOL> if not self . config_parser . has_option ( self . CONFIG_SECTION_NAME , option_name ) : <EOL> self . config_parser . set ( self . CONFIG_SECTION_NAME , option_name , plugin_name ) <EOL> return self . config_has_changed ( ) <EOL> else : <EOL> past_list_str = self . config_parser . get ( self . CONFIG_SECTION_NAME , option_name ) <EOL> past_list = self . __getCategoryPluginsListFromConfig ( past_list_str ) <EOL> if plugin_name not in past_list : <EOL> past_list . append ( plugin_name ) <EOL> new_list_str = self . __getCategoryPluginsConfigFromList ( past_list ) <EOL> self . config_parser . set ( self . CONFIG_SECTION_NAME , option_name , new_list_str ) <EOL> return self . config_has_changed ( ) <EOL> def __removePluginFromConfig ( self , category_name , plugin_name ) : <EOL> """<STR_LIT>""" <EOL> if not self . config_parser . has_section ( self . CONFIG_SECTION_NAME ) : <EOL> return <EOL> option_name = self . __getCategoryOptionsName ( category_name ) <EOL> if not self . config_parser . has_option ( self . CONFIG_SECTION_NAME , option_name ) : <EOL> return <EOL> else : <EOL> past_list_str = self . config_parser . get ( self . CONFIG_SECTION_NAME , option_name ) <EOL> past_list = self . __getCategoryPluginsListFromConfig ( past_list_str ) <EOL> if plugin_name in past_list : <EOL> past_list . remove ( plugin_name ) <EOL> new_list_str = self . __getCategoryPluginsConfigFromList ( past_list ) <EOL> self . config_parser . set ( self . CONFIG_SECTION_NAME , option_name , new_list_str ) <EOL> self . config_has_changed ( ) <EOL> def registerOptionFromPlugin ( self , <EOL> category_name , plugin_name , <EOL> option_name , option_value ) : <EOL> """<STR_LIT>""" <EOL> section_name = "<STR_LIT>" % ( category_name , plugin_name ) <EOL> if not self . config_parser . has_section ( section_name ) : <EOL> self . config_parser . add_section ( section_name ) <EOL> self . config_parser . set ( section_name , option_name , option_value ) <EOL> self . config_has_changed ( ) <EOL> def hasOptionFromPlugin ( self , <EOL> category_name , plugin_name , option_name ) : <EOL> """<STR_LIT>""" <EOL> section_name = "<STR_LIT>" % ( category_name , plugin_name ) <EOL> return self . config_parser . has_section ( section_name ) and self . config_parser . has_option ( section_name , option_name ) <EOL> def readOptionFromPlugin ( self , <EOL> category_name , plugin_name , option_name ) : <EOL> """<STR_LIT>""" <EOL> section_name = "<STR_LIT>" % ( category_name , plugin_name ) <EOL> return self . config_parser . get ( section_name , option_name ) <EOL> def __decoratePluginObject ( self , category_name , plugin_name , plugin_object ) : <EOL> """<STR_LIT>""" <EOL> plugin_object . setConfigOption = lambda x , y : self . registerOptionFromPlugin ( category_name , <EOL> plugin_name , <EOL> x , y ) <EOL> plugin_object . setConfigOption . __doc__ = self . registerOptionFromPlugin . __doc__ <EOL> plugin_object . getConfigOption = lambda x : self . readOptionFromPlugin ( category_name , <EOL> plugin_name , <EOL> x ) <EOL> plugin_object . getConfigOption . __doc__ = self . readOptionFromPlugin . __doc__ <EOL> plugin_object . hasConfigOption = lambda x : self . hasOptionFromPlugin ( category_name , <EOL> plugin_name , <EOL> x ) <EOL> plugin_object . hasConfigOption . __doc__ = self . hasOptionFromPlugin . __doc__ <EOL> def activatePluginByName ( self , plugin_name , category_name = "<STR_LIT>" , save_state = True ) : <EOL> """<STR_LIT>""" <EOL> pta = self . _component . getPluginByName ( plugin_name , category_name ) <EOL> if pta is None : <EOL> return None <EOL> self . __decoratePluginObject ( category_name , plugin_name , pta . plugin_object ) <EOL> plugin_object = self . _component . activatePluginByName ( plugin_name , category_name ) <EOL> if plugin_object . is_activated : <EOL> if save_state : <EOL> self . __addPluginToConfig ( category_name , plugin_name ) <EOL> return plugin_object <EOL> return None <EOL> def deactivatePluginByName ( self , plugin_name , category_name = "<STR_LIT>" , save_state = True ) : <EOL> """<STR_LIT>""" <EOL> plugin_object = self . _component . deactivatePluginByName ( plugin_name , category_name ) <EOL> if plugin_object is None : <EOL> return None <EOL> if not plugin_object . is_activated : <EOL> if save_state : <EOL> self . __removePluginFromConfig ( category_name , plugin_name ) <EOL> return plugin_object <EOL> return None <EOL> def loadPlugins ( self , callback = None ) : <EOL> """<STR_LIT>""" <EOL> self . _component . loadPlugins ( callback ) <EOL> if self . config_parser . has_section ( self . CONFIG_SECTION_NAME ) : <EOL> for category_name in list ( self . _component . category_mapping . keys ( ) ) : <EOL> option_name = "<STR_LIT>" % category_name <EOL> if self . config_parser . has_option ( self . CONFIG_SECTION_NAME , <EOL> option_name ) : <EOL> plugin_list_str = self . config_parser . get ( self . CONFIG_SECTION_NAME , <EOL> option_name ) <EOL> plugin_list = self . __getCategoryPluginsListFromConfig ( plugin_list_str ) <EOL> for plugin_name in plugin_list : <EOL> self . activatePluginByName ( plugin_name , category_name ) </s>
