From e018bbd79b2ddc0cf2538dff84baa8b2bf460ccd Mon Sep 17 00:00:00 2001 From: Eric Lin Date: Wed, 25 Apr 2018 11:40:33 -0400 Subject: Removed the expensive imports from cmd2/__init__.py Added some shared definitions to cmd2/__init__.py -> maybe there's a better place for these? Figured out how to trick bash into showing argument hints. It's a bit weird. Updated all of the tests and examples to import cmd2 resources from their new location without the automatic imports in cmd2/__init__.py For #369 --- cmd2/__init__.py | 4 +-- cmd2/argcomplete_bridge.py | 42 ++++++++++++++++++++++--- examples/alias_startup.py | 4 +-- examples/arg_print.py | 6 ++-- examples/argparse_example.py | 2 +- examples/environment.py | 2 +- examples/event_loops.py | 2 +- examples/example.py | 2 +- examples/help_categories.py | 2 +- examples/paged_output.py | 4 +-- examples/persistent_history.py | 2 +- examples/pirate.py | 2 +- examples/python_scripting.py | 2 +- examples/remove_unused.py | 2 +- examples/subcommands.py | 71 +++++++++++++++++++++++++----------------- examples/submenus.py | 2 +- examples/tab_autocompletion.py | 4 +-- examples/tab_completion.py | 4 +-- examples/table_display.py | 2 +- tests/conftest.py | 2 +- tests/test_argparse.py | 2 +- tests/test_cmd2.py | 20 ++++++------ tests/test_completion.py | 2 +- tests/test_parsing.py | 18 +++++------ tests/test_submenu.py | 2 +- tests/test_transcript.py | 8 ++--- 26 files changed, 128 insertions(+), 87 deletions(-) diff --git a/cmd2/__init__.py b/cmd2/__init__.py index 773eba37..cea82e57 100644 --- a/cmd2/__init__.py +++ b/cmd2/__init__.py @@ -1,8 +1,8 @@ # # -*- coding: utf-8 -*- # -from .cmd2 import __version__, Cmd, set_posix_shlex, set_strip_quotes, AddSubmenu, CmdResult, categorize -from .cmd2 import with_argument_list, with_argparser, with_argparser_and_unknown_args, with_category +# from .cmd2 import __version__, Cmd, set_posix_shlex, set_strip_quotes, AddSubmenu, CmdResult, categorize +# from .cmd2 import with_argument_list, with_argparser, with_argparser_and_unknown_args, with_category # Used for tab completion and word breaks. Do not change. QUOTES = ['"', "'"] diff --git a/cmd2/argcomplete_bridge.py b/cmd2/argcomplete_bridge.py index 93b3cbbb..f1ed910e 100644 --- a/cmd2/argcomplete_bridge.py +++ b/cmd2/argcomplete_bridge.py @@ -2,7 +2,9 @@ """Hijack the ArgComplete's bash completion handler to return AutoCompleter results""" import argcomplete +from contextlib import redirect_stdout import copy +from io import StringIO import os import shlex import sys @@ -162,7 +164,13 @@ class CompletionFinder(argcomplete.CompletionFinder): comp_point = int(os.environ["COMP_POINT"]) comp_line = argcomplete.ensure_str(comp_line) - ### SWAPPED FOR AUTOCOMPLETER + + ############################## + # SWAPPED FOR AUTOCOMPLETER + # + # Replaced with our own tokenizer function + ############################## + # cword_prequote, cword_prefix, cword_suffix, comp_words, last_wordbreak_pos = split_line(comp_line, comp_point) tokens, _, begidx, endidx = tokens_for_completion(comp_line, comp_point) @@ -172,7 +180,11 @@ class CompletionFinder(argcomplete.CompletionFinder): # 2: python