diff options
author | kotfu <kotfu@kotfu.net> | 2018-05-23 21:16:17 -0600 |
---|---|---|
committer | kotfu <kotfu@kotfu.net> | 2018-05-23 21:16:17 -0600 |
commit | 6780baa83457f7e99ba3a9c4f6a3c27701326ac5 (patch) | |
tree | c130136619c740645c45326a2ada0fcffa9f65a2 | |
parent | 1a70b90f375997751bc7df16b5e3f58c6194c71b (diff) | |
download | cmd2-git-6780baa83457f7e99ba3a9c4f6a3c27701326ac5.tar.gz |
Standardize cmd2 imports in tests and examples
-rwxr-xr-x | examples/alias_startup.py | 3 | ||||
-rwxr-xr-x | examples/arg_print.py | 11 | ||||
-rwxr-xr-x | examples/argparse_example.py | 10 | ||||
-rwxr-xr-x | examples/environment.py | 4 | ||||
-rwxr-xr-x | examples/event_loops.py | 2 | ||||
-rwxr-xr-x | examples/example.py | 8 | ||||
-rwxr-xr-x | examples/help_categories.py | 23 | ||||
-rwxr-xr-x | examples/paged_output.py | 5 | ||||
-rwxr-xr-x | examples/persistent_history.py | 2 | ||||
-rwxr-xr-x | examples/pirate.py | 7 | ||||
-rwxr-xr-x | examples/python_scripting.py | 2 | ||||
-rwxr-xr-x | examples/remove_unused.py | 2 | ||||
-rwxr-xr-x | examples/subcommands.py | 5 | ||||
-rwxr-xr-x | examples/tab_autocompletion.py | 31 | ||||
-rwxr-xr-x | examples/tab_completion.py | 7 | ||||
-rwxr-xr-x | examples/table_display.py | 2 | ||||
-rw-r--r-- | tests/conftest.py | 2 | ||||
-rw-r--r-- | tests/test_argparse.py | 2 | ||||
-rw-r--r-- | tests/test_cmd2.py | 2 | ||||
-rw-r--r-- | tests/test_parsing.py | 3 | ||||
-rw-r--r-- | tests/test_transcript.py | 2 |
21 files changed, 63 insertions, 72 deletions
diff --git a/examples/alias_startup.py b/examples/alias_startup.py index 7ccfa6e5..4a061e87 100755 --- a/examples/alias_startup.py +++ b/examples/alias_startup.py @@ -5,8 +5,7 @@ 2) How to load an initialization script at startup """ -from cmd2 import cmd2 - +import cmd2 class AliasAndStartup(cmd2.Cmd): """ Example cmd2 application where we create commands that just print the arguments they are called with.""" diff --git a/examples/arg_print.py b/examples/arg_print.py index b2f0fcda..1e6babc1 100755 --- a/examples/arg_print.py +++ b/examples/arg_print.py @@ -11,9 +11,7 @@ It also serves as an example of how to create command aliases (shortcuts). """ import argparse -from cmd2 import cmd2 -from cmd2.cmd2 import with_argument_list, with_argparser, with_argparser_and_unknown_args - +import cmd2 class ArgumentAndOptionPrinter(cmd2.Cmd): """ Example cmd2 application where we create commands that just print the arguments they are called with.""" @@ -31,7 +29,7 @@ class ArgumentAndOptionPrinter(cmd2.Cmd): """Print the argument string this basic command is called with.""" self.poutput('aprint was called with argument: {!r}'.format(arg)) - @with_argument_list + @cmd2.with_argument_list def do_lprint(self, arglist): """Print the argument list this basic command is called with.""" self.poutput('lprint was called with the following list of arguments: {!r}'.format(arglist)) @@ -42,7 +40,7 @@ class ArgumentAndOptionPrinter(cmd2.Cmd): oprint_parser.add_argument('-r', '--repeat', type=int, help='output [n] times') oprint_parser.add_argument('words', nargs='+', help='words to print') - @with_argparser(oprint_parser) + @cmd2.with_argparser(oprint_parser) def do_oprint(self, args): """Print the options and argument list this options command was called with.""" self.poutput('oprint was called with the following\n\toptions: {!r}'.format(args)) @@ -51,13 +49,12 @@ class ArgumentAndOptionPrinter(cmd2.Cmd): pprint_parser.add_argument('-p', '--piglatin', action='store_true', help='atinLay') pprint_parser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE') pprint_parser.add_argument('-r', '--repeat', type=int, help='output [n] times') - @with_argparser_and_unknown_args(pprint_parser) + @cmd2.with_argparser_and_unknown_args(pprint_parser) def do_pprint(self, args, unknown): """Print the options and argument list this options command was called with.""" self.poutput('oprint was called with the following\n\toptions: {!r}\n\targuments: {}'.format(args, unknown)) - if __name__ == '__main__': app = ArgumentAndOptionPrinter() app.cmdloop() diff --git a/examples/argparse_example.py b/examples/argparse_example.py index 6e5dcf35..236e2af4 100755 --- a/examples/argparse_example.py +++ b/examples/argparse_example.py @@ -14,10 +14,10 @@ verifying that the output produced matches the transcript. import argparse import sys -from cmd2.cmd2 import Cmd, with_argparser, with_argument_list +import cmd2 -class CmdLineApp(Cmd): +class CmdLineApp(cmd2.Cmd): """ Example cmd2 application. """ def __init__(self, ip_addr=None, port=None, transcript_files=None): self.multiline_commands = ['orate'] @@ -46,7 +46,7 @@ class CmdLineApp(Cmd): speak_parser.add_argument('-r', '--repeat', type=int, help='output [n] times') speak_parser.add_argument('words', nargs='+', help='words to say') - @with_argparser(speak_parser) + @cmd2.with_argparser(speak_parser) def do_speak(self, args): """Repeats what you tell me to.""" words = [] @@ -67,13 +67,13 @@ class CmdLineApp(Cmd): tag_parser.add_argument('tag', help='tag') tag_parser.add_argument('content', nargs='+', help='content to surround with tag') - @with_argparser(tag_parser) + @cmd2.with_argparser(tag_parser) def do_tag(self, args): """create a html tag""" self.poutput('<{0}>{1}</{0}>'.format(args.tag, ' '.join(args.content))) - @with_argument_list + @cmd2.with_argument_list def do_tagg(self, arglist): """verion of creating an html tag using arglist instead of argparser""" if len(arglist) >= 2: diff --git a/examples/environment.py b/examples/environment.py index af452e4e..c45ce71c 100755 --- a/examples/environment.py +++ b/examples/environment.py @@ -4,10 +4,10 @@ A sample application for cmd2 demonstrating customized environment parameters """ -from cmd2.cmd2 import Cmd +import cmd2 -class EnvironmentApp(Cmd): +class EnvironmentApp(cmd2.Cmd): """ Example cmd2 application. """ degrees_c = 22 diff --git a/examples/event_loops.py b/examples/event_loops.py index a76c5d91..53d3ca2b 100755 --- a/examples/event_loops.py +++ b/examples/event_loops.py @@ -6,7 +6,7 @@ This is an example of how to use cmd2 in a way so that cmd2 doesn't own the inne This opens up the possibility of registering cmd2 input with event loops, like asyncio, without occupying the main loop. """ -from cmd2 import cmd2 +import cmd2 class Cmd2EventBased(cmd2.Cmd): diff --git a/examples/example.py b/examples/example.py index f07b9c74..264abd84 100755 --- a/examples/example.py +++ b/examples/example.py @@ -14,10 +14,10 @@ the transcript. import random import argparse -from cmd2.cmd2 import Cmd, with_argparser +import cmd2 -class CmdLineApp(Cmd): +class CmdLineApp(cmd2.Cmd): """ Example cmd2 application. """ # Setting this true makes it run a shell command if a cmd2/cmd command doesn't exist @@ -43,7 +43,7 @@ class CmdLineApp(Cmd): speak_parser.add_argument('-r', '--repeat', type=int, help='output [n] times') speak_parser.add_argument('words', nargs='+', help='words to say') - @with_argparser(speak_parser) + @cmd2.with_argparser(speak_parser) def do_speak(self, args): """Repeats what you tell me to.""" words = [] @@ -65,7 +65,7 @@ class CmdLineApp(Cmd): mumble_parser.add_argument('-r', '--repeat', type=int, help='how many times to repeat') mumble_parser.add_argument('words', nargs='+', help='words to say') - @with_argparser(mumble_parser) + @cmd2.with_argparser(mumble_parser) def do_mumble(self, args): """Mumbles what you tell me to.""" repetitions = args.repeat or 1 diff --git a/examples/help_categories.py b/examples/help_categories.py index dcfbd31f..a8cf528c 100755 --- a/examples/help_categories.py +++ b/examples/help_categories.py @@ -6,10 +6,9 @@ A sample application for tagging categories on commands. import argparse -from cmd2.cmd2 import Cmd, categorize, __version__, with_argparser, with_category +import cmd2 - -class HelpCategories(Cmd): +class HelpCategories(cmd2.Cmd): """ Example cmd2 application. """ # Command categories @@ -28,7 +27,7 @@ class HelpCategories(Cmd): # Tag the above command functions under the category Connecting categorize(do_connect, CMD_CAT_CONNECTING) - @with_category(CMD_CAT_CONNECTING) + @cmd2.with_category(CMD_CAT_CONNECTING) def do_which(self, _): """Which command""" self.poutput('Which') @@ -58,8 +57,8 @@ class HelpCategories(Cmd): choices=['now', 'later', 'sometime', 'whenever'], help='Specify when to restart') - @with_argparser(restart_parser) - @with_category(CMD_CAT_APP_MGMT) + @cmd2.with_argparser(restart_parser) + @cmd2.with_category(CMD_CAT_APP_MGMT) def do_restart(self, _): """Restart command""" self.poutput('Restart') @@ -123,12 +122,12 @@ class HelpCategories(Cmd): self.poutput('VM Info') # Tag the above command functions under the category Server Information - categorize(do_resources, CMD_CAT_SERVER_INFO) - categorize(do_status, CMD_CAT_SERVER_INFO) - categorize(do_serverinfo, CMD_CAT_SERVER_INFO) - categorize(do_thread_dump, CMD_CAT_SERVER_INFO) - categorize(do_sslconnectorciphers, CMD_CAT_SERVER_INFO) - categorize(do_vminfo, CMD_CAT_SERVER_INFO) + cmd2.categorize(do_resources, CMD_CAT_SERVER_INFO) + cmd2.categorize(do_status, CMD_CAT_SERVER_INFO) + cmd2.categorize(do_serverinfo, CMD_CAT_SERVER_INFO) + cmd2.categorize(do_thread_dump, CMD_CAT_SERVER_INFO) + cmd2.categorize(do_sslconnectorciphers, CMD_CAT_SERVER_INFO) + cmd2.categorize(do_vminfo, CMD_CAT_SERVER_INFO) # The following command functions don't have the HELP_CATEGORY attribute set # and show up in the 'Other' group diff --git a/examples/paged_output.py b/examples/paged_output.py index 9396f04e..c56dcb89 100755 --- a/examples/paged_output.py +++ b/examples/paged_output.py @@ -3,8 +3,7 @@ """A simple example demonstrating the using paged output via the ppaged() method. """ -from cmd2 import cmd2 -from cmd2.cmd2 import with_argument_list +import cmd2 class PagedOutput(cmd2.Cmd): @@ -13,7 +12,7 @@ class PagedOutput(cmd2.Cmd): def __init__(self): super().__init__() - @with_argument_list + @cmd2.with_argument_list def do_page_file(self, args): """Read in a text file and display its output in a pager.""" if not args: diff --git a/examples/persistent_history.py b/examples/persistent_history.py index 251dbd67..61e26b9c 100755 --- a/examples/persistent_history.py +++ b/examples/persistent_history.py @@ -5,7 +5,7 @@ This will allow end users of your cmd2-based application to use the arrow keys and Ctrl+r in a manner which persists across invocations of your cmd2 application. This can make it much easier for them to use your application. """ -from cmd2 import cmd2 +import cmd2 class Cmd2PersistentHistory(cmd2.Cmd): diff --git a/examples/pirate.py b/examples/pirate.py index f6f4c629..9da634aa 100755 --- a/examples/pirate.py +++ b/examples/pirate.py @@ -7,10 +7,11 @@ presented as part of her PyCon 2010 talk. It demonstrates many features of cmd2. """ import argparse -from cmd2.cmd2 import Cmd, with_argparser +import cmd2 -class Pirate(Cmd): + +class Pirate(cmd2.Cmd): """A piratical example cmd2 application involving looting and drinking.""" def __init__(self): self.default_to_shell = True @@ -74,7 +75,7 @@ class Pirate(Cmd): yo_parser.add_argument('-c', '--commas', action='store_true', help='Intersperse commas') yo_parser.add_argument('beverage', help='beverage to drink with the chant') - @with_argparser(yo_parser) + @cmd.with_argparser(yo_parser) def do_yo(self, args): """Compose a yo-ho-ho type chant with flexible options.""" chant = ['yo'] + ['ho'] * args.ho diff --git a/examples/python_scripting.py b/examples/python_scripting.py index 865cf052..7e2cf345 100755 --- a/examples/python_scripting.py +++ b/examples/python_scripting.py @@ -17,7 +17,7 @@ This application and the "scripts/conditional.py" script serve as an example for import argparse import os -from cmd2 import cmd2 +import cmd2 class CmdLineApp(cmd2.Cmd): diff --git a/examples/remove_unused.py b/examples/remove_unused.py index dfe0a055..8a567123 100755 --- a/examples/remove_unused.py +++ b/examples/remove_unused.py @@ -9,7 +9,7 @@ name, they just won't clutter the help menu. Commands can also be removed entirely by using Python's "del". """ -from cmd2 import cmd2 +import cmd2 class RemoveUnusedBuiltinCommands(cmd2.Cmd): diff --git a/examples/subcommands.py b/examples/subcommands.py index 55be7711..356c2e09 100755 --- a/examples/subcommands.py +++ b/examples/subcommands.py @@ -52,8 +52,7 @@ except ImportError: # Intentionally below the bash completion code to reduce tab completion lag -from cmd2 import cmd2 -from cmd2.cmd2 import with_argparser +import cmd2 class SubcommandsExample(cmd2.Cmd): @@ -83,7 +82,7 @@ class SubcommandsExample(cmd2.Cmd): parser_bar.set_defaults(func=base_bar) parser_sport.set_defaults(func=base_sport) - @with_argparser(base_parser) + @cmd2.with_argparser(base_parser) def do_base(self, args): """Base command help""" func = getattr(args, 'func', None) diff --git a/examples/tab_autocompletion.py b/examples/tab_autocompletion.py index adfe9702..e2c5b3da 100755 --- a/examples/tab_autocompletion.py +++ b/examples/tab_autocompletion.py @@ -10,8 +10,7 @@ import argparse import itertools from typing import List -from cmd2 import cmd2, argparse_completer -from cmd2.cmd2 import with_argparser, with_category +import cmd2 actors = ['Mark Hamill', 'Harrison Ford', 'Carrie Fisher', 'Alec Guinness', 'Peter Mayhew', 'Anthony Daniels', 'Adam Driver', 'Daisy Ridley', 'John Boyega', 'Oscar Isaac', @@ -114,7 +113,7 @@ class TabCompleteExample(cmd2.Cmd): # - The help output for arguments with multiple flags or with append=True is more concise # - ACArgumentParser adds the ability to specify ranges of argument counts in 'nargs' - suggest_parser = argparse_completer.ACArgumentParser() + suggest_parser = cmd2.argparse_completer.ACArgumentParser() suggest_parser.add_argument('-t', '--type', choices=['movie', 'show'], required=True) suggest_parser.add_argument('-d', '--duration', nargs=(1, 2), action='append', @@ -122,8 +121,8 @@ class TabCompleteExample(cmd2.Cmd): '\tsingle value - maximum duration\n' '\t[a, b] - duration range') - @with_category(CAT_AUTOCOMPLETE) - @with_argparser(suggest_parser) + @cmd2.with_category(CAT_AUTOCOMPLETE) + @cmd2.with_argparser(suggest_parser) def do_suggest(self, args) -> None: """Suggest command demonstrates argparse customizations @@ -139,7 +138,7 @@ class TabCompleteExample(cmd2.Cmd): suggest_parser_hybrid = argparse.ArgumentParser() # This registers the custom narg range handling - argparse_completer.register_custom_actions(suggest_parser_hybrid) + cmd2.argparse_completer.register_custom_actions(suggest_parser_hybrid) suggest_parser_hybrid.add_argument('-t', '--type', choices=['movie', 'show'], required=True) suggest_parser_hybrid.add_argument('-d', '--duration', nargs=(1, 2), action='append', @@ -147,8 +146,8 @@ class TabCompleteExample(cmd2.Cmd): '\tsingle value - maximum duration\n' '\t[a, b] - duration range') - @with_category(CAT_AUTOCOMPLETE) - @with_argparser(suggest_parser_hybrid) + @cmd2.with_category(CAT_AUTOCOMPLETE) + @cmd2.with_argparser(suggest_parser_hybrid) def do_hybrid_suggest(self, args): if not args.type: self.do_help('orig_suggest') @@ -165,8 +164,8 @@ class TabCompleteExample(cmd2.Cmd): '\tsingle value - maximum duration\n' '\t[a, b] - duration range') - @with_argparser(suggest_parser_orig) - @with_category(CAT_AUTOCOMPLETE) + @cmd2.with_argparser(suggest_parser_orig) + @cmd2.with_category(CAT_AUTOCOMPLETE) def do_orig_suggest(self, args) -> None: if not args.type: self.do_help('orig_suggest') @@ -261,8 +260,8 @@ class TabCompleteExample(cmd2.Cmd): vid_shows_list_parser = vid_shows_commands_subparsers.add_parser('list') - @with_category(CAT_AUTOCOMPLETE) - @with_argparser(video_parser) + @cmd2.with_category(CAT_AUTOCOMPLETE) + @cmd2.with_argparser(video_parser) def do_video(self, args): """Video management command demonstrates multiple layers of subcommands being handled by AutoCompleter""" func = getattr(args, 'func', None) @@ -339,8 +338,8 @@ class TabCompleteExample(cmd2.Cmd): shows_list_parser = shows_commands_subparsers.add_parser('list') - @with_category(CAT_AUTOCOMPLETE) - @with_argparser(media_parser) + @cmd2.with_category(CAT_AUTOCOMPLETE) + @cmd2.with_argparser(media_parser) def do_media(self, args): """Media management command demonstrates multiple layers of subcommands being handled by AutoCompleter""" func = getattr(args, 'func', None) @@ -440,8 +439,8 @@ class TabCompleteExample(cmd2.Cmd): return self._filter_library(text, line, begidx, endidx, all_episodes, user_eps) return [] - @with_category(CAT_AUTOCOMPLETE) - @with_argparser(library_parser) + @cmd2.with_category(CAT_AUTOCOMPLETE) + @cmd2.with_argparser(library_parser) def do_library(self, args): """Media management command demonstrates multiple layers of subcommands being handled by AutoCompleter""" func = getattr(args, 'func', None) diff --git a/examples/tab_completion.py b/examples/tab_completion.py index 30fa283d..2ec7ff70 100755 --- a/examples/tab_completion.py +++ b/examples/tab_completion.py @@ -4,8 +4,7 @@ """ import argparse -from cmd2 import cmd2 -from cmd2.cmd2 import with_argparser, with_argument_list +import cmd2 # List of strings used with flag and index based completion functions food_item_strs = ['Pizza', 'Ham', 'Ham Sandwich', 'Potato'] @@ -24,7 +23,7 @@ class TabCompleteExample(cmd2.Cmd): add_item_group.add_argument('-s', '--sport', help='Adds sport item') add_item_group.add_argument('-o', '--other', help='Adds other item') - @with_argparser(add_item_parser) + @cmd2.with_argparser(add_item_parser) def do_add_item(self, args): """Add item command help""" if args.food: @@ -57,7 +56,7 @@ class TabCompleteExample(cmd2.Cmd): return self.flag_based_complete(text, line, begidx, endidx, flag_dict=flag_dict) - @with_argument_list + @cmd2.with_argument_list def do_list_item(self, args): """List item command help""" self.poutput("You listed {}".format(args)) diff --git a/examples/table_display.py b/examples/table_display.py index 5d168408..2e6ea804 100755 --- a/examples/table_display.py +++ b/examples/table_display.py @@ -12,7 +12,7 @@ WARNING: This example requires the tabulate module. """ import functools -from cmd2 import cmd2 +import cmd2 import tabulate # Format to use with tabulate module when displaying tables diff --git a/tests/conftest.py b/tests/conftest.py index 562ca4fa..90d45bd9 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -10,7 +10,7 @@ import sys from pytest import fixture from unittest import mock -from cmd2 import cmd2 +import cmd2 # Prefer statically linked gnureadline if available (for macOS compatibility due to issues with libedit) try: diff --git a/tests/test_argparse.py b/tests/test_argparse.py index f1a2b357..469cbe76 100644 --- a/tests/test_argparse.py +++ b/tests/test_argparse.py @@ -5,7 +5,7 @@ Cmd2 testing for argument parsing import argparse import pytest -from cmd2 import cmd2 +import cmd2 from unittest import mock from .conftest import run_cmd, StdOut diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py index c66b1264..3780bc5f 100644 --- a/tests/test_cmd2.py +++ b/tests/test_cmd2.py @@ -21,7 +21,7 @@ try: except ImportError: from unittest import mock -from cmd2 import cmd2 +import cmd2 from cmd2 import utils from .conftest import run_cmd, normalize, BASE_HELP, BASE_HELP_VERBOSE, \ HELP_HISTORY, SHORTCUTS_TXT, SHOW_TXT, SHOW_LONG, StdOut diff --git a/tests/test_parsing.py b/tests/test_parsing.py index 7b361b7e..59f9a610 100644 --- a/tests/test_parsing.py +++ b/tests/test_parsing.py @@ -7,9 +7,8 @@ Released under MIT license, see LICENSE file """ import pytest -from cmd2 import cmd2 +import cmd2 from cmd2.parsing import StatementParser - from cmd2 import utils @pytest.fixture diff --git a/tests/test_transcript.py b/tests/test_transcript.py index 99d4735e..302d80c8 100644 --- a/tests/test_transcript.py +++ b/tests/test_transcript.py @@ -15,7 +15,7 @@ import tempfile from unittest import mock import pytest -from cmd2 import cmd2 +import cmd2 from .conftest import run_cmd, StdOut from cmd2 import transcript |