"""helperfile for providing path managing methods.
- conversion to rel path
- listing of a filetree
"""
import os, sys, pathlib
from loguru import logger
[docs]
def getRelPath(absPath: str) -> str:
"""
returns the relative path for some absolute path
Parameters
----------
absPath : str
the absolute path, which is to be convert
Returns
-------
relPath : str
the resulting relative path
"""
logger.trace("Starting getRelPath function with input: " + absPath)
abs_path_object = pathlib.Path(absPath)
relPath = os.path.relpath(str(abs_path_object), start=os.getcwd())
logger.trace("Finished getRelPath function with Output: " + relPath)
return relPath
[docs]
def list_relative_filepaths(path: str) -> list[str]:
"""
Collects all files in a directory tree and returns their paths relative to the given base path.
Parameters
----------
path : str
The base directory to scan (relative path only).
Returns
-------
filepaths : list[str]
List of file paths relative to the provided base directory.
"""
logger.trace("Started list_relative_filepaths function with Input: " +path)
if os.path.isabs(path):
err_msg = "Path must be relative"
logger.error(err_msg)
raise ValueError(err_msg)
search_path = os.path.abspath(path)
base_path = os.getcwd()
if not os.path.isdir(base_path):
logger.debug("No files found! Exiting list_relative_filepaths")
return []
filepaths: list[str] = []
for root, _, files in os.walk(search_path): #NOTE Why the ,_, ?
for filename in files:
absolute_path = os.path.join(root, filename)
relative_path = os.path.relpath(absolute_path, start=base_path)
filepaths.append(relative_path)
logger.trace("Finished list_relative_filepaths Function with output: " + str(filepaths))
return filepaths