Skip to content

Commit 3d2f18f

Browse files
committed
When copying allow forceDirectory to override
Only overrides if the state is not known.
1 parent 7aab88b commit 3d2f18f

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

python/lsst/resources/_resourcePath.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -211,23 +211,30 @@ def __new__(
211211
# We invoke __new__ again with str(self) to add a scheme for
212212
# forceAbsolute, but for the others that seems more likely to paper
213213
# over logic errors than do something useful, so we just raise.
214-
if forceDirectory and not uri.isdir():
214+
if forceDirectory is not None and uri.dirLike is not None and forceDirectory is not uri.dirLike:
215+
# Can not force a file-like URI to become a dir-like one or
216+
# vice versa.
215217
raise RuntimeError(
216-
f"{uri} is already a file-like ResourcePath; cannot force it to directory."
218+
f"{uri} can not be forced to change directory vs file state when previously declared."
217219
)
218220
if isTemporary is not None and isTemporary is not uri.isTemporary:
219221
raise RuntimeError(
220222
f"{uri} is already a {'temporary' if uri.isTemporary else 'permanent'} "
221223
f"ResourcePath; cannot make it {'temporary' if isTemporary else 'permanent'}."
222224
)
225+
223226
if forceAbsolute and not uri.scheme:
227+
# Create new absolute from relative.
224228
return ResourcePath(
225229
str(uri),
226230
root=root,
227-
forceAbsolute=True,
228-
forceDirectory=uri.dirLike,
231+
forceAbsolute=forceAbsolute,
232+
forceDirectory=forceDirectory or uri.dirLike,
229233
isTemporary=uri.isTemporary,
230234
)
235+
elif forceDirectory is not None and uri.dirLike is None:
236+
# Clone but with a new dirLike status.
237+
return uri.replace(forceDirectory=forceDirectory)
231238
return uri
232239
else:
233240
raise ValueError(

python/lsst/resources/tests.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,14 @@ def test_creation(self) -> None:
225225
with self.assertRaises(RuntimeError):
226226
ResourcePath(self.root_uri, isTemporary=True)
227227

228-
file = self.root_uri.join("file.txt")
228+
file = self.root_uri.join("file.txt", forceDirectory=False)
229229
with self.assertRaises(RuntimeError):
230230
ResourcePath(file, forceDirectory=True)
231231

232+
file = self.root_uri.join("file.txt")
233+
file_as_dir = ResourcePath(file, forceDirectory=True)
234+
self.assertTrue(file_as_dir.isdir())
235+
232236
dir = self._make_uri("a/b/c/")
233237
with self.assertRaises(ValueError):
234238
ResourcePath(dir, forceDirectory=False)

0 commit comments

Comments
 (0)