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