Skip to content

Commit 1370bbf

Browse files
Federico Ceratto federico cerattoFederico Ceratto federico ceratto
authored andcommitted
loadrc() changed in loadini() to parse .ini configuration files
user: Federico Ceratto federico.ceratto@gmail.com changed bpython/cli.py
1 parent 802df4b commit 1370bbf

File tree

1 file changed

+29
-59
lines changed

1 file changed

+29
-59
lines changed

bpython/cli.py

Lines changed: 29 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
from cStringIO import StringIO
5050
from urlparse import urljoin
5151
from xmlrpclib import ServerProxy, Error as XMLRPCError
52+
from ConfigParser import ConfigParser, NoSectionError, NoOptionError
5253

5354
# These are used for syntax hilighting.
5455
from pygments import highlight
@@ -112,16 +113,6 @@ def readlines(self, x):
112113
OPTS = Struct()
113114
DO_RESIZE = False
114115

115-
116-
# Set default values. (Overridden by loadrc())
117-
OPTS.tab_length = 4
118-
OPTS.auto_display_list = True
119-
OPTS.syntax = True
120-
OPTS.arg_spec = True
121-
OPTS.hist_file = '~/.pythonhist'
122-
OPTS.hist_length = 100
123-
OPTS.flush_output = True
124-
125116
# TODO:
126117
#
127118
# C-l doesn't repaint the screen yet.
@@ -1718,59 +1709,38 @@ def do_resize(caller):
17181709
caller.resize()
17191710
# The list win resizes itself every time it appears so no need to do it here.
17201711

1721-
1722-
def loadrc():
1723-
"""Use the shlex module to make a simple lexer for the settings,
1724-
it also attempts to convert any integers to Python ints, otherwise
1725-
leaves them as strings and handles hopefully all the sane ways of
1726-
representing a boolean."""
1727-
1712+
def loadini():
1713+
"""Loads .ini configuration file and stores its values in OPTS"""
1714+
class CP(ConfigParser):
1715+
def safeget(self, section, option, default):
1716+
"""safet get method using default values"""
1717+
try:
1718+
v = self.get(section, option)
1719+
except NoSectionError, NoOptionError:
1720+
v = default
1721+
if isinstance(v, bool):
1722+
return v
1723+
try:
1724+
return int(v)
1725+
except ValueError:
1726+
return v
1727+
17281728
if len(sys.argv) > 2:
17291729
path = sys.argv[2]
17301730
else:
1731-
path = os.path.expanduser('~/.bpythonrc')
1732-
1733-
if not os.path.isfile(path):
1734-
return
1735-
1736-
f = open(path)
1737-
parser = shlex.shlex(f)
1738-
1739-
bools = {
1740-
'true': True,
1741-
'yes': True,
1742-
'on': True,
1743-
'false': False,
1744-
'no': False,
1745-
'off': False
1746-
}
1747-
1748-
config = {}
1749-
while True:
1750-
k = parser.get_token()
1751-
v = None
1752-
1753-
if not k:
1754-
break
1755-
1756-
k = k.lower()
1757-
1758-
if parser.get_token() == '=':
1759-
v = parser.get_token() or None
1760-
1761-
if v is not None:
1762-
try:
1763-
v = int(v)
1764-
except ValueError:
1765-
if v.lower() in bools:
1766-
v = bools[v.lower()]
1731+
configfile = os.path.expanduser('~/.bpython.ini')
1732+
1733+
config = CP()
1734+
config.read(configfile)
17671735

1768-
config[k] = v
1769-
f.close()
1736+
OPTS.tab_length = config.safeget('general', 'tab_length', 4)
1737+
OPTS.auto_display_list = config.safeget('general', 'auto_display_list', True)
1738+
OPTS.syntax = config.safeget('general', 'syntax', True)
1739+
OPTS.arg_spec = config.safeget('general', 'arg_spec', True)
1740+
OPTS.hist_file = config.safeget('general', 'hist_file', '~/.pythonhist')
1741+
OPTS.hist_length = config.safeget('general', 'hist_length', 100)
1742+
OPTS.flush_output = config.safeget('general', 'flush_output', True)
17701743

1771-
for k, v in config.iteritems():
1772-
if hasattr(OPTS, k):
1773-
setattr(OPTS, k, v)
17741744

17751745
class FakeDict(object):
17761746
"""Very simple dict-alike that returns a constant value for any key -
@@ -1795,7 +1765,7 @@ def main_curses(scr):
17951765
global DO_RESIZE
17961766
DO_RESIZE = False
17971767
signal.signal(signal.SIGWINCH, lambda *_: sigwinch(scr))
1798-
loadrc()
1768+
loadini()
17991769
stdscr = scr
18001770
try:
18011771
curses.start_color()

0 commit comments

Comments
 (0)