11#!/usr/bin/env python
2- # bpython 0.5.1 ::fancy curses interface to the Python repl::Bob Farrell 2008
2+ # bpython 0.5.2 ::fancy curses interface to the Python repl::Bob Farrell 2008
33#
44# The MIT License
55#
4242import termios
4343import fcntl
4444import string
45- import ConfigParser
45+ import shlex
4646from bpython .formatter import BPythonFormatter
4747
4848class Dummy ( object ):
@@ -768,10 +768,10 @@ def repl( self ):
768768
769769# The regular help() function uses PAGER to display the help, which
770770# screws with bpython.
771- from bpython import internal
772- internal .window = self .scr
773- self .push ('from bpython import internal \n ' )
774- self .push ('help = internal ._help' )
771+ from bpython import _internal
772+ _internal .window = self .scr
773+ self .push ('from bpython import _internal \n ' )
774+ self .push ('help = _internal ._help' )
775775
776776 self .iy , self .ix = self .scr .getyx ()
777777 more = False
@@ -788,6 +788,7 @@ def repl( self ):
788788 self .scr .redrawwin ()
789789 if self .do_exit :
790790 return
791+
791792 self .h_i = 0
792793 self .history .append ( inp )
793794 self .s_hist [- 1 ] += self .f_string
@@ -1385,20 +1386,50 @@ def do_resize( caller ):
13851386 # The list win resizes itself every time it appears so no need to do it here.
13861387
13871388def loadrc ():
1388- """Attempt to load the rc file and apply settings."""
1389+ """Use the shlex module to make a simple lexer for the settings,
1390+ it also attempts to convert any integers to Python ints, otherwise
1391+ leaves them as strings and handles hopefully all the sane ways of
1392+ representing a boolean."""
13891393
1390- c = ConfigParser .ConfigParser ()
1391- if not ( c .read ( os .path .expanduser ('~/.bpythonrc' ) ) ):
1392- return
1393-
1394- if not c .has_section ('Global' ):
1394+ if not os .path .isfile ( os .path .expanduser ( '~/.bpythonrc' ) ):
13951395 return
1396+
1397+ f = open ( os .path .expanduser ( '~/.bpythonrc' ) )
1398+ parser = shlex .shlex ( f )
13961399
1397- if c .has_option ('Global' , 'requiretab' ):
1398- if c .get ('Global' , 'requiretab' ) == 'no' :
1399- OPTS .requiretab = True
1400- else :
1401- OPTS .requiretab = False
1400+ bools = {
1401+ 'true' : True ,
1402+ 'yes' : True ,
1403+ 'false' : False ,
1404+ 'no' : False
1405+ }
1406+
1407+ config = {}
1408+ while True :
1409+ k = parser .get_token ()
1410+ v = None
1411+
1412+ if not k :
1413+ break
1414+
1415+ if parser .get_token () == '=' :
1416+ v = parser .get_token () or None
1417+
1418+ if v is not None :
1419+ try :
1420+ v = int (v )
1421+ except ValueError :
1422+ if v .lower () in bools :
1423+ v = bools [v .lower ()]
1424+
1425+ config [k ] = v
1426+
1427+
1428+ for k in config :
1429+ if hasattr ( OPTS , k ):
1430+ setattr ( OPTS , k , v )
1431+
1432+ stdscr = None
14021433
14031434def main ( scr ):
14041435 """main function for the curses convenience wrapper
@@ -1414,7 +1445,6 @@ def main( scr ):
14141445 global DO_RESIZE
14151446 DO_RESIZE = False
14161447 signal .signal ( signal .SIGWINCH , lambda x ,y : sigwinch (scr ) )
1417-
14181448 loadrc ()
14191449 stdscr = scr
14201450 curses .start_color ()
@@ -1445,7 +1475,8 @@ def main( scr ):
14451475 tb = traceback .format_exc ()
14461476finally : # I don't know why this is necessary; without it the wrapper doesn't always
14471477# do its job.
1448- stdscr .keypad (0 )
1478+ if stdscr is not None :
1479+ stdscr .keypad (0 )
14491480 curses .echo ()
14501481 curses .nocbreak ()
14511482 curses .endwin ()
0 commit comments