Skip to content

Commit f39df53

Browse files
committed
make bpython now use ~/.bpython/config instead of ~/.bpython.ini (but still
fall back, for now, to ~/.bpython.ini if ~/.bpython/config doesn't exist)
1 parent b8dbcad commit f39df53

File tree

6 files changed

+40
-17
lines changed

6 files changed

+40
-17
lines changed

bpython/cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1969,7 +1969,7 @@ def init_wins(scr, cols):
19691969

19701970
# TODO:
19711971
#
1972-
# This should show to be configured keys from bpython.ini
1972+
# This should show to be configured keys from ~/.bpython/config
19731973
#
19741974
statusbar = Statusbar(scr, main_win,
19751975
".:: <C-d> Exit <C-r> Rewind <%s> Save <%s> Pastebin ::." % (OPTS.save_key, OPTS.pastebin_key),
@@ -2130,7 +2130,7 @@ def main(args=None):
21302130
'NOTE: If bpython sees an argument it does '
21312131
'not know, execution falls back to the '
21322132
'regular Python interpreter.')
2133-
parser.add_option('--config', '-c', default='~/.bpython.ini',
2133+
parser.add_option('--config', '-c', default='~/.bpython/config',
21342134
help='use CONFIG instead of default config file')
21352135
parser.add_option('--interactive', '-i', action='store_true',
21362136
help='Drop to bpython shell after running file '

bpython/config.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from ConfigParser import ConfigParser, NoSectionError, NoOptionError
44
from itertools import chain
55
from bpython.keys import key_dispatch
6+
import errno
67

78
class Struct(object):
89
"""Simple class for instantiating objects we can add arbitrary attributes
@@ -13,6 +14,9 @@ class Struct(object):
1314
class 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):
3041
def 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."
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
.\" First parameter, NAME, should be all caps
33
.\" Second parameter, SECTION, should be 1-8, maybe w/ subsection
44
.\" other parameters are allowed: see man(7), man(1)
5-
.TH BPYTHON.INI 5 "August 13, 2008"
5+
.TH BPYTHON-CONFIG 5 "August 13, 2008"
66
.\" Please adjust this date whenever revising the manpage.
77
.\"
88
.\" Some roff macros, for reference:
@@ -16,12 +16,12 @@
1616
.\" .sp <n> insert n+1 empty lines
1717
.\" for manpage-specific macros, see man(7)
1818
.SH NAME
19-
bpython.ini \- user configuration file for bpython
19+
config \- user configuration file for bpython
2020
.SH SYNOPSIS
21-
.B ~/.bpython.ini
21+
.B ~/.bpython/config
2222
.SH DESCRIPTION
2323
The
24-
.B bpython.ini
24+
.B config
2525
contains various options controlling the behaviour of
2626
.B bpython
2727
.

light.theme

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
# which stands for:
44
# blacK, Red, Green, Yellow, Blue, Magenta, Cyan, White, Default
55
# Capital letters represent bold
6-
# Copy to ~/.bpython/foo.theme and set "color_scheme = foo" in ~/.bpython.ini
6+
# Copy to ~/.bpython/foo.theme and set "color_scheme = foo" in
7+
# ~/.bpython/config
78

89
[syntax]
910
keyword = M

sample.ini renamed to sample-config

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
2-
# This is a standard python .ini file
1+
# This is a standard python config file
32
# Valid values can be True, False, integer numbers, strings
4-
# By default bpython will look for ~/.bpython.ini or you can specify a file as
5-
# the first argument on the command line
3+
# By default bpython will look for ~/.bpython/config or you can specify a file
4+
# with the -c option on the command line
65

76
# General section tag
87
[general]

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
packages = ["bpython"],
3232
data_files = [
3333
(os.path.join(man_dir, 'man1'), ['doc/bpython.1']),
34-
(os.path.join(man_dir, 'man5'), ['doc/bpython.ini.5']),
34+
(os.path.join(man_dir, 'man5'), ['doc/bpython-config.5']),
3535
('share/applications', ['data/bpython.desktop'])
3636
],
3737
entry_points = {

0 commit comments

Comments
 (0)