cmd2 2.5.11__py3-none-any.whl → 2.6.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
cmd2/__init__.py CHANGED
@@ -1,26 +1,19 @@
1
- #
2
- # -*- coding: utf-8 -*-
3
- # flake8: noqa F401
4
- """This simply imports certain things for backwards compatibility."""
5
-
6
- import sys
1
+ """Import certain things for backwards compatibility."""
7
2
 
3
+ import argparse
4
+ import contextlib
8
5
  import importlib.metadata as importlib_metadata
6
+ import sys
9
7
 
10
- try:
8
+ with contextlib.suppress(importlib_metadata.PackageNotFoundError):
11
9
  __version__ = importlib_metadata.version(__name__)
12
- except importlib_metadata.PackageNotFoundError: # pragma: no cover
13
- # package is not installed
14
- pass
15
-
16
- from typing import List
17
10
 
18
11
  from .ansi import (
19
- Cursor,
20
12
  Bg,
21
- Fg,
13
+ Cursor,
22
14
  EightBitBg,
23
15
  EightBitFg,
16
+ Fg,
24
17
  RgbBg,
25
18
  RgbFg,
26
19
  TextStyle,
@@ -36,20 +29,18 @@ from .argparse_custom import (
36
29
 
37
30
  # Check if user has defined a module that sets a custom value for argparse_custom.DEFAULT_ARGUMENT_PARSER.
38
31
  # Do this before loading cmd2.Cmd class so its commands use the custom parser.
39
- import argparse
40
-
41
32
  cmd2_parser_module = getattr(argparse, 'cmd2_parser_module', None)
42
33
  if cmd2_parser_module is not None:
43
34
  import importlib
44
35
 
45
36
  importlib.import_module(cmd2_parser_module)
46
37
 
38
+ from . import plugin
47
39
  from .argparse_completer import set_default_ap_completer_type
48
-
49
40
  from .cmd2 import Cmd
50
41
  from .command_definition import CommandSet, with_default_category
51
42
  from .constants import COMMAND_NAME, DEFAULT_SHORTCUTS
52
- from .decorators import with_argument_list, with_argparser, with_category, as_subcommand_to
43
+ from .decorators import as_subcommand_to, with_argparser, with_argument_list, with_category
53
44
  from .exceptions import (
54
45
  Cmd2ArgparseError,
55
46
  CommandSetRegistrationError,
@@ -57,13 +48,11 @@ from .exceptions import (
57
48
  PassThroughException,
58
49
  SkipPostcommandHooks,
59
50
  )
60
- from . import plugin
61
51
  from .parsing import Statement
62
52
  from .py_bridge import CommandResult
63
- from .utils import categorize, CompletionMode, CustomCompletionSettings, Settable
64
-
53
+ from .utils import CompletionMode, CustomCompletionSettings, Settable, categorize
65
54
 
66
- __all__: List[str] = [
55
+ __all__: list[str] = [ # noqa: RUF022
67
56
  'COMMAND_NAME',
68
57
  'DEFAULT_SHORTCUTS',
69
58
  # ANSI Exports
cmd2/ansi.py CHANGED
@@ -1,7 +1,6 @@
1
- # coding=utf-8
2
- """
3
- Support for ANSI escape sequences which are used for things like applying style to text,
4
- setting the window title, and asynchronous alerts.
1
+ """Support for ANSI escape sequences.
2
+
3
+ These are used for things like applying style to text, setting the window title, and asynchronous alerts.
5
4
  """
6
5
 
7
6
  import functools
@@ -12,7 +11,6 @@ from enum import (
12
11
  from typing import (
13
12
  IO,
14
13
  Any,
15
- List,
16
14
  Optional,
17
15
  cast,
18
16
  )
@@ -31,18 +29,18 @@ BEL = '\a'
31
29
 
32
30
 
33
31
  class AllowStyle(Enum):
34
- """Values for ``cmd2.ansi.allow_style``"""
32
+ """Values for ``cmd2.ansi.allow_style``."""
35
33
 
36
34
  ALWAYS = 'Always' # Always output ANSI style sequences
37
35
  NEVER = 'Never' # Remove ANSI style sequences from all output
38
36
  TERMINAL = 'Terminal' # Remove ANSI style sequences if the output is not going to the terminal
39
37
 
40
38
  def __str__(self) -> str:
41
- """Return value instead of enum name for printing in cmd2's set command"""
39
+ """Return value instead of enum name for printing in cmd2's set command."""
42
40
  return str(self.value)
43
41
 
44
42
  def __repr__(self) -> str:
45
- """Return quoted value instead of enum description for printing in cmd2's set command"""
43
+ """Return quoted value instead of enum description for printing in cmd2's set command."""
46
44
  return repr(self.value)
47
45
 
48
46
 
@@ -85,8 +83,7 @@ RGB_BG_RE = re.compile(rf'{ESC}\[48;2(?:;(?:1?[0-9]?[0-9]?|2[0-4][0-9]|25[0-5]))
85
83
 
86
84
 
87
85
  def strip_style(text: str) -> str:
88
- """
89
- Strip ANSI style sequences from a string.
86
+ """Strip ANSI style sequences from a string.
90
87
 
91
88
  :param text: string which may contain ANSI style sequences
92
89
  :return: the same string with any ANSI style sequences removed
@@ -95,8 +92,8 @@ def strip_style(text: str) -> str:
95
92
 
96
93
 
97
94
  def style_aware_wcswidth(text: str) -> int:
98
- """
99
- Wrap wcswidth to make it compatible with strings that contain ANSI style sequences.
95
+ """Wrap wcswidth to make it compatible with strings that contain ANSI style sequences.
96
+
100
97
  This is intended for single line strings. If text contains a newline, this
101
98
  function will return -1. For multiline strings, call widest_line() instead.
102
99
 
@@ -110,9 +107,10 @@ def style_aware_wcswidth(text: str) -> int:
110
107
 
111
108
 
112
109
  def widest_line(text: str) -> int:
113
- """
114
- Return the width of the widest line in a multiline string. This wraps style_aware_wcswidth()
115
- so it handles ANSI style sequences and has the same restrictions on non-printable characters.
110
+ """Return the width of the widest line in a multiline string.
111
+
112
+ This wraps style_aware_wcswidth() so it handles ANSI style sequences and has the same
113
+ restrictions on non-printable characters.
116
114
 
117
115
  :param text: the string being measured
118
116
  :return: The width of the string when printed to the terminal if no errors occur.
@@ -130,8 +128,7 @@ def widest_line(text: str) -> int:
130
128
 
131
129
 
132
130
  def style_aware_write(fileobj: IO[str], msg: str) -> None:
133
- """
134
- Write a string to a fileobject and strip its ANSI style sequences if required by allow_style setting
131
+ """Write a string to a fileobject and strip its ANSI style sequences if required by allow_style setting.
135
132
 
136
133
  :param fileobj: the file object being written to
137
134
  :param msg: the string being written
@@ -145,8 +142,7 @@ def style_aware_write(fileobj: IO[str], msg: str) -> None:
145
142
  # Utility functions which create various ANSI sequences
146
143
  ####################################################################################
147
144
  def set_title(title: str) -> str:
148
- """
149
- Generate a string that, when printed, sets a terminal's window title.
145
+ """Generate a string that, when printed, sets a terminal's window title.
150
146
 
151
147
  :param title: new title for the window
152
148
  :return: the set title string
@@ -155,8 +151,7 @@ def set_title(title: str) -> str:
155
151
 
156
152
 
157
153
  def clear_screen(clear_type: int = 2) -> str:
158
- """
159
- Generate a string that, when printed, clears a terminal screen based on value of clear_type.
154
+ """Generate a string that, when printed, clears a terminal screen based on value of clear_type.
160
155
 
161
156
  :param clear_type: integer which specifies how to clear the screen (Defaults to 2)
162
157
  Possible values:
@@ -173,8 +168,7 @@ def clear_screen(clear_type: int = 2) -> str:
173
168
 
174
169
 
175
170
  def clear_line(clear_type: int = 2) -> str:
176
- """
177
- Generate a string that, when printed, clears a line based on value of clear_type.
171
+ """Generate a string that, when printed, clears a line based on value of clear_type.
178
172
 
179
173
  :param clear_type: integer which specifies how to clear the line (Defaults to 2)
180
174
  Possible values:
@@ -193,69 +187,65 @@ def clear_line(clear_type: int = 2) -> str:
193
187
  # Base classes which are not intended to be used directly
194
188
  ####################################################################################
195
189
  class AnsiSequence:
196
- """Base class to create ANSI sequence strings"""
190
+ """Base class to create ANSI sequence strings."""
197
191
 
198
192
  def __add__(self, other: Any) -> str:
199
- """
200
- Support building an ANSI sequence string when self is the left operand
193
+ """Support building an ANSI sequence string when self is the left operand.
194
+
201
195
  e.g. Fg.LIGHT_MAGENTA + "hello"
202
196
  """
203
197
  return str(self) + str(other)
204
198
 
205
199
  def __radd__(self, other: Any) -> str:
206
- """
207
- Support building an ANSI sequence string when self is the right operand
200
+ """Support building an ANSI sequence string when self is the right operand.
201
+
208
202
  e.g. "hello" + Fg.RESET
209
203
  """
210
204
  return str(other) + str(self)
211
205
 
212
206
 
213
207
  class FgColor(AnsiSequence):
214
- """Base class for ANSI Sequences which set foreground text color"""
215
-
216
- pass
208
+ """Base class for ANSI Sequences which set foreground text color."""
217
209
 
218
210
 
219
211
  class BgColor(AnsiSequence):
220
- """Base class for ANSI Sequences which set background text color"""
221
-
222
- pass
212
+ """Base class for ANSI Sequences which set background text color."""
223
213
 
224
214
 
225
215
  ####################################################################################
226
- # Implementations intended for direct use
216
+ # Implementations intended for direct use (do NOT use outside of cmd2)
227
217
  ####################################################################################
228
218
  class Cursor:
229
- """Create ANSI sequences to alter the cursor position"""
219
+ """Create ANSI sequences to alter the cursor position."""
230
220
 
231
221
  @staticmethod
232
- def UP(count: int = 1) -> str:
233
- """Move the cursor up a specified amount of lines (Defaults to 1)"""
222
+ def UP(count: int = 1) -> str: # noqa: N802
223
+ """Move the cursor up a specified amount of lines (Defaults to 1)."""
234
224
  return f"{CSI}{count}A"
235
225
 
236
226
  @staticmethod
237
- def DOWN(count: int = 1) -> str:
238
- """Move the cursor down a specified amount of lines (Defaults to 1)"""
227
+ def DOWN(count: int = 1) -> str: # noqa: N802
228
+ """Move the cursor down a specified amount of lines (Defaults to 1)."""
239
229
  return f"{CSI}{count}B"
240
230
 
241
231
  @staticmethod
242
- def FORWARD(count: int = 1) -> str:
243
- """Move the cursor forward a specified amount of lines (Defaults to 1)"""
232
+ def FORWARD(count: int = 1) -> str: # noqa: N802
233
+ """Move the cursor forward a specified amount of lines (Defaults to 1)."""
244
234
  return f"{CSI}{count}C"
245
235
 
246
236
  @staticmethod
247
- def BACK(count: int = 1) -> str:
248
- """Move the cursor back a specified amount of lines (Defaults to 1)"""
237
+ def BACK(count: int = 1) -> str: # noqa: N802
238
+ """Move the cursor back a specified amount of lines (Defaults to 1)."""
249
239
  return f"{CSI}{count}D"
250
240
 
251
241
  @staticmethod
252
- def SET_POS(x: int, y: int) -> str:
253
- """Set the cursor position to coordinates which are 1-based"""
242
+ def SET_POS(x: int, y: int) -> str: # noqa: N802
243
+ """Set the cursor position to coordinates which are 1-based."""
254
244
  return f"{CSI}{y};{x}H"
255
245
 
256
246
 
257
247
  class TextStyle(AnsiSequence, Enum):
258
- """Create text style ANSI sequences"""
248
+ """Create text style ANSI sequences."""
259
249
 
260
250
  # Resets all styles and colors of text
261
251
  RESET_ALL = 0
@@ -278,17 +268,17 @@ class TextStyle(AnsiSequence, Enum):
278
268
  UNDERLINE_DISABLE = 24
279
269
 
280
270
  def __str__(self) -> str:
281
- """
282
- Return ANSI text style sequence instead of enum name
271
+ """Return ANSI text style sequence instead of enum name.
272
+
283
273
  This is helpful when using a TextStyle in an f-string or format() call
284
- e.g. my_str = f"{TextStyle.UNDERLINE_ENABLE}hello{TextStyle.UNDERLINE_DISABLE}"
274
+ e.g. my_str = f"{TextStyle.UNDERLINE_ENABLE}hello{TextStyle.UNDERLINE_DISABLE}".
285
275
  """
286
276
  return f"{CSI}{self.value}m"
287
277
 
288
278
 
289
279
  class Fg(FgColor, Enum):
290
- """
291
- Create ANSI sequences for the 16 standard terminal foreground text colors.
280
+ """Create ANSI sequences for the 16 standard terminal foreground text colors.
281
+
292
282
  A terminal's color settings affect how these colors appear.
293
283
  To reset any foreground color, use Fg.RESET.
294
284
  """
@@ -313,17 +303,17 @@ class Fg(FgColor, Enum):
313
303
  RESET = 39
314
304
 
315
305
  def __str__(self) -> str:
316
- """
317
- Return ANSI color sequence instead of enum name
306
+ """Return ANSI color sequence instead of enum name.
307
+
318
308
  This is helpful when using an Fg in an f-string or format() call
319
- e.g. my_str = f"{Fg.BLUE}hello{Fg.RESET}"
309
+ e.g. my_str = f"{Fg.BLUE}hello{Fg.RESET}".
320
310
  """
321
311
  return f"{CSI}{self.value}m"
322
312
 
323
313
 
324
314
  class Bg(BgColor, Enum):
325
- """
326
- Create ANSI sequences for the 16 standard terminal background text colors.
315
+ """Create ANSI sequences for the 16 standard terminal background text colors.
316
+
327
317
  A terminal's color settings affect how these colors appear.
328
318
  To reset any background color, use Bg.RESET.
329
319
  """
@@ -348,17 +338,17 @@ class Bg(BgColor, Enum):
348
338
  RESET = 49
349
339
 
350
340
  def __str__(self) -> str:
351
- """
352
- Return ANSI color sequence instead of enum name
341
+ """Return ANSI color sequence instead of enum name.
342
+
353
343
  This is helpful when using a Bg in an f-string or format() call
354
- e.g. my_str = f"{Bg.BLACK}hello{Bg.RESET}"
344
+ e.g. my_str = f"{Bg.BLACK}hello{Bg.RESET}".
355
345
  """
356
346
  return f"{CSI}{self.value}m"
357
347
 
358
348
 
359
349
  class EightBitFg(FgColor, Enum):
360
- """
361
- Create ANSI sequences for 8-bit terminal foreground text colors. Most terminals support 8-bit/256-color mode.
350
+ """Create ANSI sequences for 8-bit terminal foreground text colors. Most terminals support 8-bit/256-color mode.
351
+
362
352
  The first 16 colors correspond to the 16 colors from Fg and behave the same way.
363
353
  To reset any foreground color, including 8-bit, use Fg.RESET.
364
354
  """
@@ -621,17 +611,17 @@ class EightBitFg(FgColor, Enum):
621
611
  GRAY_93 = 255
622
612
 
623
613
  def __str__(self) -> str:
624
- """
625
- Return ANSI color sequence instead of enum name
614
+ """Return ANSI color sequence instead of enum name.
615
+
626
616
  This is helpful when using an EightBitFg in an f-string or format() call
627
- e.g. my_str = f"{EightBitFg.SLATE_BLUE_1}hello{Fg.RESET}"
617
+ e.g. my_str = f"{EightBitFg.SLATE_BLUE_1}hello{Fg.RESET}".
628
618
  """
629
619
  return f"{CSI}38;5;{self.value}m"
630
620
 
631
621
 
632
622
  class EightBitBg(BgColor, Enum):
633
- """
634
- Create ANSI sequences for 8-bit terminal background text colors. Most terminals support 8-bit/256-color mode.
623
+ """Create ANSI sequences for 8-bit terminal background text colors. Most terminals support 8-bit/256-color mode.
624
+
635
625
  The first 16 colors correspond to the 16 colors from Bg and behave the same way.
636
626
  To reset any background color, including 8-bit, use Bg.RESET.
637
627
  """
@@ -894,23 +884,22 @@ class EightBitBg(BgColor, Enum):
894
884
  GRAY_93 = 255
895
885
 
896
886
  def __str__(self) -> str:
897
- """
898
- Return ANSI color sequence instead of enum name
887
+ """Return ANSI color sequence instead of enum name.
888
+
899
889
  This is helpful when using an EightBitBg in an f-string or format() call
900
- e.g. my_str = f"{EightBitBg.KHAKI_3}hello{Bg.RESET}"
890
+ e.g. my_str = f"{EightBitBg.KHAKI_3}hello{Bg.RESET}".
901
891
  """
902
892
  return f"{CSI}48;5;{self.value}m"
903
893
 
904
894
 
905
895
  class RgbFg(FgColor):
906
- """
907
- Create ANSI sequences for 24-bit (RGB) terminal foreground text colors. The terminal must support 24-bit/true-color mode.
896
+ """Create ANSI sequences for 24-bit (RGB) terminal foreground text colors. The terminal must support 24-bit/true-color.
897
+
908
898
  To reset any foreground color, including 24-bit, use Fg.RESET.
909
899
  """
910
900
 
911
901
  def __init__(self, r: int, g: int, b: int) -> None:
912
- """
913
- RgbFg initializer
902
+ """RgbFg initializer.
914
903
 
915
904
  :param r: integer from 0-255 for the red component of the color
916
905
  :param g: integer from 0-255 for the green component of the color
@@ -923,23 +912,22 @@ class RgbFg(FgColor):
923
912
  self._sequence = f"{CSI}38;2;{r};{g};{b}m"
924
913
 
925
914
  def __str__(self) -> str:
926
- """
927
- Return ANSI color sequence instead of enum name
915
+ """Return ANSI color sequence instead of enum name.
916
+
928
917
  This is helpful when using an RgbFg in an f-string or format() call
929
- e.g. my_str = f"{RgbFg(0, 55, 100)}hello{Fg.RESET}"
918
+ e.g. my_str = f"{RgbFg(0, 55, 100)}hello{Fg.RESET}".
930
919
  """
931
920
  return self._sequence
932
921
 
933
922
 
934
923
  class RgbBg(BgColor):
935
- """
936
- Create ANSI sequences for 24-bit (RGB) terminal background text colors. The terminal must support 24-bit/true-color mode.
924
+ """Create ANSI sequences for 24-bit (RGB) terminal background text colors. The terminal must support 24-bit/true-color.
925
+
937
926
  To reset any background color, including 24-bit, use Bg.RESET.
938
927
  """
939
928
 
940
929
  def __init__(self, r: int, g: int, b: int) -> None:
941
- """
942
- RgbBg initializer
930
+ """RgbBg initializer.
943
931
 
944
932
  :param r: integer from 0-255 for the red component of the color
945
933
  :param g: integer from 0-255 for the green component of the color
@@ -952,10 +940,10 @@ class RgbBg(BgColor):
952
940
  self._sequence = f"{CSI}48;2;{r};{g};{b}m"
953
941
 
954
942
  def __str__(self) -> str:
955
- """
956
- Return ANSI color sequence instead of enum name
943
+ """Return ANSI color sequence instead of enum name.
944
+
957
945
  This is helpful when using an RgbBg in an f-string or format() call
958
- e.g. my_str = f"{RgbBg(100, 255, 27)}hello{Bg.RESET}"
946
+ e.g. my_str = f"{RgbBg(100, 255, 27)}hello{Bg.RESET}".
959
947
  """
960
948
  return self._sequence
961
949
 
@@ -972,8 +960,8 @@ def style(
972
960
  strikethrough: Optional[bool] = None,
973
961
  underline: Optional[bool] = None,
974
962
  ) -> str:
975
- """
976
- Apply ANSI colors and/or styles to a string and return it.
963
+ """Apply ANSI colors and/or styles to a string and return it.
964
+
977
965
  The styling is self contained which means that at the end of the string reset code(s) are issued
978
966
  to undo whatever styling was done at the beginning.
979
967
 
@@ -992,11 +980,11 @@ def style(
992
980
  :raises TypeError: if bg isn't None or a subclass of BgColor
993
981
  :return: the stylized string
994
982
  """
995
- # List of strings that add style
996
- additions: List[AnsiSequence] = []
983
+ # list of strings that add style
984
+ additions: list[AnsiSequence] = []
997
985
 
998
- # List of strings that remove style
999
- removals: List[AnsiSequence] = []
986
+ # list of strings that remove style
987
+ removals: list[AnsiSequence] = []
1000
988
 
1001
989
  # Process the style settings
1002
990
  if fg is not None:
@@ -1062,7 +1050,6 @@ def async_alert_str(*, terminal_columns: int, prompt: str, line: str, cursor_off
1062
1050
  :param alert_msg: the message to display to the user
1063
1051
  :return: the correct string so that the alert message appears to the user to be printed above the current line.
1064
1052
  """
1065
-
1066
1053
  # Split the prompt lines since it can contain newline characters.
1067
1054
  prompt_lines = prompt.splitlines() or ['']
1068
1055