Skip to content

Commit 4699451

Browse files
committed
Added type hinting.
1 parent c1fc040 commit 4699451

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

cmd2/pyscript_bridge.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
88
"""
99

1010
import argparse
11-
from collections import namedtuple
1211
import functools
1312
import sys
14-
from typing import List, Tuple
13+
from typing import List, Tuple, Callable
1514

1615
# Python 3.4 require contextlib2 for temporarily redirecting stderr and stdout
1716
if sys.version_info < (3, 5):
@@ -41,7 +40,7 @@ def __bool__(self):
4140

4241
class CopyStream(object):
4342
"""Copies all data written to a stream"""
44-
def __init__(self, inner_stream, echo):
43+
def __init__(self, inner_stream, echo: bool = False):
4544
self.buffer = ''
4645
self.inner_stream = inner_stream
4746
self.echo = echo
@@ -57,14 +56,14 @@ def read(self):
5756
def clear(self):
5857
self.buffer = ''
5958

60-
def __getattr__(self, item):
59+
def __getattr__(self, item: str):
6160
if item in self.__dict__:
6261
return self.__dict__[item]
6362
else:
6463
return getattr(self.inner_stream, item)
6564

6665

67-
def _exec_cmd(cmd2_app, func, echo):
66+
def _exec_cmd(cmd2_app, func: Callable, echo: bool):
6867
"""Helper to encapsulate executing a command and capturing the results"""
6968
copy_stdout = CopyStream(sys.stdout, echo)
7069
copy_stderr = CopyStream(sys.stderr, echo)
@@ -93,10 +92,10 @@ class ArgparseFunctor:
9392
"""
9493
Encapsulates translating python object traversal
9594
"""
96-
def __init__(self, echo: bool, cmd2_app, item, parser):
95+
def __init__(self, echo: bool, cmd2_app, command_name: str, parser: argparse.ArgumentParser):
9796
self._echo = echo
9897
self._cmd2_app = cmd2_app
99-
self._item = item
98+
self._command_name = command_name
10099
self._parser = parser
101100

102101
# Dictionary mapping command argument name to value
@@ -112,7 +111,7 @@ def __dir__(self):
112111
commands.extend(action.choices)
113112
return commands
114113

115-
def __getattr__(self, item):
114+
def __getattr__(self, item: str):
116115
"""Search for a subcommand matching this item and update internal state to track the traversal"""
117116
# look for sub-command under the current command/sub-command layer
118117
for action in self.__current_subcommand_parser._actions:
@@ -205,7 +204,7 @@ def __call__(self, *args, **kwargs):
205204

206205
def _run(self):
207206
# look up command function
208-
func = getattr(self._cmd2_app, 'do_' + self._item)
207+
func = getattr(self._cmd2_app, 'do_' + self._command_name)
209208

210209
# reconstruct the cmd2 command from the python call
211210
cmd_str = ['']
@@ -298,5 +297,5 @@ def __dir__(self):
298297
commands.insert(0, 'cmd_echo')
299298
return commands
300299

301-
def __call__(self, args):
300+
def __call__(self, args: str):
302301
return _exec_cmd(self._cmd2_app, functools.partial(self._cmd2_app.onecmd_plus_hooks, args + '\n'), self.cmd_echo)

0 commit comments

Comments
 (0)