@@ -33,23 +33,26 @@ def check_uri(self, uri):
33
33
34
34
35
35
class Subject (object ):
36
- def __init__ (self , attrs = None ):
36
+ def __init__ (self , attrs = None , prefixes = None ):
37
37
"""
38
38
A subject of metadata statements.
39
39
40
40
attrs: an dictionary of key value pair attributes
41
41
"""
42
42
if attrs is None :
43
- attrs = []
43
+ attrs = {}
44
+ if prefixes is None :
45
+ prefixes = {}
44
46
self .attrs = attrs
47
+ self ._prefixes = prefixes
45
48
self ._prefix_suffix = re .compile ('(^(?:(?!__).)*)__((?!.*__).*$)' )
46
49
_http_p = 'http[s]?://.*'
47
50
self ._http_uri = re .compile ('{}' .format (_http_p ))
48
51
self ._http_uri_prefix = re .compile ('{}/|#' .format (_http_p ))
49
52
50
53
def prefixes (self ):
51
54
prefixes = {}
52
- for key , value in self .attrs .iteritems ():
55
+ for key , value in self ._prefixes .iteritems ():
53
56
if key .endswith ('__' ) and self ._http_uri_prefix .match (value ):
54
57
pref = key .rstrip ('__' )
55
58
if pref in prefixes :
@@ -105,17 +108,21 @@ def validate_netcdf(afilepath):
105
108
106
109
with load (afilepath ) as fhandle :
107
110
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 ()])
108
115
attrs = {}
109
116
for k in fhandle .ncattrs ():
110
117
attrs [k ] = getattr (fhandle , k )
111
- root_container = Subject (attrs )
118
+ root_container = Subject (attrs , prefixes = prefixes )
112
119
root_val = bv .ContainerValidation (subject = root_container ,
113
120
fhandle = fhandle )
114
121
sval .stored_exceptions += root_val .exceptions ()
115
122
for name in fhandle .variables :
116
123
sattrs = fhandle .__dict__ .copy ()
117
124
sattrs .update (fhandle .variables [name ].__dict__ .copy ())
118
- var = Subject (sattrs )
125
+ var = Subject (sattrs , prefixes = prefixes )
119
126
var_val = bv .ArrayValidation (name , fhandle .variables [name ], fhandle = fhandle ,
120
127
subject = var )
121
128
sval .stored_exceptions += var_val .exceptions ()
@@ -132,7 +139,11 @@ def validate_hdf5(afilepath):
132
139
with load (afilepath ) as fhandle :
133
140
sval = bv .StoredValidation ()
134
141
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 )
136
147
root_val = bv .ContainerValidation (subject = root_container ,
137
148
fhandle = fhandle )
138
149
sval .stored_exceptions += root_val .exceptions ()
@@ -144,7 +155,7 @@ def validate_hdf5(afilepath):
144
155
# #
145
156
sattrs = dict (fhandle .attrs ).copy ()
146
157
sattrs .update (dataset .attrs )
147
- dset = Subject (sattrs )
158
+ dset = Subject (sattrs , prefixes )
148
159
dset_val = bv .ArrayValidation (name , dataset , fhandle = fhandle ,
149
160
subject = dset )
150
161
sval .stored_exceptions += dset_val .exceptions ()
0 commit comments