diff --git a/pyproject.toml b/pyproject.toml index a28ed91..d717acb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 = "ziga.ivansek@gmail.com" }] license = { file = "LICENSE.txt" } diff --git a/stdl/__init__.py b/stdl/__init__.py index 1e1ef24..ba48699 100644 --- a/stdl/__init__.py +++ b/stdl/__init__.py @@ -1 +1 @@ -from stdl import dt, fs, log, lst, net, st +from stdl import decorators, dt, fs, log, lst, net, st diff --git a/stdl/fs.py b/stdl/fs.py index 25a7534..2a266df 100644 --- a/stdl/fs.py +++ b/stdl/fs.py @@ -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) @@ -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. @@ -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. @@ -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)): @@ -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)): @@ -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. @@ -1086,8 +1084,8 @@ def startfile(path: str | PathLike) -> None: "dirname", "joinpath", "splitpath", - "read_stdin", - "startfile", + "start_file", + "read_piped", "isdir", "isfile", "islink", diff --git a/stdl/st.py b/stdl/st.py index 03ee22b..e9cb5af 100644 --- a/stdl/st.py +++ b/stdl/st.py @@ -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()