Skip to content

Commit 02631dd

Browse files
pep8 of curtsies repl
1 parent f7aa4b2 commit 02631dd

File tree

1 file changed

+39
-27
lines changed

1 file changed

+39
-27
lines changed

bpython/curtsiesfrontend/repl.py

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,16 @@
2929

3030
import bpython
3131
from bpython.repl import Repl as BpythonRepl, SourceNotFound
32-
from bpython.config import Struct, loadini, default_config_path, \
33-
getpreferredencoding
32+
from bpython.config import (Struct, loadini, default_config_path,
33+
getpreferredencoding)
3434
from bpython.formatter import BPythonFormatter
3535
from bpython import autocomplete, importcompletion
3636
from bpython.translations import _
3737
from bpython._py3compat import py3
3838
from bpython.pager import get_pager_command
3939

4040
from bpython.curtsiesfrontend import replpainter as paint
41-
from bpython.curtsiesfrontend import sitefix; sitefix.monkeypatch_quit()
41+
from bpython.curtsiesfrontend import sitefix
4242
from bpython.curtsiesfrontend.coderunner import CodeRunner, FakeOutput
4343
from bpython.curtsiesfrontend.filewatch import ModuleChangedEventHandler
4444
from bpython.curtsiesfrontend.interaction import StatusBar
@@ -47,10 +47,8 @@
4747
from bpython.curtsiesfrontend.parse import parse as bpythonparse
4848
from bpython.curtsiesfrontend.parse import func_for_letter, color_for_letter
4949
from bpython.curtsiesfrontend.preprocess import preprocess
50-
from bpython.curtsiesfrontend.interpreter import Interp, \
51-
code_finished_will_parse
52-
53-
#TODO other autocomplete modes (also fix in other bpython implementations)
50+
from bpython.curtsiesfrontend.interpreter import (Interp,
51+
code_finished_will_parse)
5452

5553
from curtsies.configfile_keynames import keymap as key_dispatch
5654

@@ -62,20 +60,22 @@
6260
HELP_MESSAGE = u"""
6361
Thanks for using bpython!
6462
65-
See http://bpython-interpreter.org/ for info, http://docs.bpython-interpreter.org/ for docs, and https://github.com/bpython/bpython for source.
63+
See http://bpython-interpreter.org/ for more information and
64+
http://docs.bpython-interpreter.org/ for docs.
6665
Please report issues at https://github.com/bpython/bpython/issues
6766
67+
Features:
6868
Try using undo ({config.undo_key})!
69-
Edit the current line ({config.edit_current_block_key}) or the entire session ({config.external_editor_key}) in an external editor! (currently {config.editor})
69+
Edit the current line ({config.edit_current_block_key}) or the entire session ({config.external_editor_key}) in an external editor. (currently {config.editor})
7070
Save sessions ({config.save_key}) or post them to pastebins ({config.pastebin_key})! Current pastebin helper: {config.pastebin_helper}
71-
Re-execute the current session and reload all modules ({config.reimport_key}) to test out changes to a module!
72-
Toggle auto-reload mode ({config.toggle_file_watch_key}) to re-execute the current session when a module you've imported is modified!
71+
Reload all modules and rerun session ({config.reimport_key}) to test out changes to a module.
72+
Toggle auto-reload mode ({config.toggle_file_watch_key}) to re-execute the current session when a module you've imported is modified.
7373
74-
Use bpython-curtsies -i your_script.py to run a file in interactive mode (interpreter in namespace of script).
75-
Use bpython-curtsies -t your_script.py to paste in the contents of a file, as though you typed them.
74+
bpython -i your_script.py runs a file in interactive mode
75+
bpython -t your_script.py pastes the contents of a file into the session
7676
77-
Use a config file at {config_file_location} to customize keys and behavior of bpython.
78-
You can customize which pastebin helper to use and which external editor to use.
77+
A config file at {config_file_location} customizes keys and behavior of bpython.
78+
You can also set which pastebin helper and which external editor to use.
7979
See {example_config_url} for an example config file.
8080
Press {config.edit_config_key} to edit this config file.
8181
"""
@@ -86,11 +86,14 @@
8686

8787

