6262# This for import completion
6363from bpython import importcompletion
6464
65+ # This for config
66+ from bpython .config import Struct , loadini , migrate_rc
67+
6568from bpython import __version__
6669
6770def log (x ):
@@ -71,11 +74,6 @@ def log(x):
7174orig_stdout = sys .__stdout__
7275stdscr = None
7376
74- class Struct (object ):
75- """Simple class for instantiating objects we can add arbitrary attributes
76- to and use for various arbitrary things."""
77- pass
78-
7977
8078class FakeStdin (object ):
8179 """Provide a fake stdin type for things like raw_input() etc."""
@@ -1834,124 +1832,6 @@ def do_resize(caller):
18341832 caller .resize ()
18351833# The list win resizes itself every time it appears so no need to do it here.
18361834
1837- def migrate_rc (path ):
1838- """Use the shlex module to convert the old configuration file to the new format.
1839- The old configuration file is renamed but not removed by now."""
1840- import shlex
1841- f = open (path )
1842- parser = shlex .shlex (f )
1843-
1844- bools = {
1845- 'true' : True ,
1846- 'yes' : True ,
1847- 'on' : True ,
1848- 'false' : False ,
1849- 'no' : False ,
1850- 'off' : False
1851- }
1852-
1853- config = ConfigParser ()
1854- config .add_section ('general' )
1855-
1856- while True :
1857- k = parser .get_token ()
1858- v = None
1859-
1860- if not k :
1861- break
1862-
1863- k = k .lower ()
1864-
1865- if parser .get_token () == '=' :
1866- v = parser .get_token () or None
1867-
1868- if v is not None :
1869- try :
1870- v = int (v )
1871- except ValueError :
1872- if v .lower () in bools :
1873- v = bools [v .lower ()]
1874- config .set ('general' , k , v )
1875- f .close ()
1876- f = open (os .path .expanduser ('~/.bpython.ini' ), 'w' )
1877- config .write (f )
1878- f .close ()
1879- os .rename (path , os .path .expanduser ('~/.bpythonrc.bak' ))
1880- print ("The configuration file for bpython has been changed. A new "
1881- ".bpython.ini file has been created in your home directory." )
1882- print ("The existing .bpythonrc file has been renamed to .bpythonrc.bak "
1883- "and it can be removed." )
1884- print "Press enter to continue."
1885- raw_input ()
1886-
1887-
1888- class CP (ConfigParser ):
1889- def safeget (self , section , option , default ):
1890- """safet get method using default values"""
1891- try :
1892- v = self .get (section , option )
1893- except NoSectionError :
1894- v = default
1895- except NoOptionError :
1896- v = default
1897- if isinstance (v , bool ):
1898- return v
1899- try :
1900- return int (v )
1901- except ValueError :
1902- return v
1903-
1904- def loadini (configfile ):
1905- """Loads .ini configuration file and stores its values in OPTS"""
1906-
1907- configfile = os .path .expanduser (configfile )
1908-
1909- config = CP ()
1910- config .read (configfile )
1911-
1912- OPTS .tab_length = config .safeget ('general' , 'tab_length' , 4 )
1913- OPTS .auto_display_list = config .safeget ('general' , 'auto_display_list' , True )
1914- OPTS .syntax = config .safeget ('general' , 'syntax' , True )
1915- OPTS .arg_spec = config .safeget ('general' , 'arg_spec' , True )
1916- OPTS .hist_file = config .safeget ('general' , 'hist_file' , '~/.pythonhist' )
1917- OPTS .hist_length = config .safeget ('general' , 'hist_length' , 100 )
1918- OPTS .flush_output = config .safeget ('general' , 'flush_output' , True )
1919- color_scheme_name = config .safeget ('general' , 'color_scheme' , 'default' )
1920-
1921- if color_scheme_name == 'default' :
1922- OPTS .color_scheme = {
1923- 'keyword' : 'Y' ,
1924- 'name' : 'B' ,
1925- 'comment' : 'b' ,
1926- 'string' : 'g' ,
1927- 'error' : 'r' ,
1928- 'number' : 'g' ,
1929- 'operator' : 'C' ,
1930- 'punctuation' : 'c' ,
1931- 'token' : 'g' ,
1932- 'background' : 'k' ,
1933- 'output' : 'w' ,
1934- 'main' : 'c' ,
1935- 'prompt' : 'y' ,
1936- 'prompt_more' : 'g' ,
1937- }
1938- else :
1939- path = os .path .expanduser ('~/.bpython/%s.theme' % (color_scheme_name ,))
1940- load_theme (path )
1941-
1942- def load_theme (path ):
1943- theme = CP ()
1944- f = open (path , 'r' )
1945- theme .readfp (f )
1946- OPTS .color_scheme = {}
1947- for k , v in chain (theme .items ('syntax' ), theme .items ('interface' )):
1948- if theme .has_option ('syntax' , k ):
1949- OPTS .color_scheme [k ] = theme .get ('syntax' , k )
1950- else :
1951- OPTS .color_scheme [k ] = theme .get ('interface' , k )
1952- f .close ()
1953-
1954-
19551835class FakeDict (object ):
19561836 """Very simple dict-alike that returns a constant value for any key -
19571837 used as a hacky solution to using a colours dict containing colour codes if
@@ -2053,7 +1933,7 @@ def main(args=None):
20531933 path = os .path .expanduser ('~/.bpythonrc' ) # migrating old configuration file
20541934 if os .path .isfile (path ):
20551935 migrate_rc (path )
2056- loadini (options .config )
1936+ loadini (OPTS , options .config )
20571937
20581938 try :
20591939 o = curses .wrapper (main_curses )
0 commit comments