@@ -9,33 +9,14 @@ class Struct(object):
99 """Simple class for instantiating objects we can add arbitrary attributes
1010 to and use for various arbitrary things."""
1111
12+ def fill_config_with_default_values (config , default_values ):
13+ for section in default_values .iterkeys ():
14+ if not config .has_section (section ):
15+ config .add_section (section )
1216
13-
14- class CP (ConfigParser ):
15- def safeget (self , section , option , default ):
16- """safet get method using default values"""
17- bools_t = ['true' , 'yes' , 'y' , 'on' ]
18- bools_f = ['false' , 'no' , 'n' , 'off' ]
19-
20- try :
21- v = self .get (section , option )
22- except NoSectionError :
23- v = default
24- except NoOptionError :
25- v = default
26- if isinstance (v , bool ):
27- return v
28- try :
29- if v .lower () in bools_t :
30- return True
31- if v .lower () in bools_f :
32- return False
33- except AttributeError :
34- pass
35- try :
36- return int (v )
37- except ValueError :
38- return v
17+ for (opt , val ) in default_values [section ].iteritems ():
18+ if not config .has_option (section , opt ):
19+ config .set (section , opt , str (val ))
3920
4021
4122def loadini (struct , configfile ):
@@ -48,32 +29,59 @@ def loadini(struct, configfile):
4829 # eventually please.
4930 config_path = os .path .expanduser ('~/.bpython.ini' )
5031
51- config = CP ()
32+ config = ConfigParser ()
33+ fill_config_with_default_values (config , {
34+ 'general' : {
35+ 'arg_spec' : True ,
36+ 'auto_display_list' : True ,
37+ 'color_scheme' : 'default' ,
38+ 'flush_output' : True ,
39+ 'hist_file' : '~/.pythonhist' ,
40+ 'hist_length' : 100 ,
41+ 'paste_time' : 0.02 ,
42+ 'syntax' : True ,
43+ 'tab_length' : 4
44+ },
45+ 'keyboard' : {
46+ 'clear_line' : 'C-u' ,
47+ 'clear_screen' : 'C-l' ,
48+ 'clear_word' : 'C-w' ,
49+ 'cut_to_buffer' : 'C-k' ,
50+ 'down_one_line' : 'C-n' ,
51+ 'exit' : 'C-d' ,
52+ 'last_output' : 'F9' ,
53+ 'pastebin' : 'F8' ,
54+ 'save' : 'C-s' ,
55+ 'undo' : 'C-r' ,
56+ 'up_one_line' : 'C-p' ,
57+ 'yank_from_buffer' : 'C-y'
58+ }
59+ })
5260 config .read (config_path )
5361
54- struct .tab_length = config .safeget ('general' , 'tab_length' , 4 )
55- struct .auto_display_list = config .safeget ('general' , 'auto_display_list ' ,
56- True )
57- struct .syntax = config .safeget ('general' , 'syntax' , True )
58- struct .arg_spec = config .safeget ('general' , 'arg_spec' , True )
59- struct .paste_time = config .safeget ('general' , 'paste_time' , 0.02 )
60- struct .hist_file = config .safeget ('general' , 'hist_file' , '~/.pythonhist ' )
61- struct .hist_length = config .safeget ('general' , 'hist_length' , 100 )
62- struct .flush_output = config .safeget ('general' , 'flush_output' , True )
63- struct .pastebin_key = config .safeget ('keyboard' , 'pastebin' , 'F8 ' )
64- struct .save_key = config .safeget ('keyboard' , 'save' , 'C-s ' )
65- struct .undo_key = config .safeget ('keyboard' , 'undo' , 'C-r ' )
66- struct .up_one_line_key = config .safeget ('keyboard' , 'up_one_line' , 'C-p ' )
67- struct .down_one_line_key = config .safeget ('keyboard' , 'down_one_line' , 'C-n ' )
68- struct .cut_to_buffer_key = config .safeget ('keyboard' , 'cut_to_buffer' , 'C-k ' )
69- struct .yank_from_buffer_key = config .safeget ( 'keybard ' , 'yank_from_buffer' , 'C-y ' )
70- struct .clear_word_key = config .safeget ('keyboard' , 'clear_word' , 'C-w ' )
71- struct .clear_line_key = config .safeget ('keyboard' , 'clear_line' , 'C-u ' )
72- struct .clear_screen_key = config .safeget ('keyboard' , 'clear_screen' , 'C-l ' )
73- struct .exit_key = config .safeget ('keyboard' , 'exit' , 'C-d ' )
74- struct .last_output_key = config .safeget ('keyboard' , 'last_output' , 'F9 ' )
75-
76- color_scheme_name = config .safeget ('general' , 'color_scheme' , 'default ' )
62+ struct .tab_length = config .getint ('general' , 'tab_length' )
63+ struct .auto_display_list = config .getboolean ('general' ,
64+ 'auto_display_list' )
65+ struct .syntax = config .getboolean ('general' , 'syntax' )
66+ struct .arg_spec = config .getboolean ('general' , 'arg_spec' )
67+ struct .paste_time = config .getfloat ('general' , 'paste_time' )
68+ struct .hist_file = config .get ('general' , 'hist_file' )
69+ struct .hist_length = config .getint ('general' , 'hist_length' )
70+ struct .flush_output = config .getboolean ('general' , 'flush_output' )
71+ struct .pastebin_key = config .get ('keyboard' , 'pastebin' )
72+ struct .save_key = config .get ('keyboard' , 'save' )
73+ struct .undo_key = config .get ('keyboard' , 'undo' )
74+ struct .up_one_line_key = config .get ('keyboard' , 'up_one_line' )
75+ struct .down_one_line_key = config .get ('keyboard' , 'down_one_line' )
76+ struct .cut_to_buffer_key = config .get ('keyboard' , 'cut_to_buffer' )
77+ struct .yank_from_buffer_key = config .get ( 'keyboard ' , 'yank_from_buffer' )
78+ struct .clear_word_key = config .get ('keyboard' , 'clear_word' )
79+ struct .clear_line_key = config .get ('keyboard' , 'clear_line' )
80+ struct .clear_screen_key = config .get ('keyboard' , 'clear_screen' )
81+ struct .exit_key = config .get ('keyboard' , 'exit' )
82+ struct .last_output_key = config .get ('keyboard' , 'last_output' )
83+
84+ color_scheme_name = config .get ('general' , 'color_scheme' )
7785
7886 if color_scheme_name == 'default' :
7987 struct .color_scheme = {
@@ -102,7 +110,7 @@ def loadini(struct, configfile):
102110 key_dispatch [key ]
103111
104112def load_theme (struct , path , inipath ):
105- theme = CP ()
113+ theme = ConfigParser ()
106114 try :
107115 f = open (path , 'r' )
108116 except (IOError , OSError ), e :
0 commit comments