Skip to content

Commit

Permalink
rename has_terminal to can_prompt
Browse files Browse the repository at this point in the history
This commit renames `has_terminal` to `can_prompt`
and ensures that the code will run in Windows.
On Windows it currently always returns `True`.
  • Loading branch information
christian-monch committed Apr 12, 2024
1 parent 2810073 commit 6054947
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
6 changes: 3 additions & 3 deletions datalad/ui/dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from ..utils import auto_repr
from ..utils import on_windows
from .base import InteractiveUI
from .utils import has_terminal
from .utils import can_prompt
from datalad.support.exceptions import CapturedException

# Example APIs which might be useful to look for "inspiration"
Expand Down Expand Up @@ -359,7 +359,7 @@ def get_progressbar(self, *args, **kwargs):
*args, **kwargs)

def input(self, prompt, hidden=False):
if not has_terminal():
if not can_prompt():
# we are not interactive
raise RuntimeError('Interactive input not available for `ui.input()` in annex remotes')
return super(UnderAnnexUI, self).input(prompt, hidden)
Expand All @@ -371,7 +371,7 @@ def question(self,
default=None,
hidden=False,
repeat=None):
if not has_terminal():
if not can_prompt():
# we are not interactive
raise RuntimeError('Interactive input not available for `ui.question()` in annex remotes')
return super(UnderAnnexUI, self).question(
Expand Down
18 changes: 11 additions & 7 deletions datalad/ui/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,17 @@ def show_hint(msg):
ansi_colors.YELLOW)))


def has_terminal():
"""Return True if the process has a controlling terminal
def can_prompt() -> bool:
"""Return True if the process can prompt for credentials
This checks for a controlling terminal on Linux
On Linux this method checks for a controlling terminal.
On Windows it always returns True.
"""
try:
open('/dev/tty', 'r').close()
if on_windows:
return True
except (IOError, OSError):
return False
else:
try:
open('/dev/tty', 'r').close()
return True
except (IOError, OSError):
return False

0 comments on commit 6054947

Please sign in to comment.