Skip to content

Commit 97a6b0b

Browse files
committed
prefix container
1 parent dcfa808 commit 97a6b0b

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

lib/bald/__init__.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,26 @@ def check_uri(self, uri):
3333

3434

3535
class Subject(object):
36-
def __init__(self, attrs=None):
36+
def __init__(self, attrs=None, prefixes=None):
3737
"""
3838
A subject of metadata statements.
3939
4040
attrs: an dictionary of key value pair attributes
4141
"""
4242
if attrs is None:
43-
attrs = []
43+
attrs = {}
44+
if prefixes is None:
45+
prefixes = {}
4446
self.attrs = attrs
47+
self._prefixes = prefixes
4548
self._prefix_suffix = re.compile('(^(?:(?!__).)*)__((?!.*__).*$)')
4649
_http_p = 'http[s]?://.*'
4750
self._http_uri = re.compile('{}'.format(_http_p))
4851
self._http_uri_prefix = re.compile('{}/|#'.format(_http_p))
4952

5053
def prefixes(self):
5154
prefixes = {}
52-
for key, value in self.attrs.iteritems():
55+
for key, value in self._prefixes.iteritems():
5356
if key.endswith('__') and self._http_uri_prefix.match(value):
5457
pref = key.rstrip('__')
5558
if pref in prefixes:
@@ -105,17 +108,21 @@ def validate_netcdf(afilepath):
105108

106109
with load(afilepath) as fhandle:
107110
sval = bv.StoredValidation()
111+
prefix_group = fhandle[fhandle.bald__prefixes] if hasattr(fhandle, 'bald__prefixes') else {}
112+
prefixes = {}
113+
if prefix_group:
114+
prefixes = dict([(prefix, getattr(prefix_group, prefix)) for prefix in prefix_group.ncattrs()])
108115
attrs = {}
109116
for k in fhandle.ncattrs():
110117
attrs[k] = getattr(fhandle, k)
111-
root_container = Subject(attrs)
118+
root_container = Subject(attrs, prefixes=prefixes)
112119
root_val = bv.ContainerValidation(subject=root_container,
113120
fhandle=fhandle)
114121
sval.stored_exceptions += root_val.exceptions()
115122
for name in fhandle.variables:
116123
sattrs = fhandle.__dict__.copy()
117124
sattrs.update(fhandle.variables[name].__dict__.copy())
118-
var = Subject(sattrs)
125+
var = Subject(sattrs, prefixes=prefixes)
119126
var_val = bv.ArrayValidation(name, fhandle.variables[name], fhandle=fhandle,
120127
subject=var)
121128
sval.stored_exceptions += var_val.exceptions()
@@ -132,7 +139,11 @@ def validate_hdf5(afilepath):
132139
with load(afilepath) as fhandle:
133140
sval = bv.StoredValidation()
134141
cache = {}
135-
root_container = Subject(fhandle.attrs)
142+
prefix_group = fhandle.attrs.get('bald__prefixes')
143+
prefixes = {}
144+
if prefix_group:
145+
prefixes = fhandle[prefix_group].attrs
146+
root_container = Subject(fhandle.attrs, prefixes=prefixes)
136147
root_val = bv.ContainerValidation(subject=root_container,
137148
fhandle=fhandle)
138149
sval.stored_exceptions += root_val.exceptions()
@@ -144,7 +155,7 @@ def validate_hdf5(afilepath):
144155
# #
145156
sattrs = dict(fhandle.attrs).copy()
146157
sattrs.update(dataset.attrs)
147-
dset = Subject(sattrs)
158+
dset = Subject(sattrs, prefixes)
148159
dset_val = bv.ArrayValidation(name, dataset, fhandle=fhandle,
149160
subject=dset)
150161
sval.stored_exceptions += dset_val.exceptions()

lib/bald/tests/integration/test_netcdf.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
from bald.tests import BaldTestCase
99

1010
def _fattrs(f):
11-
f.bald__ = 'http://binary-array-ld.net/latest/'
12-
f.rdf__ = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'
1311
f.rdf__type = 'bald__Container'
12+
group_pref = f.createGroup('bald__prefix_list')
13+
group_pref.bald__ = 'http://binary-array-ld.net/latest/'
14+
group_pref.rdf__ = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'
15+
f.bald__prefixes = 'bald__prefix_list'
1416
return f
1517

1618
def _create_parent_child(f, pshape, cshape):

lib/bald/tests/integration/test_validation.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
from bald.tests import BaldTestCase
88

99
def _fattrs(f):
10-
f.attrs['bald__'] = 'http://binary-array-ld.net/latest/'
11-
f.attrs['rdf__'] = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'
1210
f.attrs['rdf__type'] = 'bald__Container'
11+
group_pref = f.create_group('bald__prefix_list')
12+
group_pref.attrs['bald__'] = 'http://binary-array-ld.net/latest/'
13+
group_pref.attrs['rdf__'] = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'
14+
f.attrs['bald__prefixes'] = group_pref.ref
1315
return f
1416

1517
def _create_parent_child(f, pshape, cshape):

0 commit comments

Comments
 (0)