<s> """<STR_LIT>""" <EOL> import codecs <EOL> try : <EOL> import yaml <EOL> except ImportError : <EOL> yaml = None <EOL> try : <EOL> from ConfigParser import ConfigParser <EOL> except ImportError : <EOL> from configparser import ConfigParser <EOL> import json <EOL> CONFIG_TYPE_JSON = "<STR_LIT>" <EOL> CONFIG_TYPE_YAML = "<STR_LIT>" <EOL> CONFIG_TYPE_INI = "<STR_LIT>" <EOL> DEFAULT_CONFIG_TYPE = CONFIG_TYPE_YAML <EOL> JSON_EXTS = [ "<STR_LIT>" ] <EOL> YAML_EXTS = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> INI_EXTS = [ "<STR_LIT>" ] <EOL> EXT_MAP = { <EOL> CONFIG_TYPE_JSON : JSON_EXTS , <EOL> CONFIG_TYPE_YAML : YAML_EXTS , <EOL> CONFIG_TYPE_INI : INI_EXTS , <EOL> } <EOL> def read_file ( path , type = None , default_type = DEFAULT_CONFIG_TYPE ) : <EOL> if path is None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> config_type = __find_type ( path , type , default_type ) <EOL> return EXT_READERS [ config_type ] ( path ) <EOL> def __find_type ( path , explicit_type , default_type ) : <EOL> if explicit_type : <EOL> return explicit_type <EOL> for config_type , config_exts in EXT_MAP . items ( ) : <EOL> for ext in config_exts : <EOL> if path . endswith ( ext ) : <EOL> return config_type <EOL> return default_type <EOL> def __read_yaml ( path ) : <EOL> if yaml is None : <EOL> raise ImportError ( "<STR_LIT>" ) <EOL> with open ( path , "<STR_LIT:rb>" ) as f : <EOL> return yaml . load ( f ) <EOL> def __read_ini ( path ) : <EOL> config = ConfigParser ( ) <EOL> config . read ( path ) <EOL> return config . _sections <EOL> def __read_json ( path ) : <EOL> reader = codecs . getreader ( "<STR_LIT:utf-8>" ) <EOL> with open ( path , "<STR_LIT:rb>" ) as f : <EOL> return json . load ( reader ( f ) ) <EOL> EXT_READERS = { <EOL> CONFIG_TYPE_JSON : __read_json , <EOL> CONFIG_TYPE_YAML : __read_yaml , <EOL> CONFIG_TYPE_INI : __read_ini , <EOL> } </s>
