<s> import json <EOL> import re <EOL> import os <EOL> import os . path <EOL> from collections import OrderedDict <EOL> comment_re = re . compile ( <EOL> '<STR_LIT>' , <EOL> re . DOTALL | re . MULTILINE <EOL> ) <EOL> c = { } <EOL> def fixup ( adict , k , v ) : <EOL> """<STR_LIT>""" <EOL> for key in adict . keys ( ) : <EOL> if key == k : <EOL> adict [ key ] = v <EOL> elif isinstance ( adict [ key ] , dict ) : <EOL> fixup ( adict [ key ] , k , v ) <EOL> def load_config ( filepath ) : <EOL> """<STR_LIT>""" <EOL> with open ( filepath ) as f : <EOL> content = '<STR_LIT>' . join ( f . readlines ( ) ) <EOL> match = comment_re . search ( content ) <EOL> while match : <EOL> content = content [ : match . start ( ) ] + content [ match . end ( ) : ] <EOL> match = comment_re . search ( content ) <EOL> return json . loads ( content , object_pairs_hook = OrderedDict ) <EOL> def get_all_config ( ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> path = os . path . expanduser ( "<STR_LIT>" ) + os . sep + '<STR_LIT>' <EOL> data = load_config ( path ) <EOL> data . pop ( '<STR_LIT>' , None ) <EOL> data . pop ( '<STR_LIT>' , None ) <EOL> data . pop ( '<STR_LIT>' , None ) <EOL> data . pop ( '<STR_LIT>' , None ) <EOL> data . pop ( '<STR_LIT>' , None ) <EOL> return data <EOL> except : <EOL> return [ ] <EOL> def get_default_config ( key ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> path = os . path . dirname ( __file__ ) + '<STR_LIT>' <EOL> data = load_config ( path ) <EOL> return data [ key ] <EOL> except : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> def get_config ( key ) : <EOL> """<STR_LIT>""" <EOL> return c [ key ] <EOL> def set_config ( key , value ) : <EOL> """<STR_LIT>""" <EOL> if value . isdigit ( ) : <EOL> value = int ( value ) <EOL> elif value . lower ( ) == '<STR_LIT:true>' : <EOL> value = True <EOL> elif value . lower ( ) == '<STR_LIT:false>' : <EOL> value = False <EOL> c [ key ] = value <EOL> path = os . path . expanduser ( "<STR_LIT>" ) + os . sep + '<STR_LIT>' <EOL> data = { } <EOL> try : <EOL> data = load_config ( path ) <EOL> except : <EOL> return <EOL> if key in data : <EOL> fixup ( data , key , value ) <EOL> else : <EOL> data [ key ] = value <EOL> with open ( path , '<STR_LIT:w>' ) as out : <EOL> json . dump ( data , out , indent = <NUM_LIT:4> ) <EOL> os . system ( '<STR_LIT>' + path ) <EOL> def delete_config ( key ) : <EOL> """<STR_LIT>""" <EOL> path = os . path . expanduser ( "<STR_LIT>" ) + os . sep + '<STR_LIT>' <EOL> try : <EOL> data = load_config ( path ) <EOL> except : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> if key in data and key in c : <EOL> data . pop ( key ) <EOL> c . pop ( key ) <EOL> try : <EOL> data [ key ] = c [ key ] = get_default_config ( key ) <EOL> except : <EOL> pass <EOL> else : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> with open ( path , '<STR_LIT:w>' ) as out : <EOL> json . dump ( data , out , indent = <NUM_LIT:4> ) <EOL> os . system ( '<STR_LIT>' + path ) <EOL> def reload_config ( ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> rainbow_config = os . path . expanduser ( "<STR_LIT>" ) + os . sep + '<STR_LIT>' <EOL> data = load_config ( rainbow_config ) <EOL> for d in data : <EOL> c [ d ] = data [ d ] <EOL> except : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> def init_config ( ) : <EOL> """<STR_LIT>""" <EOL> config = os . path . dirname ( __file__ ) + '<STR_LIT>' <EOL> try : <EOL> data = load_config ( config ) <EOL> for d in data : <EOL> c [ d ] = data [ d ] <EOL> except : <EOL> pass <EOL> rainbow_config = os . path . expanduser ( "<STR_LIT>" ) + os . sep + '<STR_LIT>' <EOL> try : <EOL> data = load_config ( rainbow_config ) <EOL> for d in data : <EOL> c [ d ] = data [ d ] <EOL> except ( IOError , ValueError ) as e : <EOL> c [ '<STR_LIT>' ] = str ( e ) <EOL> theme_file = os . path . dirname ( __file__ ) + '<STR_LIT>' + c [ '<STR_LIT>' ] + '<STR_LIT>' <EOL> try : <EOL> data = load_config ( theme_file ) <EOL> for d in data : <EOL> c [ d ] = data [ d ] <EOL> except : <EOL> pass <EOL> init_config ( ) </s>
