summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2018-10-26 11:40:15 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2018-10-26 11:40:15 -0400
commitdfbe317a7cc52b691723698b4464b260996ab425 (patch)
tree85df59946767d98d80a9bf4d0cab367ab4aae3c7
parent2cc1b90eba33fae1bf8d06a49bb9f64b662569ad (diff)
downloadcmd2-git-dfbe317a7cc52b691723698b4464b260996ab425.tar.gz
Refactored filtering in path_complete to use a function
-rw-r--r--cmd2/cmd2.py22
-rw-r--r--cmd2/transcript.py2
-rw-r--r--docs/freefeatures.rst2
-rwxr-xr-xexamples/python_scripting.py2
-rw-r--r--tests/test_completion.py2
5 files changed, 15 insertions, 15 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index b9e6aa99..f8dd63d3 100644
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -1007,16 +1007,17 @@ class Cmd(cmd.Cmd):
return matches
# noinspection PyUnusedLocal
- def path_complete(self, text: str, line: str, begidx: int, endidx: int, dir_exe_only: bool=False,
- dir_only: bool=False) -> List[str]:
+ def path_complete(self, text: str, line: str, begidx: int, endidx: int,
+ path_filter: Optional[Callable[[str], bool]] = None) -> List[str]:
"""Performs completion of local file system paths
:param text: the string prefix we are attempting to match (all returned matches must begin with it)
:param line: the current input line with leading whitespace removed
:param begidx: the beginning index of the prefix text
:param endidx: the ending index of the prefix text
- :param dir_exe_only: only return directories and executables, not non-executable files
- :param dir_only: only return directories
+ :param path_filter: optional filter function that determines if a path belongs in the results
+ this function takes a path as its argument and returns True if the path should
+ be kept in the results
:return: a list of possible tab completions
"""
@@ -1076,7 +1077,7 @@ class Cmd(cmd.Cmd):
search_str = os.path.join(os.getcwd(), '*')
cwd_added = True
else:
- # Purposely don't match any path containing wildcards - what we are doing is complicated enough!
+ # Purposely don't match any path containing wildcards
wildcards = ['*', '?']
for wildcard in wildcards:
if wildcard in text:
@@ -1112,11 +1113,9 @@ class Cmd(cmd.Cmd):
# Find all matching path completions
matches = glob.glob(search_str)
- # Filter based on type
- if dir_exe_only:
- matches = [c for c in matches if os.path.isdir(c) or os.access(c, os.X_OK)]
- elif dir_only:
- matches = [c for c in matches if os.path.isdir(c)]
+ # Filter out results that don't belong
+ if path_filter is not None:
+ matches = [c for c in matches if path_filter(c)]
# Don't append a space or closing quote to directory
if len(matches) == 1 and os.path.isdir(matches[0]):
@@ -1199,7 +1198,8 @@ class Cmd(cmd.Cmd):
# Otherwise look for executables in the given path
else:
- return self.path_complete(text, line, begidx, endidx, dir_exe_only=True)
+ return self.path_complete(text, line, begidx, endidx,
+ lambda path: os.path.isdir(path) or os.access(path, os.X_OK))
def _redirect_complete(self, text: str, line: str, begidx: int, endidx: int, compfunc: Callable) -> List[str]:
"""Called by complete() as the first tab completion function for all commands
diff --git a/cmd2/transcript.py b/cmd2/transcript.py
index baaa6caf..fe43188f 100644
--- a/cmd2/transcript.py
+++ b/cmd2/transcript.py
@@ -89,7 +89,7 @@ class Cmd2TestCase(unittest.TestCase):
if utils.strip_ansi(line).startswith(self.cmdapp.visible_prompt):
message = '\nFile {}, line {}\nCommand was:\n{}\nExpected: (nothing)\nGot:\n{}\n'.format(
fname, line_num, command, result)
- self.assert_(not (result.strip()), message)
+ self.assertTrue(not (result.strip()), message)
continue
expected = []
while not utils.strip_ansi(line).startswith(self.cmdapp.visible_prompt):
diff --git a/docs/freefeatures.rst b/docs/freefeatures.rst
index 0a95a829..7864f146 100644
--- a/docs/freefeatures.rst
+++ b/docs/freefeatures.rst
@@ -350,4 +350,4 @@ path completion of directories only for this command by adding a line of code si
which inherits from ``cmd2.Cmd``::
# Make sure you have an "import functools" somewhere at the top
- complete_bar = functools.partialmethod(cmd2.Cmd.path_complete, dir_only=True)
+ complete_bar = functools.partialmethod(cmd2.Cmd.path_complete, path_filter=os.path.isdir)
diff --git a/examples/python_scripting.py b/examples/python_scripting.py
index 0b0030a5..7847b8b6 100755
--- a/examples/python_scripting.py
+++ b/examples/python_scripting.py
@@ -88,7 +88,7 @@ class CmdLineApp(cmd2.Cmd):
# Enable tab completion for cd command
def complete_cd(self, text, line, begidx, endidx):
- return self.path_complete(text, line, begidx, endidx, dir_only=True)
+ return self.path_complete(text, line, begidx, endidx, path_filter=os.path.isdir)
dir_parser = argparse.ArgumentParser()
dir_parser.add_argument('-l', '--long', action='store_true', help="display in long format with one item per line")
diff --git a/tests/test_completion.py b/tests/test_completion.py
index 0df06423..9f681359 100644
--- a/tests/test_completion.py
+++ b/tests/test_completion.py
@@ -399,7 +399,7 @@ def test_path_completion_directories_only(cmd2_app, request):
expected = [text + 'cripts' + os.path.sep]
- assert cmd2_app.path_complete(text, line, begidx, endidx, dir_only=True) == expected
+ assert cmd2_app.path_complete(text, line, begidx, endidx, os.path.isdir) == expected
def test_basic_completion_single(cmd2_app):
text = 'Pi'