diff options
author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2019-06-14 17:36:44 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-06-14 17:36:44 -0400 |
commit | ddd07f9cd6d72baca1232ae98856cf3b3d564706 (patch) | |
tree | 8e03d435730baf1cc16ccc016e594d0b64d8e04a /cmd2/utils.py | |
parent | f64f9d559caa08b5649b9bd356af2812acf103bd (diff) | |
parent | 756d8d38502e934ea180c4cfb8dea3efd124a3bf (diff) | |
download | cmd2-git-ddd07f9cd6d72baca1232ae98856cf3b3d564706.tar.gz |
Merge pull request #696 from python-cmd2/script_refactor
Script refactor
Diffstat (limited to 'cmd2/utils.py')
-rw-r--r-- | cmd2/utils.py | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/cmd2/utils.py b/cmd2/utils.py index 54ad763d..3500ba7a 100644 --- a/cmd2/utils.py +++ b/cmd2/utils.py @@ -2,6 +2,7 @@ """Shared utility functions""" import collections +import glob import os import re import subprocess @@ -319,6 +320,34 @@ def find_editor() -> str: return editor +def files_from_glob_pattern(pattern: str, access=os.F_OK) -> List[str]: + """Return a list of file paths based on a glob pattern. + + Only files are returned, not directories, and optionally only files for which the user has a specified access to. + + :param pattern: file name or glob pattern + :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 name or glob pattern + """ + 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. |