summaryrefslogtreecommitdiff
path: root/cmd2/utils.py
diff options
context:
space:
mode:
authorTodd Leonhardt <todd.leonhardt@gmail.com>2019-06-06 23:50:46 -0400
committerTodd Leonhardt <todd.leonhardt@gmail.com>2019-06-06 23:50:46 -0400
commit16f6bee13c9b568c898a9997e2813b343ca98596 (patch)
tree7f3dc13ab920aa3e6c4151555fb6494295200613 /cmd2/utils.py
parent6cdf70823b344b99d6623af19fb618a9c2dbdad4 (diff)
downloadcmd2-git-16f6bee13c9b568c898a9997e2813b343ca98596.tar.gz
Extracted duplicated code to utility function
Diffstat (limited to 'cmd2/utils.py')
-rw-r--r--cmd2/utils.py13
1 files changed, 13 insertions, 0 deletions
diff --git a/cmd2/utils.py b/cmd2/utils.py
index 54ad763d..0db61ca5 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,18 @@ 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)]
+
+
class StdSim(object):
"""
Class to simulate behavior of sys.stdout or sys.stderr.