Skip to content

Commit 3b37ac0

Browse files
Federico Ceratto federico cerattoFederico Ceratto federico ceratto
authored andcommitted
Configuration migration added.
user: Federico Ceratto federico.ceratto@gmail.com changed bpython/cli.py
1 parent e6a58e5 commit 3b37ac0

File tree

1 file changed

+52
-6
lines changed

1 file changed

+52
-6
lines changed

bpython/cli.py

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
import termios
4343
import fcntl
4444
import string
45-
import shlex
4645
import socket
4746
import pydoc
4847
import types
@@ -175,7 +174,7 @@ def next_token_inside_string(s, inside_string):
175174

176175

177176
class Interpreter(code.InteractiveInterpreter):
178-
177+
179178
def __init__(self):
180179
"""The syntaxerror callback can be set at any time and will be called
181180
on a caught syntax error. The purpose for this in bpython is so that
@@ -825,7 +824,7 @@ def pastebin(self):
825824
except XMLRPCError, e:
826825
self.statusbar.message( 'Upload failed: %s' % (str(e), ) )
827826
return
828-
827+
829828
paste_url = urljoin(pasteservice_url, '/show/%s/' % (paste_id, ))
830829
self.statusbar.message('Pastebin URL: %s' % (paste_url, ), 10)
831830

@@ -1665,7 +1664,7 @@ def init_wins(scr, cols):
16651664

16661665
main_win = curses.newwin(h-1, w, 0, 0)
16671666
main_win.scrollok(True)
1668-
main_win.keypad(1)
1667+
main_win.keypad(1)
16691668
# Thanks to Angus Gibson for pointing out this missing line which was causing
16701669
# problems that needed dirty hackery to fix. :)
16711670

@@ -1735,6 +1734,49 @@ def do_resize(caller):
17351734
caller.resize()
17361735
# The list win resizes itself every time it appears so no need to do it here.
17371736

1737+
def migrate_rc(path):
1738+
"""Use the shlex module to convert the old configuration file to the new format"""
1739+
import shlex
1740+
f = open(path)
1741+
parser = shlex.shlex(f)
1742+
1743+
bools = {
1744+
'true': True,
1745+
'yes': True,
1746+
'on': True,
1747+
'false': False,
1748+
'no': False,
1749+
'off': False
1750+
}
1751+
1752+
config = ConfigParser()
1753+
config.add_section('general')
1754+
1755+
while True:
1756+
k = parser.get_token()
1757+
v = None
1758+
1759+
if not k:
1760+
break
1761+
1762+
k = k.lower()
1763+
1764+
if parser.get_token() == '=':
1765+
v = parser.get_token() or None
1766+
1767+
if v is not None:
1768+
try:
1769+
v = int(v)
1770+
except ValueError:
1771+
if v.lower() in bools:
1772+
v = bools[v.lower()]
1773+
config.set('general', k, v)
1774+
f.close()
1775+
f = open(os.path.expanduser('~/.bpython.ini'))
1776+
config.write(f)
1777+
f.close()
1778+
1779+
17381780
def loadini():
17391781
"""Loads .ini configuration file and stores its values in OPTS"""
17401782
class CP(ConfigParser):
@@ -1750,12 +1792,12 @@ def safeget(self, section, option, default):
17501792
return int(v)
17511793
except ValueError:
17521794
return v
1753-
1795+
17541796
if len(sys.argv) > 2:
17551797
path = sys.argv[2]
17561798
else:
17571799
configfile = os.path.expanduser('~/.bpython.ini')
1758-
1800+
17591801
config = CP()
17601802
config.read(configfile)
17611803

@@ -1791,6 +1833,10 @@ def main_curses(scr):
17911833
global DO_RESIZE
17921834
DO_RESIZE = False
17931835
signal.signal(signal.SIGWINCH, lambda *_: sigwinch(scr))
1836+
1837+
path = os.path.expanduser('~/.bpythonrc') # migrating old configuration file
1838+
if os.path.isfile(path):
1839+
migrate_rc(path)
17941840
loadini()
17951841
stdscr = scr
17961842
try:

0 commit comments

Comments
 (0)