Skip to content

Commit ac419e1

Browse files
Upgrade to prompt_toolkit 3.0.
- Drop Python <3.5 support and prompt_toolkit 2. - Code formatting with black. - Sorted imports with isort. - Added type annotations. - Separate event loop for reading user input.
1 parent 392b08b commit ac419e1

27 files changed

+2065
-1589
lines changed

.travis.yml

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,17 @@ language: python
44
matrix:
55
include:
66
- python: 3.6
7-
- python: 3.5
8-
- python: 3.4
9-
- python: 3.3
10-
- python: 2.7
11-
- python: 2.6
12-
- python: pypy
13-
- python: pypy3
7+
- python: 3.7
148

159
install:
16-
- travis_retry pip install . pytest
10+
- travis_retry pip install . pytest isort black
1711
- pip list
1812

1913
script:
2014
- echo "$TRAVIS_PYTHON_VERSION"
2115
- ./tests/run_tests.py
16+
17+
# Check wheather the imports were sorted correctly.
18+
- isort -c -rc ptpython tests setup.py examples
19+
20+
- black --check ptpython setup.py examples

examples/asyncio-python-embed.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@
1212
prompt.
1313
"""
1414
from __future__ import unicode_literals
15-
from ptpython.repl import embed
1615

1716
import asyncio
1817

18+
from ptpython.repl import embed
19+
1920
loop = asyncio.get_event_loop()
2021
counter = [0]
2122

@@ -26,7 +27,7 @@ def print_counter():
2627
Coroutine that prints counters and saves it in a global variable.
2728
"""
2829
while True:
29-
print('Counter: %i' % counter[0])
30+
print("Counter: %i" % counter[0])
3031
counter[0] += 1
3132
yield from asyncio.sleep(3)
3233

@@ -37,9 +38,13 @@ def interactive_shell():
3738
Coroutine that starts a Python REPL from which we can access the global
3839
counter variable.
3940
"""
40-
print('You should be able to read and update the "counter[0]" variable from this shell.')
41+
print(
42+
'You should be able to read and update the "counter[0]" variable from this shell.'
43+
)
4144
try:
42-
yield from embed(globals=globals(), return_asyncio_coroutine=True, patch_stdout=True)
45+
yield from embed(
46+
globals=globals(), return_asyncio_coroutine=True, patch_stdout=True
47+
)
4348
except EOFError:
4449
# Stop the loop when quitting the repl. (Ctrl-D press.)
4550
loop.stop()
@@ -53,5 +58,5 @@ def main():
5358
loop.close()
5459

5560

56-
if __name__ == '__main__':
61+
if __name__ == "__main__":
5762
main()

examples/asyncio-ssh-python-embed.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
Run this example and then SSH to localhost, port 8222.
77
"""
88
import asyncio
9-
import asyncssh
109
import logging
1110

11+
import asyncssh
12+
1213
from ptpython.contrib.asyncssh_repl import ReplSSHServerSession
1314

1415
logging.basicConfig()
@@ -19,6 +20,7 @@ class MySSHServer(asyncssh.SSHServer):
1920
"""
2021
Server without authentication, running `ReplSSHServerSession`.
2122
"""
23+
2224
def __init__(self, get_namespace):
2325
self.get_namespace = get_namespace
2426

@@ -37,22 +39,24 @@ def main(port=8222):
3739
loop = asyncio.get_event_loop()
3840

3941
# Namespace exposed in the REPL.
40-
environ = {'hello': 'world'}
42+
environ = {"hello": "world"}
4143

4244
# Start SSH server.
4345
def create_server():
4446
return MySSHServer(lambda: environ)
4547

46-
print('Listening on :%i' % port)
48+
print("Listening on :%i" % port)
4749
print('To connect, do "ssh localhost -p %i"' % port)
4850

4951
loop.run_until_complete(
50-
asyncssh.create_server(create_server, '', port,
51-
server_host_keys=['/etc/ssh/ssh_host_dsa_key']))
52+
asyncssh.create_server(
53+
create_server, "", port, server_host_keys=["/etc/ssh/ssh_host_dsa_key"]
54+
)
55+
)
5256

5357
# Run eventloop.
5458
loop.run_forever()
5559

5660

57-
if __name__ == '__main__':
61+
if __name__ == "__main__":
5862
main()

