Skip to content

Commit

Permalink
SPath: Add some simple docstrings (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
LightArrowsEXE authored Jun 3, 2024
1 parent cbcc929 commit bd2a89f
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions stgpytools/types/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,18 @@ def __new__(cls, *args: SPathLike, **kwargs: Any) -> SPath:
...

def format(self, *args: Any, **kwargs: Any) -> SPath:
"""Format the path with the given arguments."""

return SPath(self.to_str().format(*args, **kwargs))

def to_str(self) -> str:
"""Cast the path to a string."""

return str(self)

def get_folder(self) -> SPath:
"""Get the folder of the path."""

folder_path = self.resolve()

if folder_path.is_dir():
Expand All @@ -77,9 +83,13 @@ def get_folder(self) -> SPath:
return SPath(path.dirname(folder_path))

def mkdirp(self) -> None:
"""Make the dir path with its parents."""

return self.get_folder().mkdir(parents=True, exist_ok=True)

def rmdirs(self, missing_ok: bool = False, ignore_errors: bool = True) -> None:
"""Remove the dir path with its contents."""

from shutil import rmtree

try:
Expand All @@ -91,12 +101,16 @@ def rmdirs(self, missing_ok: bool = False, ignore_errors: bool = True) -> None:
def read_lines(
self, encoding: str | None = None, errors: str | None = None, keepends: bool = False
) -> list[str]:
"""Read the file and return its lines."""

return super().read_text(encoding, errors).splitlines(keepends)

def write_lines(
self, data: Iterable[str], encoding: str | None = None,
errors: str | None = None, newline: str | None = None
) -> int:
"""Open the file and write the given lines."""

return super().write_text('\n'.join(data), encoding, errors, newline)


Expand Down

0 comments on commit bd2a89f

Please sign in to comment.