summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorDaniel Baumann <mail@daniel-baumann.ch>2023-12-17 10:46:44 +0000
committerDaniel Baumann <mail@daniel-baumann.ch>2023-12-17 10:46:44 +0000
commit70b47719c91522ab53abc763eb6da3b62c1a00c9 (patch)
treedb7906833e1af835fb2addf0c220f00904969cd7 /examples
parentReleasing debian version 3.0.23-3. (diff)
downloadptpython-70b47719c91522ab53abc763eb6da3b62c1a00c9.tar.xz
ptpython-70b47719c91522ab53abc763eb6da3b62c1a00c9.zip
Merging upstream version 3.0.25.
Signed-off-by: Daniel Baumann <mail@daniel-baumann.ch>
Diffstat (limited to 'examples')
-rwxr-xr-xexamples/asyncio-python-embed.py15
-rwxr-xr-xexamples/asyncio-ssh-python-embed.py18
-rw-r--r--examples/ptpython_config/config.py3
-rwxr-xr-xexamples/python-embed-with-custom-prompt.py12
-rwxr-xr-xexamples/python-embed.py2
-rwxr-xr-xexamples/ssh-and-telnet-embed.py11
6 files changed, 29 insertions, 32 deletions
diff --git a/examples/asyncio-python-embed.py b/examples/asyncio-python-embed.py
index 05f52f1..a8fbba5 100755
--- a/examples/asyncio-python-embed.py
+++ b/examples/asyncio-python-embed.py
@@ -19,7 +19,7 @@ loop = asyncio.get_event_loop()
counter = [0]
-async def print_counter():
+async def print_counter() -> None:
"""
Coroutine that prints counters and saves it in a global variable.
"""
@@ -29,7 +29,7 @@ async def print_counter():
await asyncio.sleep(3)
-async def interactive_shell():
+async def interactive_shell() -> None:
"""
Coroutine that starts a Python REPL from which we can access the global
counter variable.
@@ -44,13 +44,10 @@ async def interactive_shell():
loop.stop()
-def main():
- asyncio.ensure_future(print_counter())
- asyncio.ensure_future(interactive_shell())
-
- loop.run_forever()
- loop.close()
+async def main() -> None:
+ asyncio.create_task(print_counter())
+ await interactive_shell()
if __name__ == "__main__":
- main()
+ asyncio.run(main())
diff --git a/examples/asyncio-ssh-python-embed.py b/examples/asyncio-ssh-python-embed.py
index 86b5607..be0689e 100755
--- a/examples/asyncio-ssh-python-embed.py
+++ b/examples/asyncio-ssh-python-embed.py
@@ -32,31 +32,25 @@ class MySSHServer(asyncssh.SSHServer):
return ReplSSHServerSession(self.get_namespace)
-def main(port=8222):
+async def main(port: int = 8222) -> None:
"""
Example that starts the REPL through an SSH server.
"""
- loop = asyncio.get_event_loop()
-
# Namespace exposed in the REPL.
environ = {"hello": "world"}
# Start SSH server.
- def create_server():
+ def create_server() -> MySSHServer:
return MySSHServer(lambda: environ)
print("Listening on :%i" % port)
print('To connect, do "ssh localhost -p %i"' % port)
- loop.run_until_complete(
- asyncssh.create_server(
- create_server, "", port, server_host_keys=["/etc/ssh/ssh_host_dsa_key"]
- )
+ await asyncssh.create_server(
+ create_server, "", port, server_host_keys=["/etc/ssh/ssh_host_dsa_key"]
)
-
- # Run eventloop.
- loop.run_forever()
+ await asyncio.Future() # Wait forever.
if __name__ == "__main__":
- main()
+ asyncio.run(main())
diff --git a/examples/ptpython_config/config.py b/examples/ptpython_config/config.py
index 2f3f49d..b25850a 100644
--- a/examples/ptpython_config/config.py
+++ b/examples/ptpython_config/config.py
@@ -70,6 +70,9 @@ def configure(repl):
# Vi mode.
repl.vi_mode = False
+ # Enable the modal cursor (when using Vi mode). Other options are 'Block', 'Underline', 'Beam', 'Blink under', 'Blink block', and 'Blink beam'
+ repl.cursor_shape_config = "Modal (vi)"
+
# Paste mode. (When True, don't insert whitespace after new line.)
repl.paste_mode = False
diff --git a/examples/python-embed-with-custom-prompt.py b/examples/python-embed-with-custom-prompt.py
index 968aedc..d54da1d 100755
--- a/examples/python-embed-with-custom-prompt.py
+++ b/examples/python-embed-with-custom-prompt.py
@@ -2,26 +2,26 @@
"""
Example of embedding a Python REPL, and setting a custom prompt.
"""
-from prompt_toolkit.formatted_text import HTML
+from prompt_toolkit.formatted_text import HTML, AnyFormattedText
from ptpython.prompt_style import PromptStyle
from ptpython.repl import embed
-def configure(repl):
+def configure(repl) -> None:
# Probably, the best is to add a new PromptStyle to `all_prompt_styles` and
# activate it. This way, the other styles are still selectable from the
# menu.
class CustomPrompt(PromptStyle):
- def in_prompt(self):
+ def in_prompt(self) -> AnyFormattedText:
return HTML("<ansigreen>Input[%s]</ansigreen>: ") % (
repl.current_statement_index,
)
- def in2_prompt(self, width):
+ def in2_prompt(self, width: int) -> AnyFormattedText:
return "...: ".rjust(width)
- def out_prompt(self):
+ def out_prompt(self) -> AnyFormattedText:
return HTML("<ansired>Result[%s]</ansired>: ") % (
repl.current_statement_index,
)
@@ -30,7 +30,7 @@ def configure(repl):
repl.prompt_style = "custom"
-def main():
+def main() -> None:
embed(globals(), locals(), configure=configure)
diff --git a/examples/python-embed.py b/examples/python-embed.py
index ac2cd06..49224ac 100755
--- a/examples/python-embed.py
+++ b/examples/python-embed.py
@@ -4,7 +4,7 @@
from ptpython.repl import embed
-def main():
+def main() -> None:
embed(globals(), locals(), vi_mode=False)
diff --git a/examples/ssh-and-telnet-embed.py b/examples/ssh-and-telnet-embed.py
index 378784c..62fa76d 100755
--- a/examples/ssh-and-telnet-embed.py
+++ b/examples/ssh-and-telnet-embed.py
@@ -11,13 +11,16 @@ import pathlib
import asyncssh
from prompt_toolkit import print_formatted_text
-from prompt_toolkit.contrib.ssh.server import PromptToolkitSSHServer
+from prompt_toolkit.contrib.ssh.server import (
+ PromptToolkitSSHServer,
+ PromptToolkitSSHSession,
+)
from prompt_toolkit.contrib.telnet.server import TelnetServer
from ptpython.repl import embed
-def ensure_key(filename="ssh_host_key"):
+def ensure_key(filename: str = "ssh_host_key") -> str:
path = pathlib.Path(filename)
if not path.exists():
rsa_key = asyncssh.generate_private_key("ssh-rsa")
@@ -25,12 +28,12 @@ def ensure_key(filename="ssh_host_key"):
return str(path)
-async def interact(connection=None):
+async def interact(connection: PromptToolkitSSHSession) -> None:
global_dict = {**globals(), "print": print_formatted_text}
await embed(return_asyncio_coroutine=True, globals=global_dict)
-async def main(ssh_port=8022, telnet_port=8023):
+async def main(ssh_port: int = 8022, telnet_port: int = 8023) -> None:
ssh_server = PromptToolkitSSHServer(interact=interact)
await asyncssh.create_server(
lambda: ssh_server, "", ssh_port, server_host_keys=[ensure_key()]