in src/buildstream/_frontend/complete.py [0:0]
def complete_path(path_type, incomplete, base_directory="."):
"""Helper method for implementing the completions() method
for File and Path parameter types.
"""
# Try listing the files in the relative or absolute path
# specified in `incomplete` minus the last path component,
# otherwise list files starting from the current working directory.
entries = []
base_path = ""
# This is getting a bit messy
listed_base_directory = False
if os.path.sep in incomplete:
split = incomplete.rsplit(os.path.sep, 1)
base_path = split[0]
# If there was nothing on the left of the last separator,
# we are completing files in the filesystem root
base_path = os.path.join(base_directory, base_path)
else:
incomplete_base_path = os.path.join(base_directory, incomplete)
if os.path.isdir(incomplete_base_path):
base_path = incomplete_base_path
try:
if base_path:
if os.path.isdir(base_path):
entries = [os.path.join(base_path, e) for e in os.listdir(base_path)]
else:
entries = os.listdir(base_directory)
listed_base_directory = True
except OSError:
# If for any reason the os reports an error from os.listdir(), just
# ignore this and avoid a stack trace
pass
base_directory_slash = base_directory
if not base_directory_slash.endswith(os.sep):
base_directory_slash += os.sep
base_directory_len = len(base_directory_slash)
def entry_is_dir(entry):
if listed_base_directory:
entry = os.path.join(base_directory, entry)
return os.path.isdir(entry)
def fix_path(path):
# Append slashes to any entries which are directories, or
# spaces for other files since they cannot be further completed
if entry_is_dir(path) and not path.endswith(os.sep):
path = path + os.sep
else:
path = path + " "
# Remove the artificial leading path portion which
# may have been prepended for search purposes.
if path.startswith(base_directory_slash):
path = path[base_directory_len:]
return path
return [
# Return an appropriate path for each entry
fix_path(e)
for e in sorted(entries)
# Filter out non directory elements when searching for a directory,
# the opposite is fine, however.
if not (path_type == "Directory" and not entry_is_dir(e))
]