Commit d0b1ba41 authored by Marco Kurzynski's avatar Marco Kurzynski
Browse files

WIP fixing file_finder

parent 9191f1d3
......@@ -4,38 +4,41 @@ import os
from datetime import datetime
def getDirs(fp):
# helper for list comprehension
def valid_dir(d):
if os.path.isdir(fp):
try:
datetime.strptime(os.path.basename(d)[:8], "AE%y%m%d")
return True
except ValueError:
pass
return False
# default in case of file passed
dirs = ()
directory = os.path.realpath(fp)
dirs = []
if os.path.isdir(directory):
try:
datetime.strptime(os.path.basename(directory)[:8], "AE%y%m%d")
dirs.append(directory)
except ValueError:
for d in os.listdir(directory):
if os.path.isdir(os.path.join(directory, d)):
try:
datetime.strptime(os.path.basename(d)[:8], "AE%y%m%d")
dirs.append(os.path.join(directory, d))
except ValueError:
pass
if valid_dir(directory):
dirs = (directory, )
elif os.path.isdir(directory):
dirs = tuple(os.path.join(directory, d) for d in os.listdir(directory) if valid_dir(d))
return dirs
def getFiles(fp, extensions=[]):
if os.path.isfile(fp):
tree = {os.path.dirname(os.path.realpath(fp)):[]}
extension = os.path.splitext(fp)[1]
if not extensions:
tree[os.path.dirname(os.path.realpath(fp))].append(os.path.basename(fp))
elif extension.upper() in extensions or extension.lower() in extensions:
tree[os.path.dirname(os.path.realpath(fp))].append(os.path.basename(fp))
else:
tree = {d:[] for d in getDirs(fp)}
for d in tree.keys():
for f in os.listdir(d):
extension = os.path.splitext(f)[1]
if not extensions:
tree[d].append(f)
elif extension.upper() in extensions or extension.lower() in extensions:
tree[d].append(f)
# helper for list comprehension
def valid_file(f):
if os.path.isfile(f):
extension = os.path.splitext(f)[1]
if not extensions or extension.upper() in extensions or extension.lower() in extensions:
return True
return False
tree = {};
fp = os.path.realpath(fp)
if valid_file(fp):
tree = {os.path.dirname(fp) : (os.path.basename(fp),)}
elif os.path.isdir(fp):
tree = {d : tuple(os.path.basename(f) for f in os.listdir(d) if valid_file(os.path.join(d,f))) for d in getDirs(fp)}
return tree
from utils import file_finder
import os
FILL_STRING = "foo"
# helper function for creating file structure
def create_dirs(tmp_path, dirs):
# create file structure for testing
for d in dirs:
d.mkdir()
files = (d / f"file1_{d.name[2:]}.RNC", d / f"file2_{d.name[2:]}.txt", d / f"file3_{d.name[2:]}.CXS", d / f"file4_{d.name[2:]}.sum")
for f in files:
f.write_text(FILL_STRING)
assert f.read_text() == FILL_STRING # validate file exists and was written to
def test_file_finder(tmp_path):
# create file structure
dirs = (tmp_path / "AE210101", tmp_path / "AE210102", tmp_path / "AE210103")
create_dirs(tmp_path, dirs)
# run on multiple directories
# check getDirs
found_dirs = file_finder.getDirs(tmp_path)
expected_dirs = (str(d) for d in dirs)
assert(sorted(found_dirs) == sorted(expected_dirs))
# check getFiles
found_files = file_finder.getFiles(tmp_path)
expected_files = {str(d):(f"file1_{d.name[2:]}.RNC", f"file2_{d.name[2:]}.txt", f"file3_{d.name[2:]}.CXS", f"file4_{d.name[2:]}.sum") for d in dirs}
for k in found_files.keys():
assert(sorted(expected_files[k]) == sorted(found_files[k]))
# run on single directory
# check getDirs
found_dirs = file_finder.getDirs(dirs[0]) # run on directory
expected_dirs = (str(dirs[0]),)
assert(found_dirs == expected_dirs)
# check getFiles
found_files = file_finder.getFiles(dirs[0])
expected_files = {str(dirs[0]):(f"file1_{dirs[0].name[2:]}.RNC", f"file2_{dirs[0].name[2:]}.txt", f"file3_{dirs[0].name[2:]}.CXS", f"file4_{dirs[0].name[2:]}.sum")}
for k in found_files.keys():
assert(sorted(expected_files[k]) == sorted(found_files[k]))
# run on a single file
# create single file
f = tmp_path / "just_a_file.txt"
f.write_text(FILL_STRING)
assert f.read_text() == FILL_STRING
# getDirs should be empty
found_dirs = file_finder.getDirs(f)
assert(found_dirs == ())
# check getFiles, should only have single dir and file
found_files = file_finder.getFiles(str(f))
expected_files = {os.path.dirname(f) : (str(f.name),)}
assert(found_files == expected_files)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment