Skip to content

Commit

Permalink
Merge pull request #146 from fmi-faim/config
Browse files Browse the repository at this point in the history
Add config which exposes methods to make paths relative/absolute.
  • Loading branch information
imagejan authored Jun 19, 2024
2 parents e3f92f2 + a31e63f commit 634002c
Showing 1 changed file with 48 additions and 7 deletions.
55 changes: 48 additions & 7 deletions src/faim_ipa/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import logging
import pathlib
from datetime import datetime
import os.path
from pathlib import Path

import pydantic
from pydantic import BaseModel


def wavelength_to_rgb(wavelength, gamma=0.8):
Expand Down Expand Up @@ -79,7 +82,7 @@ def create_logger(name: str) -> logging.Logger:
return logger


def get_git_root() -> Path:
def get_git_root() -> pathlib.Path:
"""
Recursively search for the directory containing the .git folder.
Expand All @@ -88,14 +91,14 @@ def get_git_root() -> Path:
Path
Path to the root of the git repository.
"""
parent_dir = Path(__file__).parent
parent_dir = pathlib.Path(__file__).parent
while not (parent_dir / ".git").exists():
parent_dir = parent_dir.parent

return parent_dir


def resolve_with_git_root(relative_path: Path) -> Path:
def resolve_with_git_root(relative_path: pathlib.Path) -> pathlib.Path:
"""
Takes a relative path and resolves it relative to the git_root directory.
Expand All @@ -113,7 +116,7 @@ def resolve_with_git_root(relative_path: Path) -> Path:
return (git_root / relative_path).resolve()


def make_relative_to_git_root(path: Path) -> Path:
def make_relative_to_git_root(path: pathlib.Path) -> pathlib.Path:
"""
Convert an absolute path to a path relative to the git_root directory.
Expand All @@ -124,7 +127,7 @@ def make_relative_to_git_root(path: Path) -> Path:
Returns
-------
Path
pathlib.Path
Path relative to the git root.
"""
git_root = get_git_root()
Expand All @@ -133,4 +136,42 @@ def make_relative_to_git_root(path: Path) -> Path:
return path.relative_to(git_root, walk_up=True)
except (ValueError, TypeError):
# fallback for Python < 3.12
return Path(os.path.relpath(path, git_root))
return pathlib.Path(os.path.relpath(path, git_root))


class IPAConfig(BaseModel):

def make_paths_absolute(self):
"""
Convert all `pathlib.Path` fields to absolute paths.
The paths are assumed to be relative to a git-root directory somewhere
in the parent directories of the class implementing `IPAConfig`.
"""
if pydantic.__version__.startswith("2"):
fields = self.model_fields_set
else:
fields = self.__fields_set__

for f in fields:
attr = getattr(self, f)
if isinstance(attr, pathlib.Path) and not attr.is_absolute():
setattr(self, f, resolve_with_git_root(attr))

def make_paths_relative(self):
"""
Convert all `pathlib.Path` fields to relative paths.
The resulting paths will be relative to the git-root directory
somewhere in the parent directories of the class implementing
`IPAConfig`.
"""
if pydantic.__version__.startswith("2"):
fields = self.model_fields_set
else:
fields = self.__fields_set__

for f in fields:
attr = getattr(self, f)
if isinstance(attr, pathlib.Path) and attr.is_absolute():
setattr(self, f, make_relative_to_git_root(attr))

0 comments on commit 634002c

Please sign in to comment.