Skip to content

Commit

Permalink
raise exception when trying to modify file open in read only mode
Browse files Browse the repository at this point in the history
  • Loading branch information
jreadey committed Feb 14, 2018
1 parent 16c4b85 commit 2dee273
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
2 changes: 0 additions & 2 deletions h5pyd/_hl/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ def create_group(self, h5path):
Name may be absolute or relative. Fails if the target name already
exists.
"""
if self.id.http_conn.mode == 'r':
raise ValueError("Unable to create group (No write intent on file)")

#if self.__contains__(name):
# raise ValueError("Unable to create link (Name alredy exists)")
Expand Down
16 changes: 16 additions & 0 deletions h5pyd/_hl/httpconn.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ def PUT(self, req, body=None, format="json", params=None, headers=None):
params["domain"] = self._domain
if self._api_key:
params["api_key"] = self._api_key

# verify the file was open for modification
if self._mode == 'r':
raise ValueError("Unable to create group (No write intent on file)")

req = self._endpoint + req

Expand Down Expand Up @@ -246,6 +250,14 @@ def POST(self, req, body=None, format="json", params=None, headers=None):
if self._api_key:
params["api_key"] = self._api_key

# verify we have write intent (unless this is a dataset point selection)
if req.startswith("/datasets/") and req.endswith("/value"):
point_sel = True
else:
point_sel = False
if self._mode == 'r' and not point_sel:
raise ValueError("Unable perform request (No write intent on file)")

# try to do a POST to the domain
req = self._endpoint + req

Expand Down Expand Up @@ -290,6 +302,10 @@ def DELETE(self, req, params=None, headers=None):
if self._api_key:
params["api_key"] = self._api_key

# verify we have write intent
if self._mode == 'r':
raise ValueError("Unable perform request (No write intent on file)")

# try to do a DELETE of the resource
req = self._endpoint + req

Expand Down
10 changes: 9 additions & 1 deletion test/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,19 @@ def test_create(self):
self.assertTrue(isinstance(r, h5py.Group))
self.assertEqual(len(f.attrs.keys()), 0)

# verify that trying to modify the file fails
try:
f.create_group("another_subgrp")
self.assertTrue(False) # expect exception
except ValueError:
except ValueError as ve:
pass

try:
f.attrs["foo"] = "bar"
self.assertTrue(False) # expect exception
except ValueError as ve:
pass

self.assertEqual(len(f.keys()), 1)

if h5py.__name__ == "h5pyd" and not is_hsds:
Expand Down
3 changes: 1 addition & 2 deletions test/test_folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def test_list(self):


filepath = self.getPathFromDomain(test_domain)
print("test_domain:", filepath)
print(filepath)
# create test file if not present.
# on first run, this may take a minute before it is visible as a folder item
f = h5py.File(filepath, mode='a')
Expand Down Expand Up @@ -126,7 +126,6 @@ def test_create_folder(self):
folder_test = self.getFileName("create_folder_test")
folder_path = self.getPathFromDomain(folder_test) + '/'

print("folder_path", folder_path)
dir = h5py.Folder(folder_path, mode='w') # create a new folder
dir.close()
# re-open
Expand Down

0 comments on commit 2dee273

Please sign in to comment.