examples/ptpython_config/config.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@
44
Copy this file to $XDG_CONFIG_HOME/ptpython/config.py
55
"""
66
from __future__ import unicode_literals
7+
78
from prompt_toolkit.filters import ViInsertMode
89
from prompt_toolkit.key_binding.key_processor import KeyPress
910
from prompt_toolkit.keys import Keys
1011
from pygments.token import Token
1112

1213
from ptpython.layout import CompletionVisualisation
1314

14-
__all__ = (
15-
'configure',
16-
)
15+
__all__ = ("configure",)
1716

1817

1918
def configure(repl):
@@ -50,7 +49,7 @@ def configure(repl):
5049

5150
# Swap light/dark colors on or off
5251
repl.swap_light_and_dark = False
53-
52+
5453
# Highlight matching parethesis.
5554
repl.highlight_matching_parenthesis = True
5655

@@ -75,7 +74,7 @@ def configure(repl):
7574
repl.paste_mode = False
7675

7776
# Use the classic prompt. (Display '>>>' instead of 'In [1]'.)
78-
repl.prompt_style = 'classic' # 'classic' or 'ipython'
77+
repl.prompt_style = "classic" # 'classic' or 'ipython'
7978

8079
# Don't insert a blank line after the output.
8180
repl.insert_blank_line_after_output = False
@@ -108,14 +107,14 @@ def configure(repl):
108107
repl.enable_input_validation = True
109108

110109
# Use this colorscheme for the code.
111-
repl.use_code_colorscheme('pastie')
110+
repl.use_code_colorscheme("pastie")
112111

113112
# Set color depth (keep in mind that not all terminals support true color).
114113

115-
#repl.color_depth = 'DEPTH_1_BIT' # Monochrome.
116-
#repl.color_depth = 'DEPTH_4_BIT' # ANSI colors only.
117-
repl.color_depth = 'DEPTH_8_BIT' # The default, 256 colors.
118-
#repl.color_depth = 'DEPTH_24_BIT' # True color.
114+
# repl.color_depth = 'DEPTH_1_BIT' # Monochrome.
115+
# repl.color_depth = 'DEPTH_4_BIT' # ANSI colors only.
116+
repl.color_depth = "DEPTH_8_BIT" # The default, 256 colors.
117+
# repl.color_depth = 'DEPTH_24_BIT' # True color.
119118

120119
# Syntax.
121120
repl.enable_syntax_highlighting = True
@@ -142,7 +141,6 @@ def _(event):
142141
event.current_buffer.validate_and_handle()
143142
"""
144143

145-
146144
# Typing 'jj' in Vi Insert mode, should send escape. (Go back to navigation
147145
# mode.)
148146
"""
@@ -178,8 +176,7 @@ def _(event):
178176
# `ptpython/style.py` for all possible tokens.
179177
_custom_ui_colorscheme = {
180178
# Blue prompt.
181-
Token.Layout.Prompt: 'bg:#eeeeff #000000 bold',
182-
179+
Token.Layout.Prompt: "bg:#eeeeff #000000 bold",
183180
# Make the status toolbar red.
184-
Token.Toolbar.Status: 'bg:#ff0000 #000000',
181+
Token.Toolbar.Status: "bg:#ff0000 #000000",
185182
}

examples/python-embed-with-custom-prompt.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
"""
55
from __future__ import unicode_literals
66

7-
from ptpython.repl import embed
8-
from ptpython.prompt_style import PromptStyle
97
from pygments.token import Token
108

9+
from ptpython.prompt_style import PromptStyle
10+
from ptpython.repl import embed
11+
1112

1213
def configure(repl):
1314
# There are several ways to override the prompt.
@@ -18,25 +19,23 @@ def configure(repl):
1819
class CustomPrompt(PromptStyle):
1920
def in_tokens(self, cli):
2021
return [
21-
(Token.In, 'Input['),
22-
(Token.In.Number, '%s' % repl.current_statement_index),
23-
(Token.In, '] >>: '),
22+
(Token.In, "Input["),
23+
(Token.In.Number, "%s" % repl.current_statement_index),
24+
(Token.In, "] >>: "),
2425
]
2526

2627
def in2_tokens(self, cli, width):
27-
return [
28-
(Token.In, '...: '.rjust(width)),
29-
]
28+
return [(Token.In, "...: ".rjust(width))]
3029

3130
def out_tokens(self, cli):
3231
return [
33-
(Token.Out, 'Result['),
34-
(Token.Out.Number, '%s' % repl.current_statement_index),
35-
(Token.Out, ']: '),
32+
(Token.Out, "Result["),
33+
(Token.Out.Number, "%s" % repl.current_statement_index),
34+
(Token.Out, "]: "),
3635
]
3736

38-
repl.all_prompt_styles['custom'] = CustomPrompt()
39-
repl.prompt_style = 'custom'
37+
repl.all_prompt_styles["custom"] = CustomPrompt()
38+
repl.prompt_style = "custom"
4039

4140
# 2. Assign a new callable to `get_input_prompt_tokens`. This will always take effect.
4241
## repl.get_input_prompt_tokens = lambda cli: [(Token.In, '[hello] >>> ')]
@@ -52,5 +51,5 @@ def main():
5251
embed(globals(), locals(), configure=configure)
5352

5453

55-
if __name__ == '__main__':
54+
if __name__ == "__main__":
5655
main()

examples/python-embed.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ def main():
1010
embed(globals(), locals(), vi_mode=False)
1111

1212

13-
if __name__ == '__main__':
13+
if __name__ == "__main__":
1414
main()

examples/python-input.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ def main():
1010
prompt = PythonInput()
1111

1212
text = prompt.app.run()
13-
print('You said: ' + text)
13+
print("You said: " + text)
1414

1515

16-
if __name__ == '__main__':
16+
if __name__ == "__main__":
1717
main()

ptpython/__main__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""
22
Make `python -m ptpython` an alias for running `./ptpython`.
33
"""
4-
from __future__ import unicode_literals
54
from .entry_points.run_ptpython import run
65

76
run()

0 commit comments

Comments
 (0)