Check for readline module instead of Windows when initializing autocomplete in install (#1104)

This commit is contained in:
Micah Jerome Ellison 2020-12-04 15:13:38 -08:00 committed by GitHub
parent bade28a0b0
commit 48cde1b473
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 9 deletions

View file

@ -12,14 +12,9 @@ from . import __version__
from .config import load_config from .config import load_config
from .config import verify_config_colors from .config import verify_config_colors
from .exception import UserAbort from .exception import UserAbort
from .os_compat import on_windows
from .prompt import yesno from .prompt import yesno
from .upgrade import is_old_version from .upgrade import is_old_version
if not on_windows:
# readline is not included in Windows Active Python
import readline
DEFAULT_CONFIG_NAME = "jrnl.yaml" DEFAULT_CONFIG_NAME = "jrnl.yaml"
DEFAULT_JOURNAL_NAME = "journal.txt" DEFAULT_JOURNAL_NAME = "journal.txt"
DEFAULT_JOURNAL_KEY = "default" DEFAULT_JOURNAL_KEY = "default"
@ -127,10 +122,7 @@ def load_or_install_jrnl():
def install(): def install():
if not on_windows: _initialize_autocomplete()
readline.set_completer_delims(" \t\n;")
readline.parse_and_bind("tab: complete")
readline.set_completer(_autocomplete_path)
# Where to create the journal? # Where to create the journal?
path_query = f"Path to your journal file (leave blank for {JOURNAL_FILE_PATH}): " path_query = f"Path to your journal file (leave blank for {JOURNAL_FILE_PATH}): "
@ -159,6 +151,16 @@ def install():
return default_config return default_config
def _initialize_autocomplete():
# readline is not included in Windows Active Python and perhaps some other distributions
if sys.modules.get("readline"):
import readline
readline.set_completer_delims(" \t\n;")
readline.parse_and_bind("tab: complete")
readline.set_completer(_autocomplete_path)
def _autocomplete_path(text, state): def _autocomplete_path(text, state):
expansions = glob.glob(os.path.expanduser(os.path.expandvars(text)) + "*") expansions = glob.glob(os.path.expanduser(os.path.expandvars(text)) + "*")
expansions = [e + "/" if os.path.isdir(e) else e for e in expansions] expansions = [e + "/" if os.path.isdir(e) else e for e in expansions]

13
tests/test_install.py Normal file
View file

@ -0,0 +1,13 @@
from unittest import mock
import pytest
import sys
@pytest.mark.filterwarnings(
"ignore:.*imp module is deprecated.*"
) # ansiwrap spits out an unrelated warning
def test_initialize_autocomplete_runs_without_readline():
from jrnl import install
with mock.patch.dict(sys.modules, {"readline": None}):
install._initialize_autocomplete() # should not throw exception