2023-03-21 05:09:52 -06:00
|
|
|
import logging
|
2023-03-08 07:02:14 -07:00
|
|
|
|
|
|
|
def barename(file):
|
|
|
|
(val, _) = os.path.splitext(os.path.basename(file))
|
|
|
|
return val
|
|
|
|
|
|
|
|
def ext(file):
|
|
|
|
(_, val) = os.path.splitext(os.path.basename(file))
|
|
|
|
return val.lower()
|
|
|
|
|
|
|
|
def same_barename(lhs, rhs):
|
|
|
|
return barename(lhs) == barename(rhs)
|
|
|
|
|
|
|
|
def is_image(file):
|
|
|
|
return ext(file) in {'.jpg', '.jpeg', '.png', '.bmp', '.webp', '.jfif'}
|
|
|
|
|
|
|
|
def read_text(file):
|
|
|
|
try:
|
|
|
|
with open(file, encoding='utf-8', mode='r') as stream:
|
|
|
|
return stream.read().strip()
|
|
|
|
except Exception as e:
|
|
|
|
logging.warning(f" *** Error reading text file {file}: {e}")
|
|
|
|
|
|
|
|
def read_float(file):
|
|
|
|
try:
|
|
|
|
return float(read_text(file))
|
|
|
|
except Exception as e:
|
2023-04-18 20:11:51 -06:00
|
|
|
logging.warning(f" *** Could not parse number to float in file {file}: {e}")
|
2023-03-08 07:02:14 -07:00
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
def walk_and_visit(path, visit_fn, context=None):
|
|
|
|
names = [entry.name for entry in os.scandir(path)]
|
|
|
|
|
|
|
|
dirs = []
|
|
|
|
files = []
|
|
|
|
for name in names:
|
|
|
|
fullname = os.path.join(path, name)
|
2023-03-31 16:05:51 -06:00
|
|
|
|
|
|
|
if str(name).startswith('.'):
|
|
|
|
continue
|
|
|
|
|
|
|
|
if os.path.isdir(fullname):
|
2023-03-08 07:02:14 -07:00
|
|
|
dirs.append(fullname)
|
|
|
|
else:
|
|
|
|
files.append(fullname)
|
|
|
|
|
|
|
|
subcontext = visit_fn(files, context)
|
|
|
|
|
|
|
|
for subdir in dirs:
|
2023-04-18 20:11:51 -06:00
|
|
|
walk_and_visit(subdir, visit_fn, subcontext)
|