Skip to content

Commit

Permalink
Support old and new prompt_toolkit versions
Browse files Browse the repository at this point in the history
  • Loading branch information
op3 committed Mar 23, 2024
1 parent dd28ddd commit 80b9d57
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ To build and run HDTV, the following dependencies are required:

* Python
- Tested with 3.8, 3.9, 3.10, and 3.11
- Packages: numpy scipy matplotlib prompt_toolkit>=3.0.40 uncertainties
- Packages: numpy scipy matplotlib prompt_toolkit>=3.0.14 uncertainties
- Packages for development & testing: docutils pytest pytest-cov
* [Cern ROOT](https://root.cern/) 6
- Tested with version 6.26 and higher
Expand Down
21 changes: 18 additions & 3 deletions hdtv/cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from enum import Enum, auto
from pwd import getpwuid

import prompt_toolkit
from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
from prompt_toolkit.completion import Completer, Completion
from prompt_toolkit.enums import EditingMode
Expand Down Expand Up @@ -419,6 +420,12 @@ def __init__(self, command_tree):
self.loop = asyncio.new_event_loop()
asyncio.set_event_loop(self.loop)

# prompt_toolkit < 3.0.40 brings its own event loop
ptk_version = prompt_toolkit.__version__
self.fallback_eventloop = (
ptk_version.startswith("3.0.") and int(ptk_version[4:].split("-")[0]) < 40
)

self.command_tree = command_tree

self.history = None
Expand All @@ -434,6 +441,9 @@ def __init__(self, command_tree):
self.exit_handlers = []

def StartEventLoop(self):
if self.fallback_eventloop:
return

def _loop(loop):
asyncio.set_event_loop(loop)
loop.run_forever()
Expand Down Expand Up @@ -517,12 +527,16 @@ def Exit(self, args=None):

def AsyncExit(self):
"""Asynchronous exit; to be called from another thread"""
self.Exit()
if self.fallback_eventloop:
self.loop.call_soon_threadsafe(self.Exit)
else:
self.Exit()

self.loop.call_soon_threadsafe(
lambda: self.session.app.exit(style="class:exiting")
)
self.thread.join()
if not self.fallback_eventloop:
self.thread.join()

def EOFHandler(self):
self.Exit()
Expand Down Expand Up @@ -747,7 +761,8 @@ def set_vi_mode(vi_mode):
if line:
self.DoLine(line)

self.loop.call_soon_threadsafe(lambda: self.loop.stop())
if not self.fallback_eventloop:
self.loop.call_soon_threadsafe(lambda: self.loop.stop())


class HDTVCompleter(Completer):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"matplotlib",
"numpy",
"ipython",
"prompt_toolkit>=3.0.40",
"prompt_toolkit>=3.0.14",
"traitlets",
"uncertainties",
],
Expand Down

0 comments on commit 80b9d57

Please sign in to comment.