@@ -37,7 +37,8 @@ def check_uri(self, uri):
37
37
38
38
39
39
class Subject (object ):
40
- def __init__ (self , attrs = None , prefixes = None , aliases = None ):
40
+ def __init__ (self , attrs = None , prefixes = None , aliases = None ,
41
+ rdftype = 'bald__Subject' ):
41
42
"""
42
43
A subject of metadata statements.
43
44
@@ -50,13 +51,35 @@ def __init__(self, attrs=None, prefixes=None, aliases=None):
50
51
if aliases is None :
51
52
aliases = {}
52
53
self .attrs = attrs
54
+ if 'rdf__type' not in attrs :
55
+ attrs ['rdf__type' ] = rdftype
56
+ elif rdftype not in attrs ['rdf__type' ]:
57
+ if isinstance (attrs ['rdf__type' ], list ):
58
+ attrs ['rdf__type' ].append (rdftype )
59
+ elif isinstance (attrs ['rdf__type' ], set ):
60
+ attrs ['rdf__type' ].add (rdftype )
61
+ else :
62
+ attrs ['rdf__type' ] = set ((attrs ['rdf__type' ], rdftype ))
63
+
53
64
self .aliases = aliases
54
65
self ._prefixes = prefixes
55
66
self ._prefix_suffix = re .compile ('(^(?:(?!__).)*)__((?!.*__).*$)' )
56
67
_http_p = 'http[s]?://.*'
57
68
self ._http_uri = re .compile ('{}' .format (_http_p ))
58
69
self ._http_uri_prefix = re .compile ('{}/|#' .format (_http_p ))
59
70
71
+ def __str__ (self ):
72
+ return '{}: {}' .format (type (self ), self .attrs )
73
+
74
+ def __repr__ (self ):
75
+ return str (self )
76
+
77
+ def __getattr__ (self , attr ):
78
+ if attr not in self .attrs :
79
+ msg = '{} object has no attribute {}' .format (type (self ), attr )
80
+ raise AttributeError (msg )
81
+ return self .attrs [attr ]
82
+
60
83
def prefixes (self ):
61
84
prefixes = {}
62
85
for key , value in self ._prefixes .iteritems ():
@@ -80,7 +103,30 @@ def unpack_uri(self, astring):
80
103
result = self .aliases [astring ]
81
104
return result
82
105
106
+ def graph (self ):
107
+ graph = rdflib .Graph ()
108
+
109
+ return graph
110
+
111
+ class Array (Subject ):
112
+ def __init__ (self , attrs = None , prefixes = None , aliases = None ,
113
+ shape = None ):
114
+ self .shape = shape
115
+ rdftype = 'bald__Array'
116
+ super (Array , self ).__init__ (attrs , prefixes , aliases , rdftype )
83
117
118
+ @property
119
+ def array_references (self ):
120
+ return self .attrs .get ('bald__references' , [])
121
+ #return []
122
+
123
+ class Container (Subject ):
124
+ def __init__ (self , attrs = None , prefixes = None , aliases = None ,
125
+ shape = None ):
126
+ rdftype = 'bald__Container'
127
+ super (Container , self ).__init__ (attrs , prefixes , aliases , rdftype )
128
+
129
+
84
130
@contextlib .contextmanager
85
131
def load (afilepath ):
86
132
if afilepath .endswith ('.hdf' ):
@@ -108,78 +154,136 @@ def load(afilepath):
108
154
finally :
109
155
f .close ()
110
156
111
-
112
- def validate_netcdf (afilepath ):
157
+ def load_netcdf (afilepath ):
113
158
"""
114
- Validate a file with respect to binarry -array-linked-data.
159
+ Validate a file with respect to binary -array-linked-data.
115
160
Returns a :class:`bald.validation.Validation`
116
161
"""
117
162
118
163
with load (afilepath ) as fhandle :
119
- sval = bv . StoredValidation ()
120
- prefix_group = fhandle [ fhandle . bald__isPrefixedBy ] if hasattr (fhandle , 'bald__isPrefixedBy' ) else {}
164
+ prefix_group = ( fhandle [ fhandle . bald__isPrefixedBy ] if
165
+ hasattr (fhandle , 'bald__isPrefixedBy' ) else {})
121
166
prefixes = {}
122
167
if prefix_group :
123
- prefixes = dict ([(prefix , getattr (prefix_group , prefix )) for prefix in prefix_group .ncattrs ()])
168
+ prefixes = (dict ([(prefix , getattr (prefix_group , prefix )) for
169
+ prefix in prefix_group .ncattrs ()]))
124
170
else :
125
171
for k in fhandle .ncattrs ():
126
172
if k .endswith ('__' ):
127
173
prefixes [k ] = getattr (fhandle , k )
128
- alias_group = fhandle [fhandle .bald__isAliasedBy ] if hasattr (fhandle , 'bald__isAliasedBy' ) else {}
174
+ alias_group = (fhandle [fhandle .bald__isAliasedBy ]
175
+ if hasattr (fhandle , 'bald__isAliasedBy' ) else {})
129
176
aliases = {}
130
177
if alias_group :
131
- aliases = dict ([(alias , getattr (alias_group , alias )) for alias in alias_group .ncattrs ()])
178
+ aliases = (dict ([(alias , getattr (alias_group , alias ))
179
+ for alias in alias_group .ncattrs ()]))
132
180
133
181
attrs = {}
134
182
for k in fhandle .ncattrs ():
135
183
attrs [k ] = getattr (fhandle , k )
136
- root_container = Subject (attrs , prefixes = prefixes , aliases = aliases )
137
- root_val = bv .ContainerValidation (subject = root_container ,
138
- fhandle = fhandle )
139
- sval .stored_exceptions += root_val .exceptions ()
184
+ # It would be nice to use the URI of the file if it is known.
185
+ attrs ['@id' ] = 'root'
186
+ root_container = Container (attrs , prefixes = prefixes ,
187
+ aliases = aliases )
188
+ root_container .attrs ['bald__contains' ] = []
189
+
140
190
for name in fhandle .variables :
141
- sattrs = fhandle .__dict__ .copy ()
142
- sattrs .update (fhandle .variables [name ].__dict__ .copy ())
143
- var = Subject (sattrs , prefixes = prefixes , aliases = aliases )
144
- var_val = bv .ArrayValidation (name , fhandle .variables [name ], fhandle = fhandle ,
145
- subject = var )
146
- sval .stored_exceptions += var_val .exceptions ()
191
+
192
+ sattrs = fhandle .variables [name ].__dict__ .copy ()
193
+ sattrs ['@id' ] = '/{}' .format (name )
194
+
195
+ if 'bald__references' in fhandle .variables [name ].ncattrs ():
196
+ child_dset = fhandle .variables [fhandle .variables [name ].bald__references ]
197
+ cattrs = child_dset .__dict__ .copy ()
198
+ cattrs ['@id' ] = '/{}' .format (child_dset .name )
199
+ carray = Array (cattrs , prefixes , aliases , child_dset .shape )
200
+ sattrs ['bald__references' ] = [carray ]
201
+
202
+ var = Array (sattrs , prefixes = prefixes , aliases = aliases ,
203
+ shape = fhandle .variables [name ].shape )
204
+ root_container .attrs ['bald__contains' ].append (var )
147
205
148
- return sval
206
+
207
+ return root_container
208
+
209
+
210
+ def validate_netcdf (afilepath ):
211
+ """
212
+ Validate a file with respect to binary-array-linked-data.
213
+ Returns a :class:`bald.validation.Validation`
214
+
215
+ """
216
+ root_container = load_netcdf (afilepath )
217
+ return validate (root_container )
149
218
150
219
151
220
def validate_hdf5 (afilepath ):
152
221
"""
153
- Validate a file with respect ot binarry-array-linked-data.
222
+ Validate a file with respect to binary-array-linked-data.
223
+ Returns a :class:`bald.validation.Validation`
224
+
225
+ """
226
+ root_container = load_hdf5 (afilepath )
227
+ return validate (root_container )
228
+
229
+ def validate (root_container ):
230
+ """
231
+ Validate a Container with respect to binary-array-linked-data.
154
232
Returns a :class:`bald.validation.Validation`
233
+
155
234
"""
235
+ sval = bv .StoredValidation ()
236
+
237
+ root_val = bv .ContainerValidation (subject = root_container )
238
+ sval .stored_exceptions += root_val .exceptions ()
239
+ for subject in root_container .attrs .get ('bald__contains' , []):
240
+
241
+ # a dataset's attribute collection inherits from and
242
+ # specialises it's container's attrbiute collection
243
+ # this only helps with prefixes, afaik, hence:
244
+ # #
245
+ array_val = bv .ArrayValidation (subject )
246
+ sval .stored_exceptions += array_val .exceptions ()
247
+
248
+ return sval
156
249
250
+ def load_hdf5 (afilepath ):
157
251
with load (afilepath ) as fhandle :
158
- sval = bv . StoredValidation ()
252
+ # unused?
159
253
cache = {}
160
254
prefix_group = fhandle .attrs .get ('bald__isPrefixedBy' )
161
255
prefixes = {}
162
256
if prefix_group :
163
- prefixes = fhandle [prefix_group ].attrs
257
+ prefixes = dict ( fhandle [prefix_group ].attrs )
164
258
alias_group = fhandle .attrs .get ('bald__isAliasedBy' )
165
259
aliases = {}
166
260
if alias_group :
167
- aliases = dict (fhandle [alias_group ].attrs .iteritems ())
168
- root_container = Subject (fhandle .attrs , prefixes = prefixes , aliases = aliases )
169
- root_val = bv .ContainerValidation (subject = root_container ,
170
- fhandle = fhandle )
171
- sval .stored_exceptions += root_val .exceptions ()
261
+ aliases = dict (fhandle [alias_group ].attrs )
262
+ attrs = dict (fhandle .attrs )
263
+ attrs ['@id' ] = 'root'
264
+ root_container = Container (attrs , prefixes = prefixes , aliases = aliases )
265
+
266
+ root_container .attrs ['bald__contains' ] = []
267
+
172
268
# iterate through the datasets
173
269
for name , dataset in fhandle .items ():
174
- # a dataset's attribute collection inherits from and
175
- # specialises it's container's attrbiute collection
176
- # this only helps with prefixes, afaik, hence:
177
- # #
178
- sattrs = dict (fhandle .attrs ).copy ()
179
- sattrs .update (dataset .attrs )
180
- dset = Subject (sattrs , prefixes , aliases )
181
- dset_val = bv .ArrayValidation (name , dataset , fhandle = fhandle ,
182
- subject = dset )
183
- sval .stored_exceptions += dset_val .exceptions ()
184
-
185
- return sval
270
+ if hasattr (dataset , 'shape' ):
271
+ sattrs = dict (dataset .attrs )
272
+ sattrs ['@id' ] = dataset .name
273
+ ref = sattrs .get ('bald__references' , '' )
274
+ if ref :
275
+ ref_dset = fhandle [ref ]
276
+ child_dset = fhandle [ref_dset .attrs .get ('bald__array' ,
277
+ None )]
278
+ if child_dset :
279
+ cattrs = dict (child_dset .attrs )
280
+ cattrs ['@id' ] = child_dset .name
281
+ carray = Array (cattrs , prefixes , aliases ,
282
+ child_dset .shape )
283
+ sattrs ['bald__references' ] = [carray ]
284
+ dset = Array (sattrs , prefixes , aliases , dataset .shape )
285
+ root_container .attrs ['bald__contains' ].append (dset )
286
+
287
+ return root_container
288
+
289
+
0 commit comments