diff options
author | Eric Lin <anselor@gmail.com> | 2018-05-18 10:40:28 -0400 |
---|---|---|
committer | Eric Lin <anselor@gmail.com> | 2018-05-18 10:40:28 -0400 |
commit | 4699451df8d0b0ff87b2332ab26498e372309ec4 (patch) | |
tree | f25af604947e3f533bb10f8fe0d892779a775166 /cmd2/pyscript_bridge.py | |
parent | c1fc040e9d86e3b6973f63ebaa43fdedc24d8815 (diff) | |
download | cmd2-git-4699451df8d0b0ff87b2332ab26498e372309ec4.tar.gz |
Added type hinting.
Diffstat (limited to 'cmd2/pyscript_bridge.py')
-rw-r--r-- | cmd2/pyscript_bridge.py | 19 |
1 files changed, 9 insertions, 10 deletions
diff --git a/cmd2/pyscript_bridge.py b/cmd2/pyscript_bridge.py index a1c367e2..277d8531 100644 --- a/cmd2/pyscript_bridge.py +++ b/cmd2/pyscript_bridge.py @@ -8,10 +8,9 @@ Released under MIT license, see LICENSE file """ import argparse -from collections import namedtuple import functools import sys -from typing import List, Tuple +from typing import List, Tuple, Callable # Python 3.4 require contextlib2 for temporarily redirecting stderr and stdout if sys.version_info < (3, 5): @@ -41,7 +40,7 @@ class CommandResult(namedtuple_with_defaults('CmdResult', ['stdout', 'stderr', ' class CopyStream(object): """Copies all data written to a stream""" - def __init__(self, inner_stream, echo): + def __init__(self, inner_stream, echo: bool = False): self.buffer = '' self.inner_stream = inner_stream self.echo = echo @@ -57,14 +56,14 @@ class CopyStream(object): def clear(self): self.buffer = '' - def __getattr__(self, item): + def __getattr__(self, item: str): if item in self.__dict__: return self.__dict__[item] else: return getattr(self.inner_stream, item) -def _exec_cmd(cmd2_app, func, echo): +def _exec_cmd(cmd2_app, func: Callable, echo: bool): """Helper to encapsulate executing a command and capturing the results""" copy_stdout = CopyStream(sys.stdout, echo) copy_stderr = CopyStream(sys.stderr, echo) @@ -93,10 +92,10 @@ class ArgparseFunctor: """ Encapsulates translating python object traversal """ - def __init__(self, echo: bool, cmd2_app, item, parser): + def __init__(self, echo: bool, cmd2_app, command_name: str, parser: argparse.ArgumentParser): self._echo = echo self._cmd2_app = cmd2_app - self._item = item + self._command_name = command_name self._parser = parser # Dictionary mapping command argument name to value @@ -112,7 +111,7 @@ class ArgparseFunctor: commands.extend(action.choices) return commands - def __getattr__(self, item): + def __getattr__(self, item: str): """Search for a subcommand matching this item and update internal state to track the traversal""" # look for sub-command under the current command/sub-command layer for action in self.__current_subcommand_parser._actions: @@ -205,7 +204,7 @@ class ArgparseFunctor: def _run(self): # look up command function - func = getattr(self._cmd2_app, 'do_' + self._item) + func = getattr(self._cmd2_app, 'do_' + self._command_name) # reconstruct the cmd2 command from the python call cmd_str = [''] @@ -298,5 +297,5 @@ class PyscriptBridge(object): commands.insert(0, 'cmd_echo') return commands - def __call__(self, args): + def __call__(self, args: str): return _exec_cmd(self._cmd2_app, functools.partial(self._cmd2_app.onecmd_plus_hooks, args + '\n'), self.cmd_echo) |