33from ConfigParser import ConfigParser , NoSectionError , NoOptionError
44from itertools import chain
55from bpython .keys import key_dispatch
6+ import errno
67
78class Struct (object ):
89 """Simple class for instantiating objects we can add arbitrary attributes
@@ -13,6 +14,9 @@ class Struct(object):
1314class CP (ConfigParser ):
1415 def safeget (self , section , option , default ):
1516 """safet get method using default values"""
17+ bools_t = ['true' , 'yes' , 'y' , 'on' ]
18+ bools_f = ['false' , 'no' , 'n' , 'off' ]
19+
1620 try :
1721 v = self .get (section , option )
1822 except NoSectionError :
@@ -21,6 +25,13 @@ def safeget(self, section, option, default):
2125 v = default
2226 if isinstance (v , bool ):
2327 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
2435 try :
2536 return int (v )
2637 except ValueError :
@@ -30,10 +41,15 @@ def safeget(self, section, option, default):
3041def loadini (struct , configfile ):
3142 """Loads .ini configuration file and stores its values in struct"""
3243
33- configfile = os .path .expanduser (configfile )
44+ config_path = os .path .expanduser (configfile )
45+ if not os .path .isfile (config_path ) and configfile == '~/.bpython/config' :
46+ # FIXME: I decided ~/.bpython.ini was a crappy place for a config file,
47+ # so this is just a fallback if the default is passed - remove this
48+ # eventually please.
49+ config_path = os .path .expanduser ('~/.bpython.ini' )
3450
3551 config = CP ()
36- config .read (configfile )
52+ config .read (config_path )
3753
3854 struct .tab_length = config .safeget ('general' , 'tab_length' , 4 )
3955 struct .auto_display_list = config .safeget ('general' , 'auto_display_list' ,
@@ -66,7 +82,7 @@ def loadini(struct, configfile):
6682 }
6783 else :
6884 path = os .path .expanduser ('~/.bpython/%s.theme' % (color_scheme_name ,))
69- load_theme (struct , path , configfile )
85+ load_theme (struct , path , config_path )
7086
7187
7288 # checks for valid key configuration this part still sucks
@@ -130,12 +146,19 @@ def migrate_rc(path):
130146 v = bools [v .lower ()]
131147 config .set ('general' , k , v )
132148 f .close ()
133- f = open (os .path .expanduser ('~/.bpython.ini' ), 'w' )
149+ try :
150+ os .makedirs (os .path .expanduser ('~/.bpython' ))
151+ except OSError , e :
152+ if e .errno == errno .EEXIST :
153+ pass
154+ else :
155+ raise
156+ f = open (os .path .expanduser ('~/.bpython/config' ), 'w' )
134157 config .write (f )
135158 f .close ()
136159 os .rename (path , os .path .expanduser ('~/.bpythonrc.bak' ))
137160 print ("The configuration file for bpython has been changed. A new "
138- ".bpython.ini file has been created in your home directory. " )
161+ "config file has been created as ~/.bpython/config " )
139162 print ("The existing .bpythonrc file has been renamed to .bpythonrc.bak "
140163 "and it can be removed." )
141164 print "Press enter to continue."
0 commit comments