Skip to content

Commit

Permalink
added base-directory to siles
Browse files Browse the repository at this point in the history
Signed-off-by: Nick Papior <[email protected]>
  • Loading branch information
zerothi committed Nov 16, 2023
1 parent 97454ea commit 9a39f7d
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/sisl/io/sile.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,12 @@ def base_file(self):
"""File of the current `Sile`"""
return basename(self._file)

def base_directory(self, relative_to="."):
"""Retrieve the base directory of the file, relative to the path `relative_to`"""
if isinstance(relative_to, str):
relative_to = Path(relative_to)
return self._directory.relative_to(relative_to.resolve())

def dir_file(self, filename=None, filename_base=""):
"""File of the current `Sile`"""
if filename is None:
Expand Down Expand Up @@ -630,11 +636,7 @@ def __str__(self):
# Check if the directory is relative to the current path
# If so, only print the relative path, otherwise print the full path
d = self._directory
try:
# bypass d.is_relative_to, added in 3.9
d = d.relative_to(Path(".").resolve())
except Exception:
pass
d = self._directory.relative_to(Path(".").resolve())
return f"{self.__class__.__name__}({self.base_file!s}, base={d!s})"


Expand Down

0 comments on commit 9a39f7d

Please sign in to comment.