Skip to content

Commit

Permalink
read_piped, ansi_strip, improve docs
Browse files Browse the repository at this point in the history
  • Loading branch information
zigai committed Jul 21, 2024
1 parent cb0edfd commit d6e58f0
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 21 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "stdl"
version = "0.5.3"
version = "0.5.4"
description = "Extended Python Standard Library"
authors = [{ name = "Žiga Ivanšek", email = "[email protected]" }]
license = { file = "LICENSE.txt" }
Expand Down
2 changes: 1 addition & 1 deletion stdl/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from stdl import dt, fs, log, lst, net, st
from stdl import decorators, dt, fs, log, lst, net, st
36 changes: 17 additions & 19 deletions stdl/fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def __init__(
Args:
path (os.PathLike): File path.
encoding (str, optional): The file's encoding.
abs (bool, keyword-only): Whether to use the absolute path.
abs (bool): Whether to use the absolute path.
"""
self.encoding = encoding
self.path: str = os.fspath(path)
Expand Down Expand Up @@ -698,7 +698,9 @@ def is_wsl() -> bool:


def mkdir(path: str | Path, mode: int = 511, exist_ok: bool = True) -> None:
"""Creates a directory.
"""
Creates a directory.
Args:
path (str | Path): The path of the directory to create.
exist_ok (bool, optional): Whether to raise an exception if the directory already exists. Defaults to True.
Expand All @@ -707,7 +709,9 @@ def mkdir(path: str | Path, mode: int = 511, exist_ok: bool = True) -> None:


def mkdirs(dest: str | Path, names: list[str]) -> None:
"""Creates directories inside a destination directory.
"""
Creates directories inside a destination directory.
Args:
dest (str | Path): The destination directory.
names (list[str]): A list of directory names to be created in the destination directory.
Expand Down Expand Up @@ -863,7 +867,7 @@ def ensure_paths_exist(*args: str | PathLike | Iterable[str | PathLike]) -> None

def check_path(path: str | PathLike):
if not os.path.exists(os.fspath(path)):
raise FileNotFoundError(f"Path does not exist: {path}")
raise FileNotFoundError(f"Path does not exist: '{path}'")

for path in args:
if isinstance(path, (str, bytes, PathLike)):
Expand All @@ -886,7 +890,7 @@ def ensure_paths_dont_exist(*args: str | PathLike | Iterable[str | PathLike]) ->

def check_path(path: str | PathLike):
if os.path.exists(os.fspath(path)):
raise FileExistsError(f"Path already exists: {path}")
raise FileExistsError(f"Path already exists: '{path}'")

for path in args:
if isinstance(path, (str, bytes, PathLike)):
Expand Down Expand Up @@ -1017,22 +1021,16 @@ def exec_cmd(
)


def read_stdin(timeout: float = 0.0) -> list[str]:
def read_piped() -> str:
"""
Reads lines from stdin.
Args:
timeout (float, optional): The time to wait for input. Defaults to 0.
Returns:
list[str]: The lines read from stdin.
Reads piped input from stdin.
"""
if select([sys.stdin], [], [], timeout)[0]:
return sys.stdin.read().strip().splitlines()
return []
if sys.stdin.isatty():
return ""
return sys.stdin.read().strip()


def startfile(path: str | PathLike) -> None:
def start_file(path: str | PathLike) -> None:
"""
Open the file with your OS's default application.
Expand Down Expand Up @@ -1086,8 +1084,8 @@ def startfile(path: str | PathLike) -> None:
"dirname",
"joinpath",
"splitpath",
"read_stdin",
"startfile",
"start_file",
"read_piped",
"isdir",
"isfile",
"islink",
Expand Down
8 changes: 8 additions & 0 deletions stdl/st.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,14 @@ def ansi_rjust(s: str, width: int, fillchar: str = " ") -> str:
return s.rjust(new_width, fillchar)


def ansi_strip(r: str) -> str:
"""
Remove ANSI escape codes from a string.
"""
ansi_escape = re.compile(r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])")
return ansi_escape.sub("", r)


if __name__ == "__main__":
FG.print_all()
BG.print_all()
Expand Down

0 comments on commit d6e58f0

Please sign in to comment.