8888
class FakeStdin(object):
89-
"""Stdin object user code references so sys.stdin.read() asked user for interactive input"""
89+
"""The stdin object user code will reference
90+
91+
In user code, sys.stdin.read() asks the user for interactive input,
92+
so this class returns control to the UI to get that input."""
9093
def __init__(self, coderunner, repl, configured_edit_keys=None):
9194
self.coderunner = coderunner
9295
self.repl = repl
93-
self.has_focus = False # whether FakeStdin receives keypress events
96+
self.has_focus = False # whether FakeStdin receives keypress events
9497
self.current_line = ''
9598
self.cursor_offset = 0
9699
self.old_num_lines = 0
@@ -147,8 +150,10 @@ def process_event(self, e):
147150
self.repl.send_to_stdin(self.current_line)
148151

149152
def add_input_character(self, e):
150-
if e == '<SPACE>': e = ' '
151-
if e.startswith('<') and e.endswith('>'): return
153+
if e == '<SPACE>':
154+
e = ' '
155+
if e.startswith('<') and e.endswith('>'):
156+
return
152157
assert len(e) == 1, 'added multiple characters: %r' % e
153158
logger.debug('adding normal char %r to current line', e)
154159

@@ -187,7 +192,8 @@ def write(self, value):
187192
def encoding(self):
188193
return 'UTF8'
189194

190-
#TODO write a read() method
195+
# TODO write a read() method?
196+
191197

192198
class ReevaluateFakeStdin(object):
193199
"""Stdin mock used during reevaluation (undo) so raw_inputs don't have to
@@ -196,6 +202,7 @@ def __init__(self, fakestdin, repl):
196202
self.fakestdin = fakestdin
197203
self.repl = repl
198204
self.readline_results = fakestdin.readline_results[:]
205+
199206
def readline(self):
200207
if self.readline_results:
201208
value = self.readline_results.pop(0)
@@ -204,6 +211,7 @@ def readline(self):
204211
self.repl.send_to_stdout(value)
205212
return value
206213

214+
207215
class Repl(BpythonRepl):
208216
"""Python Repl
209217
@@ -224,15 +232,14 @@ class Repl(BpythonRepl):
224232
received is a window resize event, this works fine.
225233
"""
226234

227-
## initialization, cleanup
228235
def __init__(self,
229236
locals_=None,
230237
config=None,
231238
request_refresh=lambda: None,
232239
schedule_refresh=lambda when=0: None,
233240
request_reload=lambda desc: None,
234241
request_undo=lambda n=1: None,
235-
get_term_hw=lambda:(50, 10),
242+
get_term_hw=lambda: (50, 10),
236243
get_cursor_vertical_diff=lambda: 0,
237244
banner=None,
238245
interp=None,
@@ -376,6 +383,7 @@ def __enter__(self):
376383
if self.watcher:
377384
# for reading modules if they fail to load
378385
old_module_locations = {}
386+
379387
@functools.wraps(self.orig_import)
380388
def new_import(name, globals={}, locals={}, fromlist=[], level=-1):
381389
try:
@@ -393,6 +401,7 @@ def new_import(name, globals={}, locals={}, fromlist=[], level=-1):
393401
return m
394402
__builtins__['__import__'] = new_import
395403

404+
sitefix.monkeypatch_quit()
396405
return self
397406

398407
def __exit__(self, *args):
@@ -732,11 +741,14 @@ def send_current_block_to_external_editor(self, filename=None):
732741
self.cursor_offset = len(self.current_line)
733742

734743
def send_session_to_external_editor(self, filename=None):
735-
for_editor = u'### current bpython session - file will be reevaluated, ### lines will not be run\n'
736-
for_editor += u'\n'.join(line[len(self.ps1):] if line.startswith(self.ps1) else
737-
(line[len(self.ps2):] if line.startswith(self.ps2) else
738-
'### '+line)
739-
for line in self.getstdout().split('\n'))
744+
for_editor = (u"### current bpython session - file will be "
745+
u"reevaluated, ### lines will not be run\n'")
746+
for_editor += u'\n'.join(line[len(self.ps1):]
747+
if line.startswith(self.ps1) else
748+
line[len(self.ps2):]
749+
if line.startswith(self.ps2) else
750+
'### '+line
751+
for line in self.getstdout().split('\n'))
740752
text = self.send_to_external_editor(for_editor)
741753
lines = text.split('\n')
742754
from_editor = [line for line in lines if line[:4] != '### ']

0 commit comments

Comments
 (0)