<s> import os <EOL> import os . path <EOL> def get_config_path ( filename ) : <EOL> if os . path . exists ( filename ) : <EOL> return filename <EOL> import sys <EOL> local_path = os . path . join ( sys . path [ <NUM_LIT:0> ] , filename ) <EOL> if os . path . exists ( local_path ) : <EOL> return local_path <EOL> user_home = os . getenv ( '<STR_LIT>' ) or os . getenv ( '<STR_LIT>' ) <EOL> lixian_home = os . getenv ( '<STR_LIT>' ) or user_home <EOL> return os . path . join ( lixian_home , filename ) <EOL> LIXIAN_DEFAULT_CONFIG = get_config_path ( '<STR_LIT>' ) <EOL> LIXIAN_DEFAULT_COOKIES = get_config_path ( '<STR_LIT>' ) <EOL> def load_config ( path ) : <EOL> values = { } <EOL> if os . path . exists ( path ) : <EOL> with open ( path ) as x : <EOL> for line in x . readlines ( ) : <EOL> line = line . strip ( ) <EOL> if line : <EOL> if line . startswith ( '<STR_LIT>' ) : <EOL> line = line . lstrip ( '<STR_LIT:->' ) <EOL> if line . startswith ( '<STR_LIT>' ) : <EOL> values [ line [ <NUM_LIT:3> : ] ] = False <EOL> elif '<STR_LIT:=>' in line : <EOL> k , v = line . split ( '<STR_LIT:=>' , <NUM_LIT:1> ) <EOL> values [ k ] = v <EOL> else : <EOL> values [ line ] = True <EOL> else : <EOL> raise NotImplementedError ( line ) <EOL> return values <EOL> def dump_config ( path , values ) : <EOL> with open ( path , '<STR_LIT:w>' ) as x : <EOL> for k in values : <EOL> v = values [ k ] <EOL> if v is True : <EOL> x . write ( '<STR_LIT>' % k ) <EOL> elif v is False : <EOL> x . write ( '<STR_LIT>' % k ) <EOL> else : <EOL> x . write ( '<STR_LIT>' % ( k , v ) ) <EOL> class Config : <EOL> def __init__ ( self , path = LIXIAN_DEFAULT_CONFIG ) : <EOL> self . path = path <EOL> self . values = load_config ( path ) <EOL> def put ( self , k , v = True ) : <EOL> self . values [ k ] = v <EOL> dump_config ( self . path , self . values ) <EOL> def get ( self , k , v = None ) : <EOL> return self . values . get ( k , v ) <EOL> def delete ( self , k ) : <EOL> if k in self . values : <EOL> del self . values [ k ] <EOL> dump_config ( self . path , self . values ) <EOL> def source ( self ) : <EOL> if os . path . exists ( self . path ) : <EOL> with open ( self . path ) as x : <EOL> return x . read ( ) <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % self . values <EOL> global_config = Config ( ) <EOL> def put_config ( k , v = True ) : <EOL> if k . startswith ( '<STR_LIT>' ) and v is True : <EOL> k = k [ <NUM_LIT:3> : ] <EOL> v = False <EOL> global_config . put ( k , v ) <EOL> def get_config ( k , v = None ) : <EOL> return global_config . get ( k , v ) <EOL> def delete_config ( k ) : <EOL> return global_config . delete ( k ) <EOL> def source_config ( ) : <EOL> return global_config . source ( ) </s>
