Skip to content

Commit 5e3caaa

Browse files
committed
Simplified some imports.
1 parent 4621211 commit 5e3caaa

File tree

5 files changed

+43
-43
lines changed

5 files changed

+43
-43
lines changed

cmd2/argparse_custom.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ def get_items(self) -> list[CompletionItems]:
295295
)
296296

297297
from . import constants
298-
from . import rich_utils as ru
298+
from .rich_utils import Cmd2Console
299299
from .styles import Cmd2Style
300300

301301
if TYPE_CHECKING: # pragma: no cover
@@ -1113,12 +1113,12 @@ def __init__(
11131113
max_help_position: int = 24,
11141114
width: int | None = None,
11151115
*,
1116-
console: ru.Cmd2Console | None = None,
1116+
console: Cmd2Console | None = None,
11171117
**kwargs: Any,
11181118
) -> None:
11191119
"""Initialize Cmd2HelpFormatter."""
11201120
if console is None:
1121-
console = ru.Cmd2Console()
1121+
console = Cmd2Console()
11221122

11231123
super().__init__(prog, indent_increment, max_help_position, width, console=console, **kwargs)
11241124

cmd2/string_utils.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@
99
from rich.align import AlignMethod
1010
from rich.style import StyleType
1111

12-
from .rich_utils import (
13-
console_width,
14-
rich_text_to_string,
15-
string_to_rich_text,
16-
)
12+
from . import rich_utils as ru
1713

1814

