<s> from ConfigParser import ConfigParser , Error as ConfigKeyError , NoSectionError <EOL> from contextlib import closing <EOL> import os <EOL> config_path = '<STR_LIT>' <EOL> config_names = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> _config_object = { } <EOL> def get_config ( config_type = '<STR_LIT>' ) : <EOL> global _config_object <EOL> if not _config_object . get ( config_type , None ) : <EOL> _config_object [ config_type ] = TUIConfig ( config_type ) <EOL> return _config_object [ config_type ] <EOL> def gen_config_file_names ( ) : <EOL> config_path = '<STR_LIT>' <EOL> config_types = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> config_names = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> return dict ( zip ( config_types , [ os . path . join ( config_path , <EOL> i ) for i in config_names ] ) ) <EOL> class TUIConfig ( ConfigParser ) : <EOL> no_default = object ( ) <EOL> def __init__ ( self , config_type , path = config_path , names = config_names ) : <EOL> ConfigParser . __init__ ( self ) <EOL> self . config_path = path <EOL> self . config_names = names <EOL> self . config_type = config_type <EOL> self . config_file = gen_config_file_names ( ) [ self . config_type ] <EOL> with closing ( open ( self . config_file ) ) as f : <EOL> self . readfp ( f ) <EOL> def getboolean ( self , section , option , default = no_default ) : <EOL> try : <EOL> return ConfigParser . getboolean ( self , section , option ) <EOL> except ConfigKeyError : <EOL> if default is not self . no_default : <EOL> return default <EOL> print '<STR_LIT>' , section , option <EOL> raise <EOL> def getint ( self , section , option , default = no_default ) : <EOL> try : <EOL> return ConfigParser . getint ( self , section , option ) <EOL> except ConfigKeyError : <EOL> if default is not self . no_default : <EOL> return default <EOL> print '<STR_LIT>' , section , option <EOL> raise <EOL> def getfloat ( self , section , option , default = no_default ) : <EOL> try : <EOL> return ConfigParser . getfloat ( self , section , option ) <EOL> except ConfigKeyError : <EOL> if default is not self . no_default : <EOL> return default <EOL> print '<STR_LIT>' , section , option <EOL> raise <EOL> def getstring ( self , section , option , default = no_default ) : <EOL> try : <EOL> return ConfigParser . get ( self , section , option ) <EOL> except ConfigKeyError : <EOL> if default is not self . no_default : <EOL> return default <EOL> print '<STR_LIT>' , section , option <EOL> raise <EOL> def getlist ( self , section , default = no_default ) : <EOL> try : <EOL> return ConfigParser . items ( self , section ) <EOL> except ConfigKeyError : <EOL> if default is not self . no_default : <EOL> return default <EOL> print '<STR_LIT>' , section <EOL> raise <EOL> def setvalue ( self , section , option , value ) : <EOL> try : <EOL> self . set ( section , option , value ) <EOL> with closing ( open ( self . config_file , '<STR_LIT>' ) ) as f : <EOL> self . write ( f ) <EOL> except NoSectionError : <EOL> self . add_section ( section ) <EOL> self . setvalue ( section , option , value ) <EOL> def _resolve_conf_name ( conf_type ) : <EOL> """<STR_LIT>""" <EOL> fnm = config_names [ conf_type ] <EOL> if os . path . isfile ( fnm ) : <EOL> return fnm <EOL> elif os . path . isfile ( config_path + fnm ) : <EOL> return config_path + fnm <EOL> else : <EOL> raise RuntimeError ( "<STR_LIT>" % conf_type ) <EOL> def c ( group , field , conf_type = '<STR_LIT>' ) : <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> """<STR_LIT>""" <EOL> conf = ConfigParser . RawConfigParser ( ) <EOL> conf . read ( _resolve_conf_name ( conf_type ) ) <EOL> return conf . get ( group , field ) <EOL> def cs ( group , field , value , conf_type = '<STR_LIT>' ) : <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> """<STR_LIT>""" <EOL> conf = ConfigParser . RawConfigParser ( ) <EOL> fnm = _resolve_conf_name ( conf_type ) <EOL> conf . read ( fnm ) <EOL> conf . set ( group , field , value ) <EOL> with open ( fnm , '<STR_LIT:wb>' ) as configfile : <EOL> conf . write ( configfile ) <EOL> def clist ( group , conf_type = '<STR_LIT>' ) : <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> """<STR_LIT>""" <EOL> conf = ConfigParser . RawConfigParser ( ) <EOL> conf . read ( _resolve_conf_name ( conf_type ) ) <EOL> return conf . items ( group ) <EOL> def has_option ( group , field , conf_type = '<STR_LIT>' ) : <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> """<STR_LIT>""" <EOL> conf = ConfigParser . RawConfigParser ( ) <EOL> conf . read ( _resolve_conf_name ( conf_type ) ) <EOL> return conf . has_option ( group , field ) </s>
