Skip to content

Commit

Permalink
restrict invalid attribute access for RDF accessor
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasprobst committed Jun 26, 2024
1 parent 7bd0fe7 commit ecfd63d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
16 changes: 11 additions & 5 deletions h5rdmtoolbox/wrapper/rdf.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
"""RDF (Resource Description Framework) module for use with HDF5 files"""
import warnings

import abc
from typing import Dict, Union, Optional, List

import h5py
import pydantic
import warnings
from pydantic import HttpUrl
from typing import Dict, Union, Optional, List

from . import lazy
from ..protocols import H5TbxAttributeManager
Expand Down Expand Up @@ -265,6 +263,14 @@ def __str__(self) -> str:
def __repr__(self) -> str:
return self.__str__()

def __setattr__(self, key, value):
if key not in ('_attr',
'subject',
'predicate',
'type'):
raise KeyError(f"Cannot set {key}. Only subject, predicate and type can be set!")
super().__setattr__(key, value)

@property
def parent(self):
"""Return the parent object"""
Expand Down Expand Up @@ -532,4 +538,4 @@ def delete(self, name):
if name in self.predicate:
del self.predicate[name]
if name in self.object:
del self.object[name]
del self.object[name]
2 changes: 2 additions & 0 deletions tests/wrapper/test_rdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ def test_RDF(self):
Attribute('0000-0001-8729-0482', rdf_predicate='000-0001-8729-0482')

with h5tbx.File(mode='w') as h5:
with self.assertRaises(KeyError):
h5.rdf.type123 = 'https://example.org/validURI'
with self.assertRaises(KeyError):
h5.rdf['not_existing'].predicate = 'https://example.org/notExisting'
h5.attrs['orcid', M4I.orcidId] = rdfobj
Expand Down

0 comments on commit ecfd63d

Please sign in to comment.