FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
bpython-bpython/bpython/config.py at main · sthagen/bpython-bpython · GitHub
sthagen
/
bpython-bpython
Public
forked from
bpython/bpython
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Security and quality
0
Insights
Additional navigation options
Code
Security and quality
Insights
Expand file tree
Breadcrumbs
bpython-bpython
/
bpython
/
config.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
398 lines (355 loc) · 14.5 KB
Breadcrumbs
bpython-bpython
/
bpython
/
config.py
Copy path
File metadata and controls
398 lines (355 loc) · 14.5 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# The MIT License
#
# Copyright (c) 2009-2015 the bpython authors.
# Copyright (c) 2015-2022 Sebastian Ramacher
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# To gradually migrate to mypy we aren't setting these globally yet
# mypy: disallow_untyped_defs=True
# mypy: disallow_untyped_calls=True
import
os
import
sys
import
locale
from
configparser
import
ConfigParser
from
itertools
import
chain
from
pathlib
import
Path
from
typing
import
Any
,
Dict
from
collections
.
abc
import
MutableMapping
,
Mapping
from
xdg
import
BaseDirectory
from
.
autocomplete
import
AutocompleteModes
default_completion
=
AutocompleteModes
.
SIMPLE
# All supported letters for colors for themes
#
# Instead of importing it from .curtsiesfrontend.parse, we define them here to
# avoid a potential import of fcntl on Windows.
COLOR_LETTERS
=
tuple
(
"krgybmcwd"
)
class
UnknownColorCode
(
Exception
):
def
__init__
(
self
,
key
:
str
,
color
:
str
)
->
None
:
self
.
key
=
key
self
.
color
=
color
def
getpreferredencoding
()
->
str
:
"""Get the user's preferred encoding."""
return
locale
.
getpreferredencoding
()
or
sys
.
getdefaultencoding
()
def
can_encode
(
c
:
str
)
->
bool
:
try
:
c
.
encode
(
getpreferredencoding
())
return
True
except
UnicodeEncodeError
:
return
False
def
supports_box_chars
()
->
bool
:
"""Check if the encoding supports Unicode box characters."""
return
all
(
map
(
can_encode
,
"│─└┘┌┐"
))
def
get_config_home
()
->
Path
:
"""Returns the base directory for bpython's configuration files."""
return
Path
(
BaseDirectory
.
xdg_config_home
)
/
"bpython"
def
default_config_path
()
->
Path
:
"""Returns bpython's default configuration file path."""
return
get_config_home
()
/
"config"
def
default_editor
()
->
str
:
"""Returns the default editor."""
return
os
.
environ
.
get
(
"VISUAL"
,
os
.
environ
.
get
(
"EDITOR"
,
"vi"
))
def
fill_config_with_default_values
(
config
:
ConfigParser
,
default_values
:
Mapping
[
str
,
Mapping
[
str
,
Any
]]
)
->
None
:
for
section
in
default_values
.
keys
():
if
not
config
.
has_section
(
section
):
config
.
add_section
(
section
)
for
opt
,
val
in
default_values
[
section
].
items
():
if
not
config
.
has_option
(
section
,
opt
):
config
.
set
(
section
,
opt
,
str
(
val
))
class
Config
:
default_colors
=
{
"keyword"
:
"y"
,
"name"
:
"c"
,
"comment"
:
"b"
,
"string"
:
"m"
,
"error"
:
"r"
,
"number"
:
"G"
,
"operator"
:
"Y"
,
"punctuation"
:
"y"
,
"token"
:
"C"
,
"background"
:
"d"
,
"output"
:
"w"
,
"main"
:
"c"
,
"paren"
:
"R"
,
"prompt"
:
"c"
,
"prompt_more"
:
"g"
,
"right_arrow_suggestion"
:
"K"
,
}
defaults
:
dict
[
str
,
dict
[
str
,
Any
]]
=
{
"general"
: {
"arg_spec"
:
True
,
"auto_display_list"
:
True
,
"autocomplete_mode"
:
default_completion
,
"color_scheme"
:
"default"
,
"complete_magic_methods"
:
True
,
"dedent_after"
:
1
,
"default_autoreload"
:
False
,
"editor"
:
default_editor
(),
"flush_output"
:
True
,
"import_completion_skiplist"
:
":"
.
join
(
(
# version tracking
".git"
,
".svn"
,
".hg"
# XDG
".config"
,
".local"
,
".share"
,
# nodejs
"node_modules"
,
# PlayOnLinux
"PlayOnLinux's virtual drives"
,
# wine
"dosdevices"
,
# Python byte code cache
"__pycache__"
,
)
),
"highlight_show_source"
:
True
,
"hist_duplicates"
:
True
,
"hist_file"
:
"~/.pythonhist"
,
"hist_length"
:
1000
,
"paste_time"
:
0.02
,
"pastebin_confirm"
:
True
,
"pastebin_expiry"
:
"1week"
,
"pastebin_helper"
:
""
,
"pastebin_url"
:
"https://bpaste.net"
,
"save_append_py"
:
False
,
"single_undo_time"
:
1.0
,
"syntax"
:
True
,
"tab_length"
:
4
,
"unicode_box"
:
True
,
"brackets_completion"
:
False
,
},
"keyboard"
: {
"backspace"
:
"C-h"
,
"beginning_of_line"
:
"C-a"
,
"clear_line"
:
"C-u"
,
"clear_screen"
:
"C-l"
,
"clear_word"
:
"C-w"
,
"copy_clipboard"
:
"F10"
,
"cut_to_buffer"
:
"C-k"
,
"delete"
:
"C-d"
,
"down_one_line"
:
"C-n"
,
"edit_config"
:
"F3"
,
"edit_current_block"
:
"C-x"
,
"end_of_line"
:
"C-e"
,
"exit"
:
""
,
"external_editor"
:
"F7"
,
"help"
:
"F1"
,
"incremental_search"
:
"M-s"
,
"last_output"
:
"F9"
,
"left"
:
"C-b"
,
"pastebin"
:
"F8"
,
"redo"
:
"C-g"
,
"reimport"
:
"F6"
,
"reverse_incremental_search"
:
"M-r"
,
"right"
:
"C-f"
,
"save"
:
"C-s"
,
"search"
:
"C-o"
,
"show_source"
:
"F2"
,
"suspend"
:
"C-z"
,
"toggle_file_watch"
:
"F5"
,
"transpose_chars"
:
"C-t"
,
"undo"
:
"C-r"
,
"up_one_line"
:
"C-p"
,
"yank_from_buffer"
:
"C-y"
,
},
"cli"
: {
"suggestion_width"
:
0.8
,
"trim_prompts"
:
False
,
},
"curtsies"
: {
"list_above"
:
False
,
"right_arrow_completion"
:
True
,
},
}
def
__init__
(
self
,
config_path
:
Path
|
None
=
None
)
->
None
:
"""Loads .ini configuration file and stores its values."""
config
=
ConfigParser
()
fill_config_with_default_values
(
config
,
self
.
defaults
)
try
:
if
config_path
is
not
None
:
config
.
read
(
config_path
)
except
UnicodeDecodeError
as
e
:
sys
.
stderr
.
write
(
"Error: Unable to parse config file at '{}' due to an "
"encoding issue ({}). Please make sure to fix the encoding "
"of the file or remove it and then try again.
\n
"
.
format
(
config_path
,
e
)
)
sys
.
exit
(
1
)
default_keys_to_commands
=
{
value
:
key
for
(
key
,
value
)
in
self
.
defaults
[
"keyboard"
].
items
()
}
def
get_key_no_doublebind
(
command
:
str
)
->
str
:
default_commands_to_keys
=
self
.
defaults
[
"keyboard"
]
requested_key
=
config
.
get
(
"keyboard"
,
command
)
try
:
default_command
=
default_keys_to_commands
[
requested_key
]
if
default_commands_to_keys
[
default_command
]
==
config
.
get
(
"keyboard"
,
default_command
):
setattr
(
self
,
f"
{
default_command
}
_key"
,
""
)
except
KeyError
:
pass
return
requested_key
self
.
config_path
=
(
config_path
.
absolute
()
if
config_path
is
not
None
else
None
)
self
.
hist_file
=
Path
(
config
.
get
(
"general"
,
"hist_file"
)).
expanduser
()
self
.
dedent_after
=
config
.
getint
(
"general"
,
"dedent_after"
)
self
.
tab_length
=
config
.
getint
(
"general"
,
"tab_length"
)
self
.
auto_display_list
=
config
.
getboolean
(
"general"
,
"auto_display_list"
)
self
.
syntax
=
config
.
getboolean
(
"general"
,
"syntax"
)
self
.
arg_spec
=
config
.
getboolean
(
"general"
,
"arg_spec"
)
self
.
paste_time
=
config
.
getfloat
(
"general"
,
"paste_time"
)
self
.
single_undo_time
=
config
.
getfloat
(
"general"
,
"single_undo_time"
)
self
.
highlight_show_source
=
config
.
getboolean
(
"general"
,
"highlight_show_source"
)
self
.
editor
=
config
.
get
(
"general"
,
"editor"
)
self
.
hist_length
=
config
.
getint
(
"general"
,
"hist_length"
)
self
.
hist_duplicates
=
config
.
getboolean
(
"general"
,
"hist_duplicates"
)
self
.
flush_output
=
config
.
getboolean
(
"general"
,
"flush_output"
)
self
.
default_autoreload
=
config
.
getboolean
(
"general"
,
"default_autoreload"
)
self
.
import_completion_skiplist
=
config
.
get
(
"general"
,
"import_completion_skiplist"
).
split
(
":"
)
self
.
pastebin_key
=
get_key_no_doublebind
(
"pastebin"
)
self
.
copy_clipboard_key
=
get_key_no_doublebind
(
"copy_clipboard"
)
self
.
save_key
=
get_key_no_doublebind
(
"save"
)
self
.
search_key
=
get_key_no_doublebind
(
"search"
)
self
.
show_source_key
=
get_key_no_doublebind
(
"show_source"
)
self
.
suspend_key
=
get_key_no_doublebind
(
"suspend"
)
self
.
toggle_file_watch_key
=
get_key_no_doublebind
(
"toggle_file_watch"
)
self
.
undo_key
=
get_key_no_doublebind
(
"undo"
)
self
.
redo_key
=
get_key_no_doublebind
(
"redo"
)
self
.
reimport_key
=
get_key_no_doublebind
(
"reimport"
)
self
.
reverse_incremental_search_key
=
get_key_no_doublebind
(
"reverse_incremental_search"
)
self
.
incremental_search_key
=
get_key_no_doublebind
(
"incremental_search"
)
self
.
up_one_line_key
=
get_key_no_doublebind
(
"up_one_line"
)
self
.
down_one_line_key
=
get_key_no_doublebind
(
"down_one_line"
)
self
.
cut_to_buffer_key
=
get_key_no_doublebind
(
"cut_to_buffer"
)
self
.
yank_from_buffer_key
=
get_key_no_doublebind
(
"yank_from_buffer"
)
self
.
clear_word_key
=
get_key_no_doublebind
(
"clear_word"
)
self
.
backspace_key
=
get_key_no_doublebind
(
"backspace"
)
self
.
clear_line_key
=
get_key_no_doublebind
(
"clear_line"
)
self
.
clear_screen_key
=
get_key_no_doublebind
(
"clear_screen"
)
self
.
delete_key
=
get_key_no_doublebind
(
"delete"
)
self
.
left_key
=
get_key_no_doublebind
(
"left"
)
self
.
right_key
=
get_key_no_doublebind
(
"right"
)
self
.
end_of_line_key
=
get_key_no_doublebind
(
"end_of_line"
)
self
.
beginning_of_line_key
=
get_key_no_doublebind
(
"beginning_of_line"
)
self
.
transpose_chars_key
=
get_key_no_doublebind
(
"transpose_chars"
)
self
.
exit_key
=
get_key_no_doublebind
(
"exit"
)
self
.
last_output_key
=
get_key_no_doublebind
(
"last_output"
)
self
.
edit_config_key
=
get_key_no_doublebind
(
"edit_config"
)
self
.
edit_current_block_key
=
get_key_no_doublebind
(
"edit_current_block"
)
self
.
external_editor_key
=
get_key_no_doublebind
(
"external_editor"
)
self
.
help_key
=
get_key_no_doublebind
(
"help"
)
self
.
pastebin_confirm
=
config
.
getboolean
(
"general"
,
"pastebin_confirm"
)
self
.
pastebin_url
=
config
.
get
(
"general"
,
"pastebin_url"
)
self
.
pastebin_expiry
=
config
.
get
(
"general"
,
"pastebin_expiry"
)
self
.
pastebin_helper
=
config
.
get
(
"general"
,
"pastebin_helper"
)
self
.
cli_suggestion_width
=
config
.
getfloat
(
"cli"
,
"suggestion_width"
)
self
.
cli_trim_prompts
=
config
.
getboolean
(
"cli"
,
"trim_prompts"
)
self
.
complete_magic_methods
=
config
.
getboolean
(
"general"
,
"complete_magic_methods"
)
self
.
autocomplete_mode
=
(
AutocompleteModes
.
from_string
(
config
.
get
(
"general"
,
"autocomplete_mode"
)
)
or
default_completion
)
self
.
save_append_py
=
config
.
getboolean
(
"general"
,
"save_append_py"
)
self
.
curtsies_list_above
=
config
.
getboolean
(
"curtsies"
,
"list_above"
)
self
.
curtsies_right_arrow_completion
=
config
.
getboolean
(
"curtsies"
,
"right_arrow_completion"
)
self
.
unicode_box
=
config
.
getboolean
(
"general"
,
"unicode_box"
)
self
.
color_scheme
=
dict
()
color_scheme_name
=
config
.
get
(
"general"
,
"color_scheme"
)
if
color_scheme_name
==
"default"
:
self
.
color_scheme
.
update
(
self
.
default_colors
)
else
:
path
=
get_config_home
()
/
f"
{
color_scheme_name
}
.theme"
try
:
load_theme
(
path
,
self
.
color_scheme
,
self
.
default_colors
)
except
OSError
:
sys
.
stderr
.
write
(
f"Could not load theme '
{
color_scheme_name
}
' from
{
path
}
.
\n
"
)
sys
.
exit
(
1
)
except
UnknownColorCode
as
ucc
:
sys
.
stderr
.
write
(
f"Theme '
{
color_scheme_name
}
' contains invalid color:
{
ucc
.
key
}
=
{
ucc
.
color
}
.
\n
"
)
sys
.
exit
(
1
)
# set box drawing characters
(
self
.
left_border
,
self
.
right_border
,
self
.
top_border
,
self
.
bottom_border
,
self
.
left_bottom_corner
,
self
.
right_bottom_corner
,
self
.
left_top_corner
,
self
.
right_top_corner
,
)
=
(
(
"│"
,
"│"
,
"─"
,
"─"
,
"└"
,
"┘"
,
"┌"
,
"┐"
)
if
self
.
unicode_box
and
supports_box_chars
()
else
(
"|"
,
"|"
,
"-"
,
"-"
,
"+"
,
"+"
,
"+"
,
"+"
)
)
self
.
brackets_completion
=
config
.
getboolean
(
"general"
,
"brackets_completion"
)
def
load_theme
(
path
:
Path
,
colors
:
MutableMapping
[
str
,
str
],
default_colors
:
Mapping
[
str
,
str
],
)
->
None
:
theme
=
ConfigParser
()
with
open
(
path
)
as
f
:
theme
.
read_file
(
f
)
for
k
,
v
in
chain
(
theme
.
items
(
"syntax"
),
theme
.
items
(
"interface"
)):
if
theme
.
has_option
(
"syntax"
,
k
):
colors
[
k
]
=
theme
.
get
(
"syntax"
,
k
)
else
:
colors
[
k
]
=
theme
.
get
(
"interface"
,
k
)
if
colors
[
k
].
lower
()
not
in
COLOR_LETTERS
:
raise
UnknownColorCode
(
k
,
colors
[
k
])
# Check against default theme to see if all values are defined
for
k
,
v
in
default_colors
.
items
():
if
k
not
in
colors
:
colors
[
k
]
=
v
Back
|
FazBrowse Home
|
New Git URL