Skip to content

Commit 0d48428

Browse files
committed
Apply automated docstring refactoring
1 parent fd2603e commit 0d48428

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+361
-744
lines changed

cmd2/ansi.py

Lines changed: 26 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
"""
2-
Support for ANSI escape sequences which are used for things like applying style to text,
1+
"""Support for ANSI escape sequences which are used for things like applying style to text,
32
setting the window title, and asynchronous alerts.
43
"""
54

@@ -83,8 +82,7 @@ def __repr__(self) -> str:
8382

8483

8584
def strip_style(text: str) -> str:
86-
"""
87-
Strip ANSI style sequences from a string.
85+
"""Strip ANSI style sequences from a string.
8886
8987
:param text: string which may contain ANSI style sequences
9088
:return: the same string with any ANSI style sequences removed
@@ -93,8 +91,7 @@ def strip_style(text: str) -> str:
9391

9492

9593
def style_aware_wcswidth(text: str) -> int:
96-
"""
97-
Wrap wcswidth to make it compatible with strings that contain ANSI style sequences.
94+
"""Wrap wcswidth to make it compatible with strings that contain ANSI style sequences.
9895
This is intended for single line strings. If text contains a newline, this
9996
function will return -1. For multiline strings, call widest_line() instead.
10097
@@ -108,8 +105,7 @@ def style_aware_wcswidth(text: str) -> int:
108105

109106

110107
def widest_line(text: str) -> int:
111-
"""
112-
Return the width of the widest line in a multiline string. This wraps style_aware_wcswidth()
108+
"""Return the width of the widest line in a multiline string. This wraps style_aware_wcswidth()
113109
so it handles ANSI style sequences and has the same restrictions on non-printable characters.
114110
115111
:param text: the string being measured
@@ -128,8 +124,7 @@ def widest_line(text: str) -> int:
128124

129125

130126
def style_aware_write(fileobj: IO[str], msg: str) -> None:
131-
"""
132-
Write a string to a fileobject and strip its ANSI style sequences if required by allow_style setting
127+
"""Write a string to a fileobject and strip its ANSI style sequences if required by allow_style setting
133128
134129
:param fileobj: the file object being written to
135130
:param msg: the string being written
@@ -143,8 +138,7 @@ def style_aware_write(fileobj: IO[str], msg: str) -> None:
143138
# Utility functions which create various ANSI sequences
144139
####################################################################################
145140
def set_title(title: str) -> str:
146-
"""
147-
Generate a string that, when printed, sets a terminal's window title.
141+
"""Generate a string that, when printed, sets a terminal's window title.
148142
149143
:param title: new title for the window
150144
:return: the set title string
@@ -153,8 +147,7 @@ def set_title(title: str) -> str:
153147

154148

155149
def clear_screen(clear_type: int = 2) -> str:
156-
"""
157-
Generate a string that, when printed, clears a terminal screen based on value of clear_type.
150+
"""Generate a string that, when printed, clears a terminal screen based on value of clear_type.
158151
159152
:param clear_type: integer which specifies how to clear the screen (Defaults to 2)
160153
Possible values:
@@ -171,8 +164,7 @@ def clear_screen(clear_type: int = 2) -> str:
171164

172165

173166
def clear_line(clear_type: int = 2) -> str:
174-
"""
175-
Generate a string that, when printed, clears a line based on value of clear_type.
167+
"""Generate a string that, when printed, clears a line based on value of clear_type.
176168
177169
:param clear_type: integer which specifies how to clear the line (Defaults to 2)
178170
Possible values:
@@ -194,15 +186,13 @@ class AnsiSequence:
194186
"""Base class to create ANSI sequence strings"""
195187

196188
def __add__(self, other: Any) -> str:
197-
"""
198-
Support building an ANSI sequence string when self is the left operand
189+
"""Support building an ANSI sequence string when self is the left operand
199190
e.g. Fg.LIGHT_MAGENTA + "hello"
200191
"""
201192
return str(self) + str(other)
202193

203194
def __radd__(self, other: Any) -> str:
204-
"""
205-
Support building an ANSI sequence string when self is the right operand
195+
"""Support building an ANSI sequence string when self is the right operand
206196
e.g. "hello" + Fg.RESET
207197
"""
208198
return str(other) + str(self)
@@ -272,17 +262,15 @@ class TextStyle(AnsiSequence, Enum):
272262
UNDERLINE_DISABLE = 24
273263

274264
def __str__(self) -> str:
275-
"""
276-
Return ANSI text style sequence instead of enum name
265+
"""Return ANSI text style sequence instead of enum name
277266
This is helpful when using a TextStyle in an f-string or format() call
278267
e.g. my_str = f"{TextStyle.UNDERLINE_ENABLE}hello{TextStyle.UNDERLINE_DISABLE}"
279268
"""
280269
return f"{CSI}{self.value}m"
281270

