diff options
author | Todd Leonhardt <todd.leonhardt@gmail.com> | 2019-06-07 00:00:21 -0400 |
---|---|---|
committer | Todd Leonhardt <todd.leonhardt@gmail.com> | 2019-06-07 00:00:21 -0400 |
commit | 069b181a887445fde7b66e27e537a9653d70e3dc (patch) | |
tree | 04d82f38d3f3276fecce9df833698c940cd9bdfd | |
parent | 16f6bee13c9b568c898a9997e2813b343ca98596 (diff) | |
download | cmd2-git-069b181a887445fde7b66e27e537a9653d70e3dc.tar.gz |
Moved a new helper function from cmd2.py to utils.py where it probably belonged
-rw-r--r-- | cmd2/cmd2.py | 16 | ||||
-rw-r--r-- | cmd2/utils.py | 16 |
2 files changed, 17 insertions, 15 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index 520ac33d..65574095 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -3700,18 +3700,6 @@ class Cmd(cmd.Cmd): relative_path = os.path.join(self._current_script_dir or '', file_path) return self.do_load(relative_path) - def _expand_transcripts(self, transcript_paths: List[str]) -> List[str]: - """Expand glob patterns to match transcript files. - - :param transcript_paths: list of transcript file paths (expanded for user), possibly including glob patterns - :return: list of transcript file paths with glob patterns expanded - """ - expanded_transcripts = [] - for pattern in transcript_paths: - files = utils.files_from_glob_pattern(pattern, access=os.R_OK) - expanded_transcripts.extend(files) - return expanded_transcripts - def run_transcript_tests(self, transcript_paths: List[str]) -> None: """Runs transcript tests for provided file(s). @@ -3726,10 +3714,8 @@ class Cmd(cmd.Cmd): class TestMyAppCase(Cmd2TestCase): cmdapp = self - # Expand glob patterns - transcripts_expanded = self._expand_transcripts(transcript_paths) - # Validate that there is at least one transcript file + transcripts_expanded = utils.files_from_glob_patterns(transcript_paths, access=os.R_OK) if not transcripts_expanded: self.perror('No test files found - nothing to test', traceback_war=False) self.exit_code = -1 diff --git a/cmd2/utils.py b/cmd2/utils.py index 0db61ca5..3500ba7a 100644 --- a/cmd2/utils.py +++ b/cmd2/utils.py @@ -332,6 +332,22 @@ def files_from_glob_pattern(pattern: str, access=os.F_OK) -> List[str]: return [f for f in glob.glob(pattern) if os.path.isfile(f) and os.access(f, access)] +def files_from_glob_patterns(patterns: List[str], access=os.F_OK) -> List[str]: + """Return a list of file paths based on a list of glob patterns. + + Only files are returned, not directories, and optionally only files for which the user has a specified access to. + + :param patterns: list of file names and/or glob patterns + :param access: file access type to verify (os.* where * is F_OK, R_OK, W_OK, or X_OK) + :return: list of files matching the names and/or glob patterns + """ + files = [] + for pattern in patterns: + matches = files_from_glob_pattern(pattern, access=access) + files.extend(matches) + return files + + class StdSim(object): """ Class to simulate behavior of sys.stdout or sys.stderr. |