1915
def align(
@@ -33,11 +29,11 @@ def align(
3329
3430
"""
3531
if width is None:
36-
width = console_width()
32+
width = ru.console_width()
3733

38-
text = string_to_rich_text(val)
34+
text = ru.string_to_rich_text(val)
3935
text.align(align, width=width, character=character)
40-
return rich_text_to_string(text)
36+
return ru.rich_text_to_string(text)
4137

4238

4339
def align_left(
@@ -92,9 +88,9 @@ def stylize(val: str, style: StyleType) -> str:
9288
:param style: style instance or style definition to apply.
9389
:return: the stylized string
9490
"""
95-
text = string_to_rich_text(val)
91+
text = ru.string_to_rich_text(val)
9692
text.stylize(style)
97-
return rich_text_to_string(text)
93+
return ru.rich_text_to_string(text)
9894

9995

10096
def strip_style(val: str) -> str:
@@ -103,7 +99,7 @@ def strip_style(val: str) -> str:
10399
:param val: string which may contain ANSI style sequences
104100
:return: the same string with any ANSI style sequences removed
105101
"""
106-
text = string_to_rich_text(val)
102+
text = ru.string_to_rich_text(val)
107103
return text.plain
108104

109105

@@ -116,7 +112,7 @@ def str_width(val: str) -> int:
116112
:param val: the string being measured
117113
:return: width of the string when printed to the terminal
118114
"""
119-
text = string_to_rich_text(val)
115+
text = ru.string_to_rich_text(val)
120116
return text.cell_len
121117

122118

cmd2/terminal_utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
These are used for things like setting the window title and asynchronous alerts.
44
"""
55

6-
from .string_utils import str_width
6+
from . import string_utils as su
77

88
#######################################################
99
# Common ANSI escape sequence constants
@@ -108,14 +108,14 @@ def async_alert_str(*, terminal_columns: int, prompt: str, line: str, cursor_off
108108
# That will be included in the input lines calculations since that is where the cursor is.
109109
num_prompt_terminal_lines = 0
110110
for prompt_line in prompt_lines[:-1]:
111-
prompt_line_width = str_width(prompt_line)
111+
prompt_line_width = su.str_width(prompt_line)
112112
num_prompt_terminal_lines += int(prompt_line_width / terminal_columns) + 1
113113

114114
# Now calculate how many terminal lines are take up by the input
115115
last_prompt_line = prompt_lines[-1]
116-
last_prompt_line_width = str_width(last_prompt_line)
116+
last_prompt_line_width = su.str_width(last_prompt_line)
117117

118-
input_width = last_prompt_line_width + str_width(line)
118+
input_width = last_prompt_line_width + su.str_width(line)
119119

120120
num_input_terminal_lines = int(input_width / terminal_columns) + 1
121121

cmd2/transcript.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ class is used in cmd2.py::run_transcript_tests()
1818
cast,
1919
)
2020

21+
from . import string_utils as su
2122
from . import utils
22-
from .string_utils import strip_style
2323

2424
if TYPE_CHECKING: # pragma: no cover
2525
from cmd2 import (
@@ -74,13 +74,13 @@ def _test_transcript(self, fname: str, transcript: Iterator[str]) -> None:
7474

7575
line_num = 0
7676
finished = False
77-
line = strip_style(next(transcript))
77+
line = su.strip_style(next(transcript))
7878
line_num += 1
7979
while not finished:
8080
# Scroll forward to where actual commands begin
8181
while not line.startswith(self.cmdapp.visible_prompt):
8282
try:
83-
line = strip_style(next(transcript))
83+
line = su.strip_style(next(transcript))
8484
except StopIteration:
8585
finished = True
8686
break
@@ -106,14 +106,14 @@ def _test_transcript(self, fname: str, transcript: Iterator[str]) -> None:
106106
result = self.cmdapp.stdout.read()
107107
stop_msg = 'Command indicated application should quit, but more commands in transcript'
108108
# Read the expected result from transcript
109-
if strip_style(line).startswith(self.cmdapp.visible_prompt):
109+
if su.strip_style(line).startswith(self.cmdapp.visible_prompt):
110110
message = f'\nFile {fname}, line {line_num}\nCommand was:\n{command}\nExpected: (nothing)\nGot:\n{result}\n'
111111
assert not result.strip(), message # noqa: S101
112112
# If the command signaled the application to quit there should be no more commands
113113
assert not stop, stop_msg # noqa: S101
114114
continue
115115
expected_parts = []
116-
while not strip_style(line).startswith(self.cmdapp.visible_prompt):
116+
while not su.strip_style(line).startswith(self.cmdapp.visible_prompt):
117117
expected_parts.append(line)
118118
try:
119119
line = next(transcript)

cmd2/utils.py

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,27 @@
1212
import subprocess
1313
import sys
1414
import threading
15-
from collections.abc import Callable, Iterable
15+
from collections.abc import (
16+
Callable,
17+
Iterable,
18+
)
1619
from difflib import SequenceMatcher
1720
from enum import Enum
18-
from typing import TYPE_CHECKING, Any, TextIO, TypeVar, Union, cast
21+
from typing import (
22+
TYPE_CHECKING,
23+
Any,
24+
TextIO,
25+
TypeVar,
26+
Union,
27+
cast,
28+
)
1929

2030
from . import constants
21-
from .argparse_custom import ChoicesProviderFunc, CompleterFunc
31+
from . import string_utils as su
32+
from .argparse_custom import (
33+
ChoicesProviderFunc,
34+
CompleterFunc,
35+
)
2236

2337
if TYPE_CHECKING: # pragma: no cover
2438
import cmd2 # noqa: F401
@@ -188,9 +202,7 @@ def alphabetical_sort(list_to_sort: Iterable[str]) -> list[str]:
188202
:param list_to_sort: the list being sorted
189203
:return: the sorted list
190204
"""
191-
from .string_utils import norm_fold
192-
193-
return sorted(list_to_sort, key=norm_fold)
205+
return sorted(list_to_sort, key=su.norm_fold)
194206

195207

196208
def try_int_or_force_to_lower_case(input_str: str) -> int | str:
@@ -199,12 +211,10 @@ def try_int_or_force_to_lower_case(input_str: str) -> int | str:
199211
:param input_str: string to convert
200212
:return: the string as an integer or a lower case version of the string.
201213
"""
202-
from .string_utils import norm_fold
203-
204214
try:
205215
return int(input_str)
206216
except ValueError:
207-
return norm_fold(input_str)
217+
return su.norm_fold(input_str)
208218

209219

210220
def natural_keys(input_str: str) -> list[int | str]:
@@ -238,11 +248,9 @@ def quote_specific_tokens(tokens: list[str], tokens_to_quote: list[str]) -> None
238248
:param tokens: token list being edited
239249
:param tokens_to_quote: the tokens, which if present in tokens, to quote
240250
"""
241-
from .string_utils import quote
242-
243251
for i, token in enumerate(tokens):
244252
if token in tokens_to_quote:
245-
tokens[i] = quote(token)
253+
tokens[i] = su.quote(token)
246254

247255

248256
def unquote_specific_tokens(tokens: list[str], tokens_to_unquote: list[str]) -> None:
@@ -251,10 +259,8 @@ def unquote_specific_tokens(tokens: list[str], tokens_to_unquote: list[str]) ->
251259
:param tokens: token list being edited
252260
:param tokens_to_unquote: the tokens, which if present in tokens, to unquote
253261
"""
254-
from .string_utils import strip_quotes
255-
256262
for i, token in enumerate(tokens):
257-
unquoted_token = strip_quotes(token)
263+
unquoted_token = su.strip_quotes(token)
258264
if unquoted_token in tokens_to_unquote:
259265
tokens[i] = unquoted_token
260266

@@ -264,12 +270,10 @@ def expand_user(token: str) -> str:
264270
265271
:param token: the string to expand
266272
"""
267-
from .string_utils import is_quoted, strip_quotes
268-
269273
if token:
270-
if is_quoted(token):
274+
if su.is_quoted(token):
271275
quote_char = token[0]
272-
token = strip_quotes(token)
276+
token = su.strip_quotes(token)
273277
else:
274278
quote_char = ''
275279

0 commit comments

Comments
 (0)