Skip to content

Commit

Permalink
Add more io utils (#119)
Browse files Browse the repository at this point in the history
* Add more io utils

* update

* update

* update

* update

* Add more io utils

* update
  • Loading branch information
goodwanghan authored Oct 31, 2023
1 parent 456835e commit 46675cb
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
29 changes: 29 additions & 0 deletions tests/utils/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,29 @@ def test_join(tmpdir):
assert iou.join("dummy://a/", "b/", "*.parquet") == "dummy://a/b/*.parquet"


def test_glob(tmpdir):
def _assert(gres, expected):
for a, b in zip(gres, expected):
assert iou.exists(a) and iou.exists(b)

assert iou.glob(os.path.join(str(tmpdir), "a")) == []
assert iou.glob(os.path.join(str(tmpdir), "a", "*.txt")) == []
iou.touch(os.path.join(str(tmpdir), "a.txt"))
_assert(
iou.glob(os.path.join(str(tmpdir), "a.txt")),
[os.path.join(str(tmpdir), "a.txt")],
)
_assert(
iou.glob(os.path.join(str(tmpdir), "*.txt")),
[os.path.join(str(tmpdir), "a.txt")],
)
iou.touch(os.path.join(str(tmpdir), "a", "a.txt"), auto_mkdir=True)
_assert(
iou.glob(os.path.join(str(tmpdir), "a", "*.txt")),
[os.path.join(str(tmpdir), "a", "a.txt")],
)


@pytest.mark.skipif(sys.platform.startswith("win"), reason="not a test for windows")
def test_join_not_win():
assert iou.join("a", "b", "c/") == "a/b/c"
Expand Down Expand Up @@ -92,6 +115,12 @@ def test_touch(tmpdir):
assert iou.isfile(path)
assert iou.read_text(path) == ""

if not base.startswith("memory"):
pytest.raises(OSError, lambda: iou.touch(iou.join(base, "b", "c.txt")))

iou.touch(iou.join(base, "b", "c.txt"), auto_mkdir=True)
iou.exists(iou.join(base, "b", "c.txt"))


def test_rm(tmpdir):
path = os.path.join(str(tmpdir), "a.txt")
Expand Down
22 changes: 18 additions & 4 deletions triad/utils/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import zipfile
from contextlib import contextmanager
from pathlib import Path, PurePosixPath
from typing import Any, Iterator, Tuple
from typing import Any, Iterator, Tuple, List

import fsspec
import fsspec.core as fc
Expand Down Expand Up @@ -54,13 +54,17 @@ def isfile(path: str) -> bool:
return fs.isfile(path)


def touch(path: str) -> None:
def touch(path: str, auto_mkdir: bool = False) -> None:
"""Create an empty file or update the timestamp of the file
:param path: the file path
:param makedirs: if True, create the directory if not exists,
defaults to False
"""
fs, path = url_to_fs(path)
fs.touch(path, truncate=True)
fs, _path = url_to_fs(path)
if auto_mkdir:
fs.makedirs(fs._parent(_path), exist_ok=True)
fs.touch(_path, truncate=True)


def rm(path: str, recursive: bool = False) -> None:
Expand Down Expand Up @@ -105,6 +109,16 @@ def join(base_path: str, *paths: str) -> str:
return p + "://" + str(PurePosixPath(path).joinpath(*paths))


def glob(path: str) -> List[str]:
"""Glob files
:param path: the path to glob
:return: the matched files
"""
fs, path = url_to_fs(path)
return list(fs.glob(path))


def write_text(path: str, contents: str) -> None:
"""Write text to a file. If the directory of the file does not exist, it
will create the directory first
Expand Down

0 comments on commit 46675cb

Please sign in to comment.