Skip to content

Commit c73c32e

Browse files
committed
Moved config stuff to bpython.config.
1 parent 1cc52aa commit c73c32e

File tree

2 files changed

+137
-124
lines changed

2 files changed

+137
-124
lines changed

bpython/cli.py

Lines changed: 4 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@
6262
# This for import completion
6363
from bpython import importcompletion
6464

65+
# This for config
66+
from bpython.config import Struct, loadini, migrate_rc
67+
6568
from bpython import __version__
6669

6770
def log(x):
@@ -71,11 +74,6 @@ def log(x):
7174
orig_stdout = sys.__stdout__
7275
stdscr = 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

8078
class 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-
19551835
class 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)

bpython/config.py

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
import os
2+
from ConfigParser import ConfigParser, NoSectionError, NoOptionError
3+
4+
5+
class Struct(object):
6+
"""Simple class for instantiating objects we can add arbitrary attributes
7+
to and use for various arbitrary things."""
8+
9+
10+
11+
class CP(ConfigParser):
12+
def safeget(self, section, option, default):
13+
"""safet get method using default values"""
14+
try:
15+
v = self.get(section, option)
16+
except NoSectionError:
17+
v = default
18+
except NoOptionError:
19+
v = default
20+
if isinstance(v, bool):
21+
return v
22+
try:
23+
return int(v)
24+
except ValueError:
25+
return v
26+
27+
28+
def loadini(struct, configfile):
29+
"""Loads .ini configuration file and stores its values in struct"""
30+
31+
configfile = os.path.expanduser(configfile)
32+
33+
config = CP()
34+
config.read(configfile)
35+
36+
struct.tab_length = config.safeget('general', 'tab_length', 4)
37+
struct.auto_display_list = config.safeget('general', 'auto_display_list',
38+
True)
39+
struct.syntax = config.safeget('general', 'syntax', True)
40+
struct.arg_spec = config.safeget('general', 'arg_spec', True)
41+
struct.hist_file = config.safeget('general', 'hist_file', '~/.pythonhist')
42+
struct.hist_length = config.safeget('general', 'hist_length', 100)
43+
struct.flush_output = config.safeget('general', 'flush_output', True)
44+
color_scheme_name = config.safeget('general', 'color_scheme', 'default')
45+
46+
if color_scheme_name == 'default':
47+
struct.color_scheme = {
48+
'keyword': 'Y',
49+
'name': 'B',
50+
'comment': 'b',
51+
'string': 'g',
52+
'error': 'r',
53+
'number': 'g',
54+
'operator': 'c',
55+
'punctuation': 'y',
56+
'token': 'g',
57+
'background': 'k',
58+
'output': 'w',
59+
'main': 'c',
60+
'prompt': 'r',
61+
'prompt_more': 'g',
62+
}
63+
else:
64+
path = os.path.expanduser('~/.bpython/%s.theme' % (color_scheme_name,))
65+
# XXX ConfigParser doesn't raise an IOError if it tries to read a file
66+
# that doesn't exist which isn't helpful to us:
67+
if not os.path.isfile(path):
68+
raise IOError("'%s' is not a readable file" % (path,))
69+
load_theme(struct, color_scheme_name)
70+
71+
72+
def load_theme(struct, path):
73+
theme = CP()
74+
f = open(path, 'r')
75+
theme.readfp(f)
76+
struct.color_scheme = {}
77+
for k, v in chain(theme.items('syntax'), theme.items('interface')):
78+
if theme.has_option('syntax', k):
79+
struct.color_scheme[k] = theme.get('syntax', k)
80+
else:
81+
struct.color_scheme[k] = theme.get('interface', k)
82+
f.close()
83+
84+
85+
def migrate_rc(path):
86+
"""Use the shlex module to convert the old configuration file to the new format.
87+
The old configuration file is renamed but not removed by now."""
88+
import shlex
89+
f = open(path)
90+
parser = shlex.shlex(f)
91+
92+
bools = {
93+
'true': True,
94+
'yes': True,
95+
'on': True,
96+
'false': False,
97+
'no': False,
98+
'off': False
99+
}
100+
101+
config = ConfigParser()
102+
config.add_section('general')
103+
104+
while True:
105+
k = parser.get_token()
106+
v = None
107+
108+
if not k:
109+
break
110+
111+
k = k.lower()
112+
113+
if parser.get_token() == '=':
114+
v = parser.get_token() or None
115+
116+
if v is not None:
117+
try:
118+
v = int(v)
119+
except ValueError:
120+
if v.lower() in bools:
121+
v = bools[v.lower()]
122+
config.set('general', k, v)
123+
f.close()
124+
f = open(os.path.expanduser('~/.bpython.ini'), 'w')
125+
config.write(f)
126+
f.close()
127+
os.rename(path, os.path.expanduser('~/.bpythonrc.bak'))
128+
print ("The configuration file for bpython has been changed. A new "
129+
".bpython.ini file has been created in your home directory.")
130+
print ("The existing .bpythonrc file has been renamed to .bpythonrc.bak "
131+
"and it can be removed.")
132+
print "Press enter to continue."
133+
raw_input()

0 commit comments

Comments
 (0)