Skip to content

Commit 1d2d892

Browse files
authored
refactor uses of UPath (#693)
1 parent e72f786 commit 1d2d892

File tree

9 files changed

+33
-49
lines changed

9 files changed

+33
-49
lines changed

webknossos/poetry.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

webknossos/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ zarr = "^2.11.0"
3131
numcodecs = "^0.9.1"
3232
zipp = "^3.5.0"
3333
fsspec = "^2022.2.0"
34-
universal-pathlib = "^0.0.14"
34+
universal-pathlib = "0.0.16"
3535

3636

3737
[tool.poetry.dev-dependencies]

webknossos/webknossos/dataset/_array.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,9 @@ def __init__(self, path: Path):
141141

142142
@classmethod
143143
def open(cls, path: Path) -> "WKWArray":
144-
if (path / "header.wkw").is_file():
144+
header_path = path / "header.wkw"
145+
146+
if header_path.exists() and header_path.is_file():
145147
return cls(path)
146148
raise ArrayException(
147149
f"Could not open WKW array at {path}. `header.wkw` not found."
@@ -288,7 +290,9 @@ def __init__(self, path: Path):
288290

289291
@classmethod
290292
def open(cls, path: Path) -> "ZarrArray":
291-
if (path / ".zarray").is_file():
293+
zarray_path = path / ".zarray"
294+
295+
if zarray_path.exists() and zarray_path.is_file():
292296
return cls(path)
293297
raise ArrayException(
294298
f"Could not open Zarr array at {path}. `.zarray` not found."

webknossos/webknossos/dataset/dataset.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import attr
2424
import numpy as np
2525
from boltons.typeutils import make_sentinel
26+
from upath import UPath
2627

2728
from ..geometry.vec3_int import Vec3IntLike
2829
from ._array import ArrayException, ArrayInfo, BaseArray, DataFormat
@@ -36,8 +37,6 @@
3637
copytree,
3738
get_executor_for_args,
3839
is_fs_path,
39-
is_symlink,
40-
make_upath,
4140
rmtree,
4241
)
4342
from ._utils.infer_bounding_box_existing_files import infer_bounding_box_existing_files
@@ -124,7 +123,7 @@ def __init__(
124123
Currently exist_ok=True is the deprecated default and will change in future releases.
125124
Please use `Dataset.open` if you intend to open an existing dataset and don't want/need the creation behavior.
126125
"""
127-
dataset_path = make_upath(dataset_path)
126+
dataset_path = UPath(dataset_path)
128127

129128
dataset_existed_already = (
130129
dataset_path.exists()
@@ -183,7 +182,7 @@ def __init__(
183182
for layer_properties in self._properties.data_layers:
184183
num_channels = _extract_num_channels(
185184
layer_properties.num_channels,
186-
make_upath(dataset_path),
185+
UPath(dataset_path),
187186
layer_properties.name,
188187
layer_properties.mags[0].mag
189188
if len(layer_properties.mags) > 0
@@ -222,7 +221,7 @@ def open(cls, dataset_path: Union[str, PathLike]) -> "Dataset":
222221
223222
The `dataset_path` refers to the top level directory of the dataset (excluding layer or magnification names).
224223
"""
225-
dataset_path = make_upath(dataset_path)
224+
dataset_path = UPath(dataset_path)
226225
assert (
227226
dataset_path.exists()
228227
), f"Cannot open Dataset: Couldn't find {dataset_path}"
@@ -648,7 +647,7 @@ def delete_layer(self, layer_name: str) -> None:
648647
layer for layer in self._properties.data_layers if layer.name != layer_name
649648
]
650649
# delete files on disk
651-
if is_symlink(layer_path):
650+
if layer_path.is_symlink():
652651
layer_path.unlink()
653652
else:
654653
# rmtree does not recurse into linked dirs, but removes the link
@@ -674,7 +673,7 @@ def add_symlink_layer(
674673
if isinstance(foreign_layer, Layer):
675674
foreign_layer_path = foreign_layer.path
676675
else:
677-
foreign_layer_path = make_upath(foreign_layer)
676+
foreign_layer_path = UPath(foreign_layer)
678677

679678
foreign_layer_name = foreign_layer_path.name
680679
layer_name = (
@@ -726,7 +725,7 @@ def add_copy_layer(
726725
if isinstance(foreign_layer, Layer):
727726
foreign_layer_path = foreign_layer.path
728727
else:
729-
foreign_layer_path = make_upath(foreign_layer)
728+
foreign_layer_path = UPath(foreign_layer)
730729

731730
foreign_layer_name = foreign_layer_path.name
732731
layer_name = (
@@ -778,7 +777,7 @@ def copy_dataset(
778777
file_len=file_len,
779778
)
780779

781-
new_dataset_path = make_upath(new_dataset_path)
780+
new_dataset_path = UPath(new_dataset_path)
782781

783782
if data_format == DataFormat.WKW:
784783
assert is_fs_path(

webknossos/webknossos/dataset/layer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, Union
1010

1111
import numpy as np
12+
from upath import UPath
1213

1314
from webknossos.geometry import BoundingBox, Mag, Vec3Int, Vec3IntLike
1415

@@ -40,7 +41,6 @@
4041
copytree,
4142
get_executor_for_args,
4243
is_fs_path,
43-
make_upath,
4444
named_partial,
4545
rmtree,
4646
warn_deprecated,
@@ -495,7 +495,7 @@ def _add_foreign_mag(
495495
# local import to prevent circular dependency
496496
from .dataset import Dataset
497497

498-
foreign_mag_view_path = make_upath(foreign_mag_view_or_path)
498+
foreign_mag_view_path = UPath(foreign_mag_view_or_path)
499499
foreign_mag_view = (
500500
Dataset.open(foreign_mag_view_path.parent.parent)
501501
.get_layer(foreign_mag_view_path.parent.name)

webknossos/webknossos/dataset/mag_view.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,10 @@
66
from uuid import uuid4
77

88
import numpy as np
9+
from upath import UPath
910

1011
from ..geometry import BoundingBox, Mag, Vec3Int, Vec3IntLike
11-
from ..utils import (
12-
get_executor_for_args,
13-
is_fs_path,
14-
make_upath,
15-
rmtree,
16-
wait_and_ensure_success,
17-
)
12+
from ..utils import get_executor_for_args, is_fs_path, rmtree, wait_and_ensure_success
1813
from ._array import ArrayInfo, BaseArray
1914
from .properties import MagViewProperties
2015

@@ -284,7 +279,7 @@ def compress(
284279
self.path
285280
), "Cannot compress a remote mag without `target_path`."
286281
else:
287-
target_path = make_upath(target_path)
282+
target_path = UPath(target_path)
288283

289284
uncompressed_full_path = self.path
290285
compressed_dataset_path = (

webknossos/webknossos/utils.py

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from datetime import datetime
1212
from inspect import getframeinfo, stack
1313
from multiprocessing import cpu_count
14-
from os import PathLike
1514
from os.path import relpath
1615
from pathlib import Path
1716
from shutil import copyfileobj
@@ -213,34 +212,21 @@ def warn_deprecated(deprecated_item: str, alternative_item: str) -> None:
213212
)
214213

215214

216-
def make_upath(maybe_path: Union[str, PathLike, Path]) -> UPath:
217-
return maybe_path if isinstance(maybe_path, UPath) else UPath(maybe_path)
218-
219-
220215
def is_fs_path(path: Path) -> bool:
221-
# Distinguish between `pathlib.*Path` and `UPath` through a `UPath`-specific attribute
222-
return not hasattr(path, "_url")
223-
224-
225-
def is_symlink(path: Path) -> bool:
226-
try:
227-
return path.is_symlink()
228-
except NotImplementedError:
229-
# `Path` raises `NotImplmentedError` for some methods, including `is_symlink`
230-
return False
216+
return not isinstance(path, UPath)
231217

232218

233219
def rmtree(path: Path) -> None:
234220
def _walk(path: Path) -> Iterator[Path]:
235221
if path.exists():
236-
if path.is_dir() and not is_symlink(path):
222+
if path.is_dir() and not path.is_symlink():
237223
for p in path.iterdir():
238224
yield from _walk(p)
239225
yield path
240226

241227
for sub_path in _walk(path):
242228
try:
243-
if sub_path.is_file() or is_symlink(sub_path):
229+
if sub_path.is_file() or sub_path.is_symlink():
244230
sub_path.unlink()
245231
elif sub_path.is_dir():
246232
sub_path.rmdir()

wkcuber/poetry.lock

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

wkcuber/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ tifffile = "^2020.11.26"
2929
webknossos = { path = "../webknossos/", develop = true }
3030
wkw = "1.1.11"
3131
zarr = "^2.11.0"
32-
universal-pathlib = "^0.0.14"
32+
universal-pathlib = "0.0.16"
3333

3434
[tool.poetry.dev-dependencies]
3535
black = "^20.8b1"

0 commit comments

Comments
 (0)