From bd2a89f395d572611feec860375ba9e833ab31b3 Mon Sep 17 00:00:00 2001 From: LightArrowsEXE Date: Mon, 3 Jun 2024 04:29:02 -0700 Subject: [PATCH] SPath: Add some simple docstrings (#5) --- stgpytools/types/file.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/stgpytools/types/file.py b/stgpytools/types/file.py index a96693f..9a1c261 100644 --- a/stgpytools/types/file.py +++ b/stgpytools/types/file.py @@ -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(): @@ -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: @@ -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)