<s> import os <EOL> import pprint <EOL> import numpy <EOL> from . import Callback <EOL> from . import Bunch <EOL> unset_value = ( "<STR_LIT>" ) <EOL> class SettingError ( Exception ) : <EOL> pass <EOL> class Setting ( Callback . Callbacks ) : <EOL> def __init__ ( self , value = unset_value , name = None , logger = None , <EOL> check_fn = None ) : <EOL> Callback . Callbacks . __init__ ( self ) <EOL> self . value = value <EOL> self . _unset = ( value == unset_value ) <EOL> self . name = name <EOL> self . logger = logger <EOL> if check_fn is None : <EOL> check_fn = self . _check_none <EOL> self . check_fn = check_fn <EOL> for name in ( '<STR_LIT>' , ) : <EOL> self . enable_callback ( name ) <EOL> def _check_none ( self , value ) : <EOL> return value <EOL> def set ( self , value , callback = True ) : <EOL> self . value = self . check_fn ( value ) <EOL> if callback : <EOL> self . make_callback ( '<STR_LIT>' , value ) <EOL> def get ( self , * args ) : <EOL> if self . _unset : <EOL> if len ( args ) == <NUM_LIT:0> : <EOL> raise KeyError ( "<STR_LIT>" % ( <EOL> self . name ) ) <EOL> else : <EOL> assert len ( args ) == <NUM_LIT:1> , SettingError ( "<STR_LIT>" % ( <EOL> str ( args ) ) ) <EOL> return args [ <NUM_LIT:0> ] <EOL> return self . value <EOL> def __repr__ ( self ) : <EOL> return repr ( self . value ) <EOL> def __str__ ( self ) : <EOL> return str ( self . value ) <EOL> class SettingGroup ( object ) : <EOL> def __init__ ( self , name = None , logger = None , preffile = None ) : <EOL> self . name = name <EOL> self . logger = logger <EOL> self . preffile = preffile <EOL> self . group = Bunch . Bunch ( ) <EOL> def addSettings ( self , ** kwdargs ) : <EOL> for key , value in kwdargs . items ( ) : <EOL> self . group [ key ] = Setting ( value = value , name = key , <EOL> logger = self . logger ) <EOL> def getSetting ( self , key ) : <EOL> return self . group [ key ] <EOL> def shareSettings ( self , other , keylist = None ) : <EOL> if keylist is None : <EOL> keylist = self . group . keys ( ) <EOL> for key in keylist : <EOL> other . group [ key ] = self . group [ key ] <EOL> def copySettings ( self , other , keylist = None ) : <EOL> if keylist is None : <EOL> keylist = self . group . keys ( ) <EOL> d = { } <EOL> for key in keylist : <EOL> d [ key ] = self . get ( key ) <EOL> other . setDict ( d ) <EOL> def setdefault ( self , key , value ) : <EOL> if key in self . group : <EOL> return self . group [ key ] . get ( value ) <EOL> else : <EOL> d = { key : value } <EOL> self . addSettings ( ** d ) <EOL> return self . group [ key ] . get ( value ) <EOL> def addDefaults ( self , ** kwdargs ) : <EOL> for key , value in kwdargs . items ( ) : <EOL> self . setdefault ( key , value ) <EOL> def setDefaults ( self , ** kwdargs ) : <EOL> return self . addDefaults ( ** kwdargs ) <EOL> def get ( self , * args ) : <EOL> key = args [ <NUM_LIT:0> ] <EOL> if len ( args ) == <NUM_LIT:1> : <EOL> return self . group [ key ] . get ( ) <EOL> if len ( args ) == <NUM_LIT:2> : <EOL> return self . setdefault ( key , args [ <NUM_LIT:1> ] ) <EOL> def getDict ( self ) : <EOL> return dict ( [ [ name , self . group [ name ] . value ] for name in self . group . keys ( ) ] ) <EOL> def setDict ( self , d , callback = True ) : <EOL> for key , value in d . items ( ) : <EOL> if key not in self . group : <EOL> self . setdefault ( key , value ) <EOL> else : <EOL> self . group [ key ] . set ( value , callback = callback ) <EOL> def set ( self , callback = True , ** kwdargs ) : <EOL> self . setDict ( kwdargs , callback = callback ) <EOL> def __getitem__ ( self , key ) : <EOL> return self . group [ key ] . value <EOL> def __setitem__ ( self , key , value ) : <EOL> self . group [ key ] . set ( value ) <EOL> def has_key ( self , key ) : <EOL> return key in self . group <EOL> def load ( self , onError = '<STR_LIT>' ) : <EOL> try : <EOL> d = { } <EOL> with open ( self . preffile , '<STR_LIT:r>' ) as in_f : <EOL> buf = in_f . read ( ) <EOL> for line in buf . split ( '<STR_LIT:\n>' ) : <EOL> line = line . strip ( ) <EOL> if line . startswith ( '<STR_LIT:#>' ) or ( not ( '<STR_LIT:=>' in line ) ) : <EOL> continue <EOL> else : <EOL> try : <EOL> i = line . index ( '<STR_LIT:=>' ) <EOL> key = line [ : i ] . strip ( ) <EOL> val = eval ( line [ i + <NUM_LIT:1> : ] . strip ( ) ) <EOL> d [ key ] = val <EOL> except Exception as e : <EOL> continue <EOL> self . setDict ( d ) <EOL> except Exception as e : <EOL> errmsg = "<STR_LIT>" % ( <EOL> self . preffile , str ( e ) ) <EOL> if onError == '<STR_LIT>' : <EOL> pass <EOL> elif onError == '<STR_LIT>' : <EOL> self . logger . warning ( errmsg ) <EOL> else : <EOL> raise SettingError ( errmsg ) <EOL> def _check ( self , d ) : <EOL> if isinstance ( d , dict ) : <EOL> for key , value in d . items ( ) : <EOL> d [ key ] = self . _check ( value ) <EOL> return d <EOL> try : <EOL> if numpy . isnan ( d ) : <EOL> return <NUM_LIT:0.0> <EOL> elif numpy . isinf ( d ) : <EOL> return <NUM_LIT:0.0> <EOL> except Exception : <EOL> pass <EOL> return d <EOL> def save ( self ) : <EOL> d = self . getDict ( ) <EOL> self . _check ( d ) <EOL> try : <EOL> keys = list ( d . keys ( ) ) <EOL> keys . sort ( ) <EOL> with open ( self . preffile , '<STR_LIT:w>' ) as out_f : <EOL> for key in keys : <EOL> out_f . write ( "<STR_LIT>" % ( key , repr ( d [ key ] ) ) ) <EOL> except Exception as e : <EOL> errmsg = "<STR_LIT>" % ( <EOL> self . preffile , str ( e ) ) <EOL> self . logger . error ( errmsg ) <EOL> class Preferences ( object ) : <EOL> def __init__ ( self , basefolder = None , logger = None ) : <EOL> self . folder = basefolder <EOL> self . logger = logger <EOL> self . settings = Bunch . Bunch ( caseless = True ) <EOL> def setDefaults ( self , category , ** kwdargs ) : <EOL> self . settings [ category ] . addDefaults ( ** kwdargs ) <EOL> def getSettings ( self , category ) : <EOL> return self . settings [ category ] <EOL> def remove_settings ( self , category ) : <EOL> del self . settings [ category ] <EOL> def get_dict_category ( self , category ) : <EOL> return self . settings [ category ] . getDict ( ) <EOL> def createCategory ( self , category ) : <EOL> if category not in self . settings : <EOL> suffix = '<STR_LIT>' <EOL> path = os . path . join ( self . folder , category + suffix ) <EOL> self . settings [ category ] = SettingGroup ( logger = self . logger , <EOL> name = category , <EOL> preffile = path ) <EOL> return self . settings [ category ] <EOL> def get_baseFolder ( self ) : <EOL> return self . folder <EOL> def getDict ( self ) : <EOL> return dict ( [ [ name , self . settings [ name ] . getDict ( ) ] for name in <EOL> self . settings . keys ( ) ] ) </s>
