From 0a9404fb2f6a908e9469179433e78eb2ec9c3e61 Mon Sep 17 00:00:00 2001 From: Marc Beninca Date: Fri, 13 Sep 2024 17:19:33 +0200 Subject: [PATCH 1/8] fs/unlink --- rwx/fs/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rwx/fs/__init__.py b/rwx/fs/__init__.py index 514fcec..a7cc5b0 100644 --- a/rwx/fs/__init__.py +++ b/rwx/fs/__init__.py @@ -2,6 +2,7 @@ import os import shutil +from pathlib import Path from rwx import ps @@ -69,7 +70,7 @@ def wipe(path: str) -> None: try: shutil.rmtree(path) except NotADirectoryError: - os.remove(path) + Path(path).unlink(missing_ok=True) except FileNotFoundError: pass From ef45fc9d810000da64ab8ad9f309906ff8c5db86 Mon Sep 17 00:00:00 2001 From: Marc Beninca Date: Fri, 13 Sep 2024 17:22:36 +0200 Subject: [PATCH 2/8] fs/open --- rwx/fs/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rwx/fs/__init__.py b/rwx/fs/__init__.py index a7cc5b0..cb97ceb 100644 --- a/rwx/fs/__init__.py +++ b/rwx/fs/__init__.py @@ -77,5 +77,5 @@ def wipe(path: str) -> None: def write(file_path: str, text: str, charset: str = CHARSET) -> None: """Write text into a file.""" - with open(file_path, "bw") as file_object: - file_object.write(text.encode(charset)) + with Path(file_path).open(mode="w", encoding=charset) as file_object: + file_object.write(text) From de80d7ecff21069f2a0c5bfd960fbb17e35eefa3 Mon Sep 17 00:00:00 2001 From: Marc Beninca Date: Fri, 13 Sep 2024 17:24:37 +0200 Subject: [PATCH 3/8] fs/mkdir --- rwx/fs/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rwx/fs/__init__.py b/rwx/fs/__init__.py index cb97ceb..1037fd0 100644 --- a/rwx/fs/__init__.py +++ b/rwx/fs/__init__.py @@ -49,7 +49,7 @@ def get_path_uuid(path: str) -> str: def make_directory(path: str) -> None: """Make a directory (and its parents) from a path.""" - os.makedirs(path, exist_ok=True) + Path(path).mkdir(exist_ok=True, parents=True) def read_file(file_path: str): @@ -77,5 +77,5 @@ def wipe(path: str) -> None: def write(file_path: str, text: str, charset: str = CHARSET) -> None: """Write text into a file.""" - with Path(file_path).open(mode="w", encoding=charset) as file_object: + with Path(file_path).open(encoding=charset, mode="w") as file_object: file_object.write(text) From cfa9ca9e3c15e33114c051b20d52ead57dd17d17 Mon Sep 17 00:00:00 2001 From: Marc Beninca Date: Fri, 13 Sep 2024 17:39:39 +0200 Subject: [PATCH 4/8] prj/Path --- rwx/prj/__init__.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/rwx/prj/__init__.py b/rwx/prj/__init__.py index 6a3e059..8b70a4a 100644 --- a/rwx/prj/__init__.py +++ b/rwx/prj/__init__.py @@ -1,6 +1,7 @@ """Handle projects.""" -from os import path +from os.path import realpath +from pathlib import Path class Project: @@ -8,6 +9,6 @@ class Project: def __init__(self, file_path: str) -> None: """Set file, root & name.""" - self.file: str = path.realpath(file_path) - self.root: str = path.dirname(self.file) - self.name: str = path.basename(self.root) + self.file: str = realpath(file_path) + self.root: str = Path(self.file).parent + self.name: str = self.root.name From 5e3c8e93ff9705afce27a06a1083c565466b219b Mon Sep 17 00:00:00 2001 From: Marc Beninca Date: Fri, 13 Sep 2024 17:41:49 +0200 Subject: [PATCH 5/8] sphinx/ --- rwx/prj/sphinx.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/rwx/prj/sphinx.py b/rwx/prj/sphinx.py index 13252f8..b85cfa5 100644 --- a/rwx/prj/sphinx.py +++ b/rwx/prj/sphinx.py @@ -1,7 +1,5 @@ """Project consisting only of a Sphinx documentation.""" -from os import path - from sphinx.cmd.build import build_main from rwx.fs import wipe @@ -17,7 +15,7 @@ class SphinxProject(Project): def build(self) -> None: """Build the project.""" - output_root: str = path.join(self.root, "out") + output_root: str = self.root / "out" wipe(output_root) arguments: list[str] = [ "-E", @@ -34,7 +32,7 @@ class SphinxProject(Project): "-c", self.root, # "-C", - path.join(self.root, self.name), - path.join(output_root, "web"), + self.root / self.name, + output_root / "web", ] build_main(arguments) From 065d647e517135cf82fa8ee3c48076b5643d11ef Mon Sep 17 00:00:00 2001 From: Marc Beninca Date: Fri, 13 Sep 2024 18:55:31 +0200 Subject: [PATCH 6/8] read/bytes --- rwx/fs/__init__.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/rwx/fs/__init__.py b/rwx/fs/__init__.py index 1037fd0..b1da3a2 100644 --- a/rwx/fs/__init__.py +++ b/rwx/fs/__init__.py @@ -52,8 +52,9 @@ def make_directory(path: str) -> None: Path(path).mkdir(exist_ok=True, parents=True) -def read_file(file_path: str): - with open(file_path, "br") as file_object: +def read_file_bytes(file_path: str) -> bytes: + """Read whole file bytes.""" + with Path(file_path).open("br") as file_object: return file_object.read() @@ -62,7 +63,7 @@ def read_file_lines(file_path: str, charset: str = CHARSET): def read_file_text(file_path: str, charset: str = CHARSET): - return read_file(file_path).decode(charset) + return read_file_bytes(file_path).decode(charset) def wipe(path: str) -> None: From b3e66ec1c3f21b672cc26ddd540e1343f3b57cda Mon Sep 17 00:00:00 2001 From: Marc Beninca Date: Fri, 13 Sep 2024 18:57:49 +0200 Subject: [PATCH 7/8] read/text --- rwx/fs/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rwx/fs/__init__.py b/rwx/fs/__init__.py index b1da3a2..82b7c16 100644 --- a/rwx/fs/__init__.py +++ b/rwx/fs/__init__.py @@ -62,7 +62,8 @@ def read_file_lines(file_path: str, charset: str = CHARSET): return read_file_text(file_path).split(os.linesep) -def read_file_text(file_path: str, charset: str = CHARSET): +def read_file_text(file_path: str, charset: str = CHARSET) -> str: + """Read whole file text.""" return read_file_bytes(file_path).decode(charset) From 78dd431ca5fd969841774f9deb4129d9eb299c7f Mon Sep 17 00:00:00 2001 From: Marc Beninca Date: Fri, 13 Sep 2024 18:58:57 +0200 Subject: [PATCH 8/8] read/lines --- rwx/fs/__init__.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/rwx/fs/__init__.py b/rwx/fs/__init__.py index 82b7c16..a8865e7 100644 --- a/rwx/fs/__init__.py +++ b/rwx/fs/__init__.py @@ -58,8 +58,9 @@ def read_file_bytes(file_path: str) -> bytes: return file_object.read() -def read_file_lines(file_path: str, charset: str = CHARSET): - return read_file_text(file_path).split(os.linesep) +def read_file_lines(file_path: str, charset: str = CHARSET) -> list[str]: + """Read whole file lines.""" + return read_file_text(file_path, charset).split(os.linesep) def read_file_text(file_path: str, charset: str = CHARSET) -> str: