<s> import sys <EOL> import imp <EOL> import os <EOL> import ConfigParser <EOL> import StringIO <EOL> import logging <EOL> from synapse . config import config <EOL> registry = { } <EOL> log = logging . getLogger ( '<STR_LIT>' ) <EOL> def register ( mapping , cls ) : <EOL> name = cls . __resource__ <EOL> if not name in registry : <EOL> path = os . path . dirname ( <EOL> os . path . abspath ( sys . modules [ cls . __module__ ] . __file__ ) ) <EOL> if not name in registry : <EOL> mod = get_module ( mapping , path ) <EOL> registry [ name ] = cls ( mod ) <EOL> def get_module ( os_mapping , dirpath ) : <EOL> mapping_config = ConfigParser . RawConfigParser ( ) <EOL> mapping_config . readfp ( StringIO . StringIO ( os_mapping ) ) <EOL> opts = config . controller <EOL> dist = opts [ '<STR_LIT>' ] <EOL> ver = opts [ '<STR_LIT>' ] <EOL> combinations = ( ( dist , ver ) , ( dist , '<STR_LIT:default>' ) , ( '<STR_LIT:default>' , '<STR_LIT:default>' ) ) <EOL> mod_name = None <EOL> for comb in combinations : <EOL> mod_name = get_module_name ( mapping_config , comb [ <NUM_LIT:0> ] , comb [ <NUM_LIT:1> ] ) <EOL> if mod_name : <EOL> break <EOL> if mod_name != '<STR_LIT:default>' and mod_name is not None : <EOL> fp , path , desc = imp . find_module ( mod_name , [ dirpath ] ) <EOL> return imp . load_module ( mod_name , fp , path , desc ) <EOL> elif mod_name == '<STR_LIT:default>' : <EOL> return None <EOL> def get_module_name ( conf , section = '<STR_LIT:default>' , option = '<STR_LIT:default>' ) : <EOL> plugin_name = None <EOL> try : <EOL> plugin_name = conf . get ( section , option ) <EOL> if plugin_name == '<STR_LIT:None>' : <EOL> plugin_name = '<STR_LIT:default>' <EOL> except ( ConfigParser . NoSectionError , ConfigParser . NoOptionError ) : <EOL> pass <EOL> return plugin_name </s>