282271

283272
class Fg(FgColor, Enum):
284-
"""
285-
Create ANSI sequences for the 16 standard terminal foreground text colors.
273+
"""Create ANSI sequences for the 16 standard terminal foreground text colors.
286274
A terminal's color settings affect how these colors appear.
287275
To reset any foreground color, use Fg.RESET.
288276
"""
@@ -307,17 +295,15 @@ class Fg(FgColor, Enum):
307295
RESET = 39
308296

309297
def __str__(self) -> str:
310-
"""
311-
Return ANSI color sequence instead of enum name
298+
"""Return ANSI color sequence instead of enum name
312299
This is helpful when using an Fg in an f-string or format() call
313300
e.g. my_str = f"{Fg.BLUE}hello{Fg.RESET}"
314301
"""
315302
return f"{CSI}{self.value}m"
316303

317304

318305
class Bg(BgColor, Enum):
319-
"""
320-
Create ANSI sequences for the 16 standard terminal background text colors.
306+
"""Create ANSI sequences for the 16 standard terminal background text colors.
321307
A terminal's color settings affect how these colors appear.
322308
To reset any background color, use Bg.RESET.
323309
"""
@@ -342,17 +328,15 @@ class Bg(BgColor, Enum):
342328
RESET = 49
343329

344330
def __str__(self) -> str:
345-
"""
346-
Return ANSI color sequence instead of enum name
331+
"""Return ANSI color sequence instead of enum name
347332
This is helpful when using a Bg in an f-string or format() call
348333
e.g. my_str = f"{Bg.BLACK}hello{Bg.RESET}"
349334
"""
350335
return f"{CSI}{self.value}m"
351336

352337

353338
class EightBitFg(FgColor, Enum):
354-
"""
355-
Create ANSI sequences for 8-bit terminal foreground text colors. Most terminals support 8-bit/256-color mode.
339+
"""Create ANSI sequences for 8-bit terminal foreground text colors. Most terminals support 8-bit/256-color mode.
356340
The first 16 colors correspond to the 16 colors from Fg and behave the same way.
357341
To reset any foreground color, including 8-bit, use Fg.RESET.
358342
"""
@@ -615,17 +599,15 @@ class EightBitFg(FgColor, Enum):
615599
GRAY_93 = 255
616600

617601
def __str__(self) -> str:
618-
"""
619-
Return ANSI color sequence instead of enum name
602+
"""Return ANSI color sequence instead of enum name
620603
This is helpful when using an EightBitFg in an f-string or format() call
621604
e.g. my_str = f"{EightBitFg.SLATE_BLUE_1}hello{Fg.RESET}"
622605
"""
623606
return f"{CSI}38;5;{self.value}m"
624607

625608

626609
class EightBitBg(BgColor, Enum):
627-
"""
628-
Create ANSI sequences for 8-bit terminal background text colors. Most terminals support 8-bit/256-color mode.
610+
"""Create ANSI sequences for 8-bit terminal background text colors. Most terminals support 8-bit/256-color mode.
629611
The first 16 colors correspond to the 16 colors from Bg and behave the same way.
630612
To reset any background color, including 8-bit, use Bg.RESET.
631613
"""
@@ -888,23 +870,20 @@ class EightBitBg(BgColor, Enum):
888870
GRAY_93 = 255
889871

890872
def __str__(self) -> str:
891-
"""
892-
Return ANSI color sequence instead of enum name
873+
"""Return ANSI color sequence instead of enum name
893874
This is helpful when using an EightBitBg in an f-string or format() call
894875
e.g. my_str = f"{EightBitBg.KHAKI_3}hello{Bg.RESET}"
895876
"""
896877
return f"{CSI}48;5;{self.value}m"
897878

898879

