in wstl1/tools/notebook/extensions/wstl/magics/_parser.py [0:0]
def _get_files(path_name, file_ext, load_contents):
"""Retrieves a list of files located at path_name.
Supports glob wildcard expressions.
Args:
path_name: the file path, including glob patterns supported by the python
glob module.
file_ext: file extensions to be loaded.
load_contents: Loads the contents of the files from disk.
Returns:
A list of file contents.
Raises:
ValueError: The file specified in the wildcard expression does not end with
an expected extension.
JSONDecodeError: The file does not contain JSON decodable data.
"""
if not file_ext:
raise ValueError("empty required extensions.")
contents = list()
norm_path = os.path.normpath(path_name)
if norm_path.startswith("~"):
norm_path = os.path.expanduser(norm_path)
for name in glob.glob(os.path.abspath(norm_path)):
_, ext = os.path.splitext(name)
if ext is None or ext not in file_ext:
continue
if os.path.isfile(name):
if load_contents:
with open(name, "r") as f:
if ext == ".json":
# decode and encode to verify contents of file are valid JSON.
content = json.load(f)
contents.append(json.dumps(content))
elif ext == ".ndjson":
json_content = f.readlines()
if json_content:
for line in json_content:
# decode and encode to verify contents of line are valid JSON.
content = json.loads(line.strip())
contents.append(json.dumps(content))
elif ext == ".wstl" or ext == ".textproto":
contents.append(f.read())
else:
raise ValueError("invalid file prefix for file {}".format(name))
else:
contents.append(name)
elif os.path.isdir(name):
raise ValueError(
"use glob expression to specify files in directory {}".format(name))
return contents