Skip to content

Commit ed0c5c9

Browse files
committed
onon
1 parent b720fcb commit ed0c5c9

File tree

6 files changed

+176
-7
lines changed

6 files changed

+176
-7
lines changed

lib/bald/__init__.py

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,48 @@
1+
import contextlib
2+
import re
3+
14
import h5py
25

3-
import bald.validation
6+
from bald.validation import ContainerValidation
7+
8+
class Container(object):
9+
def __init__(self, attrs=None):
10+
"""
11+
attrs: an dictionary of key value pair attributes
12+
"""
13+
if attrs is None:
14+
attrs = []
15+
self.attrs = attrs
16+
self._prefix_suffix = re.compile('(^(?:(?!_._).)*)_._((?!.*_._).*$)')
17+
_http_p = 'http[s]?://.*'
18+
self._http_uri = re.compile('{}'.format(_http_p))
19+
self._http_uri_prefix = re.compile('{}/|#'.format(_http_p))
20+
21+
22+
def prefixes(self):
23+
prefixes = {}
24+
for key, value in self.attrs.iteritems():
25+
if key.endswith('_._') and self._http_uri_prefix.match(value):
26+
pref = key.rstrip('_._')
27+
if prefixes.has_key(pref):
28+
raise ValueError('This container has conflicting prefix definitions')
29+
prefixes[pref] = value
30+
return prefixes
31+
432

33+
def unpack_uri(self, astring):
34+
result = astring
35+
if self._prefix_suffix.match(astring):
36+
prefix, suffix = self._prefix_suffix.match(astring).groups()
37+
38+
if self.prefixes().has_key(prefix):
39+
if self._http_uri.match(self.prefixes()[prefix]):
40+
result = astring.replace('{}_._'.format(prefix), self.prefixes()[prefix])
41+
return result
542

643
@contextlib.contextmanager
744
def load(afilepath):
8-
if afilepath.endswith('.hdf5'):
45+
if afilepath.endswith('.hdf'):
946
loader = h5py.File
1047
else:
1148
raise ValueError('filepath suffix not supported')
@@ -14,14 +51,19 @@ def load(afilepath):
1451
yield f
1552
finally:
1653
f.close()
17-
return f
1854

19-
def validate(afilepath):
55+
56+
def validate_hdf5(afilepath):
2057
"""
2158
Validate a file with respect ot binarry-array-linked-data.
2259
Returns a :class:`bald.validation.Validation`
2360
"""
2461

25-
with load(afilepath) as f:
62+
with load(afilepath) as fhandle:
63+
root_container = Container(fhandle.attrs)
64+
root_val = ContainerValidation(root_container)
65+
return root_val.is_valid()
2666

67+
68+
2769

lib/bald/__init__.pyc

-152 Bytes
Binary file not shown.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import unittest
2+
3+
import h5py
4+
import numpy as np
5+
6+
import bald
7+
from bald.tests import BaldTestCase
8+
9+
def _fattrs(f):
10+
f.attrs['bald_._'] = 'http://binary-array-ld.net/experimental/'
11+
f.attrs['bald_._type'] = 'bald_._Container'
12+
return f
13+
14+
def _create_parent_child(f, pshape, cshape):
15+
dsetp = f.create_dataset("parent_dataset", pshape, dtype='i')
16+
dsetc = f.create_dataset("child_dataset", cshape, dtype='i')
17+
dsetp.attrs['bald_._type'] = 'bald_._Dataset'
18+
dsetp.attrs['bald_._reference'] = dsetc.ref
19+
dsetc.attrs['bald_._type'] = 'bald_._Dataset'
20+
return f
21+
22+
23+
class Test(BaldTestCase):
24+
25+
def test_valid_uri(self):
26+
with self.temp_filename('.hdf') as tfile:
27+
f = h5py.File(tfile, "w")
28+
f = _fattrs(f)
29+
f = _create_parent_child(f, (11, 17), (11, 17))
30+
f.close()
31+
self.assertTrue(bald.validate_hdf5(tfile))
32+
33+
def test_invalid_uri(self):
34+
with self.temp_filename('.hdf') as tfile:
35+
f = h5py.File(tfile, "w")
36+
f = _fattrs(f)
37+
f = _create_parent_child(f, (11, 17), (11, 17))
38+
f.attrs['bald_._turtle'] = 'bald_._walnut'
39+
f.close()
40+
self.assertFalse(bald.validate_hdf5(tfile))
41+
42+
# class TestArrayReference(BaldTestCase):
43+
# def test_match_array_reference(self):
44+
# with self.temp_filename('.hdf') as tfile:
45+
# f = h5py.File(tfile, "w")
46+
# f = _fattrs(f)
47+
# f = _create_parent_child(f, (11, 17), (11, 17))
48+
# f.close()
49+
50+
51+
52+
if __name__ == '__main__':
53+
unittest.main()

lib/bald/tests/test_simple.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from bald.tests import BaldTestCase
77

88

9-
class ATest(BaldTestCase):
9+
class Test(BaldTestCase):
1010
def setUp(self):
1111
self.this = 'this'
1212

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import unittest
2+
3+
import h5py
4+
import numpy as np
5+
6+
from bald.tests import BaldTestCase
7+
from bald import validation
8+
9+
class Test(unittest.TestCase):
10+
def test_check_uri_200(self):
11+
auri = 'http://binary-array-ld.net/experimental'
12+
self.assertTrue(validation.check_uri(auri))
13+
14+
def test_check_uri_404(self):
15+
notauri = 'http://binary-array-ld.net/experimentalish'
16+
self.assertFalse(validation.check_uri(notauri))
17+
18+
19+
if __name__ == '__main__':
20+
unittest.main()

lib/bald/validation.py

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,62 @@
11

2+
import requests
3+
4+
import bald
5+
6+
def check_uri(uri):
7+
result = False
8+
if uri.startswith('http://') or uri.startswith('https://'):
9+
r = requests.get(uri)
10+
if r.status_code == 200:
11+
headers={'Accept':'text/turtle'}
12+
rraw = requests.get(uri, headers=headers)
13+
if rraw.status_code == 200:
14+
result = True
15+
return result
216

317

418

519
class Validation(object):
6-
pass
20+
21+
def is_valid(self):
22+
return not self.exceptions()
23+
24+
def exceptions(self):
25+
exceptions = []
26+
return exceptions
27+
28+
class ContainerValidation(Validation):
29+
def __init__(self, container):
30+
self.container = container
31+
32+
def is_valid(self):
33+
return not self.exceptions()
34+
35+
def exceptions(self):
36+
exceptions = []
37+
exceptions = self.check_attr_uris(exceptions)
38+
return exceptions
39+
40+
def check_attr_uris(self, exceptions):
41+
def _check_uri(uri, exceptions):
42+
if not check_uri(uri):
43+
msg = '{} is not resolving as a resource (404).'
44+
msg = msg.format(uri)
45+
exceptions.append(ValueError(msg))
46+
return exceptions
47+
48+
for pref, uri in self.container.prefixes().iteritems():
49+
exceptions = _check_uri(self.container.unpack_uri(uri),
50+
exceptions)
51+
for attr, value in self.container.attrs.iteritems():
52+
exceptions = _check_uri(self.container.unpack_uri(attr),
53+
exceptions)
54+
exceptions = _check_uri(self.container.unpack_uri(value),
55+
exceptions)
56+
57+
return exceptions
58+
59+
760

861
class ValidationSet(Validation):
962
pass
@@ -13,3 +66,4 @@ class ValidationList(Validation):
1366

1467

1568

69+
#if __name__ == '__main__':

0 commit comments

Comments
 (0)