899880
class RgbFg(FgColor):
900-
"""
901-
Create ANSI sequences for 24-bit (RGB) terminal foreground text colors. The terminal must support 24-bit/true-color mode.
881+
"""Create ANSI sequences for 24-bit (RGB) terminal foreground text colors. The terminal must support 24-bit/true-color.
902882
To reset any foreground color, including 24-bit, use Fg.RESET.
903883
"""
904884

905885
def __init__(self, r: int, g: int, b: int) -> None:
906-
"""
907-
RgbFg initializer
886+
"""RgbFg initializer
908887
909888
:param r: integer from 0-255 for the red component of the color
910889
:param g: integer from 0-255 for the green component of the color
@@ -917,23 +896,20 @@ def __init__(self, r: int, g: int, b: int) -> None:
917896
self._sequence = f"{CSI}38;2;{r};{g};{b}m"
918897

919898
def __str__(self) -> str:
920-
"""
921-
Return ANSI color sequence instead of enum name
899+
"""Return ANSI color sequence instead of enum name
922900
This is helpful when using an RgbFg in an f-string or format() call
923901
e.g. my_str = f"{RgbFg(0, 55, 100)}hello{Fg.RESET}"
924902
"""
925903
return self._sequence
926904

927905

928906
class RgbBg(BgColor):
929-
"""
930-
Create ANSI sequences for 24-bit (RGB) terminal background text colors. The terminal must support 24-bit/true-color mode.
907+
"""Create ANSI sequences for 24-bit (RGB) terminal background text colors. The terminal must support 24-bit/true-color.
931908
To reset any background color, including 24-bit, use Bg.RESET.
932909
"""
933910

934911
def __init__(self, r: int, g: int, b: int) -> None:
935-
"""
936-
RgbBg initializer
912+
"""RgbBg initializer
937913
938914
:param r: integer from 0-255 for the red component of the color
939915
:param g: integer from 0-255 for the green component of the color
@@ -946,8 +922,7 @@ def __init__(self, r: int, g: int, b: int) -> None:
946922
self._sequence = f"{CSI}48;2;{r};{g};{b}m"
947923

948924
def __str__(self) -> str:
949-
"""
950-
Return ANSI color sequence instead of enum name
925+
"""Return ANSI color sequence instead of enum name
951926
This is helpful when using an RgbBg in an f-string or format() call
952927
e.g. my_str = f"{RgbBg(100, 255, 27)}hello{Bg.RESET}"
953928
"""
@@ -966,8 +941,7 @@ def style(
966941
strikethrough: Optional[bool] = None,
967942
underline: Optional[bool] = None,
968943
) -> str:
969-
"""
970-
Apply ANSI colors and/or styles to a string and return it.
944+
"""Apply ANSI colors and/or styles to a string and return it.
971945
The styling is self contained which means that at the end of the string reset code(s) are issued
972946
to undo whatever styling was done at the beginning.
973947
@@ -1056,7 +1030,6 @@ def async_alert_str(*, terminal_columns: int, prompt: str, line: str, cursor_off
10561030
:param alert_msg: the message to display to the user
10571031
:return: the correct string so that the alert message appears to the user to be printed above the current line.
10581032
"""
1059-
10601033
# Split the prompt lines since it can contain newline characters.
10611034
prompt_lines = prompt.splitlines() or ['']
10621035

cmd2/argparse_completer.py

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
"""
2-
This module defines the ArgparseCompleter class which provides argparse-based tab completion to cmd2 apps.
1+
"""This module defines the ArgparseCompleter class which provides argparse-based tab completion to cmd2 apps.
32
See the header of argparse_custom.py for instructions on how to use these features.
43
"""
54

@@ -76,8 +75,7 @@ def _single_prefix_char(token: str, parser: argparse.ArgumentParser) -> bool:
7675

7776

7877
def _looks_like_flag(token: str, parser: argparse.ArgumentParser) -> bool:
79-
"""
80-
Determine if a token looks like a flag. Unless an argument has nargs set to argparse.REMAINDER,
78+
"""Determine if a token looks like a flag. Unless an argument has nargs set to argparse.REMAINDER,
8179
then anything that looks like a flag can't be consumed as a value for it.
8280
Based on argparse._parse_optional().
8381
"""
@@ -138,8 +136,7 @@ def __init__(self, arg_action: argparse.Action) -> None:
138136

139137
class _UnfinishedFlagError(CompletionError):
140138
def __init__(self, flag_arg_state: _ArgumentState) -> None:
141-
"""
142-
CompletionError which occurs when the user has not finished the current flag
139+
"""CompletionError which occurs when the user has not finished the current flag
143140
:param flag_arg_state: information about the unfinished flag action
144141
"""
145142
arg = f'{argparse._get_action_name(flag_arg_state.action)}'
@@ -150,8 +147,7 @@ def __init__(self, flag_arg_state: _ArgumentState) -> None:
150147

151148
class _NoResultsError(CompletionError):
152149
def __init__(self, parser: argparse.ArgumentParser, arg_action: argparse.Action) -> None:
153-
"""
154-
CompletionError which occurs when there are no results. If hinting is allowed, then its message will
150+
"""CompletionError which occurs when there are no results. If hinting is allowed, then its message will
155151
be a hint about the argument being tab completed.
156152
:param parser: ArgumentParser instance which owns the action being tab completed
157153
:param arg_action: action being tab completed
@@ -166,8 +162,7 @@ class ArgparseCompleter:
166162
def __init__(
167163
self, parser: argparse.ArgumentParser, cmd2_app: 'Cmd', *, parent_tokens: Optional[dict[str, list[str]]] = None
168164
) -> None:
169-
"""
170-
Create an ArgparseCompleter
165+
"""Create an ArgparseCompleter
171166
172167
:param parser: ArgumentParser instance
173168
:param cmd2_app: reference to the Cmd2 application that owns this ArgparseCompleter
@@ -207,8 +202,7 @@ def __init__(
207202
def complete(
208203
self, text: str, line: str, begidx: int, endidx: int, tokens: list[str], *, cmd_set: Optional[CommandSet] = None
209204
) -> list[str]:
210-
"""
211-
Complete text using argparse metadata
205+
"""Complete text using argparse metadata
212206
213207
:param text: the string prefix we are attempting to match (all matches must begin with it)
214208
:param line: the current input line with leading whitespace removed
@@ -252,8 +246,7 @@ def consume_argument(arg_state: _ArgumentState) -> None:
252246
consumed_arg_values[arg_state.action.dest].append(token)
253247

254248
def update_mutex_groups(arg_action: argparse.Action) -> None:
255-
"""
256-
Check if an argument belongs to a mutually exclusive group and either mark that group
249+
"""Check if an argument belongs to a mutually exclusive group and either mark that group
257250
as complete or print an error if the group has already been completed
258251
:param arg_action: the action of the argument
259252
:raises CompletionError: if the group is already completed
@@ -501,7 +494,6 @@ def update_mutex_groups(arg_action: argparse.Action) -> None:
501494

502495
def _complete_flags(self, text: str, line: str, begidx: int, endidx: int, matched_flags: list[str]) -> list[str]:
503496
"""Tab completion routine for a parsers unused flags"""
504-
505497
# Build a list of flags that can be tab completed
506498
match_against = []
507499

@@ -535,7 +527,6 @@ def _complete_flags(self, text: str, line: str, begidx: int, endidx: int, matche
535527

536528
def _format_completions(self, arg_state: _ArgumentState, completions: Union[list[str], list[CompletionItem]]) -> list[str]:
537529
"""Format CompletionItems into hint table"""
538-
539530
# Nothing to do if we don't have at least 2 completions which are all CompletionItems
540531
if len(completions) < 2 or not all(isinstance(c, CompletionItem) for c in completions):
541532
return cast(list[str], completions)
@@ -611,8 +602,7 @@ def _format_completions(self, arg_state: _ArgumentState, completions: Union[list
611602
return cast(list[str], completions)
612603

613604
def complete_subcommand_help(self, text: str, line: str, begidx: int, endidx: int, tokens: list[str]) -> list[str]:
614-
"""
615-
Supports cmd2's help command in the completion of subcommand names
605+
"""Supports cmd2's help command in the completion of subcommand names
616606
:param text: the string prefix we are attempting to match (all matches must begin with it)
617607
:param line: the current input line with leading whitespace removed
618608
:param begidx: the beginning index of the prefix text
@@ -638,8 +628,7 @@ def complete_subcommand_help(self, text: str, line: str, begidx: int, endidx: in
638628
return []
639629

640630
def format_help(self, tokens: list[str]) -> str:
641-
"""
642-
Supports cmd2's help command in the retrieval of help text
631+
"""Supports cmd2's help command in the retrieval of help text
643632
:param tokens: arguments passed to help command
644633
:return: help text of the command being queried
645634
"""
@@ -668,8 +657,7 @@ def _complete_arg(
668657
*,
669658
cmd_set: Optional[CommandSet] = None,
670659
) -> list[str]:
671-
"""
672-
Tab completion routine for an argparse argument
660+
"""Tab completion routine for an argparse argument
673661
:return: list of completions
674662
:raises CompletionError: if the completer or choices function this calls raises one
675663
"""
@@ -767,8 +755,7 @@ def _complete_arg(
767755

768756

769757
def set_default_ap_completer_type(completer_type: type[ArgparseCompleter]) -> None:
770-
"""
771-
Set the default ArgparseCompleter class for a cmd2 app.
758+
"""Set the default ArgparseCompleter class for a cmd2 app.
772759
773760
:param completer_type: Type that is a subclass of ArgparseCompleter.
774761
"""

0 commit comments

Comments
 (0)