From 97a6b0b837e1b8a1ec642e834f0ff937cbc678cb Mon Sep 17 00:00:00 2001 From: marqh Date: Sat, 28 May 2016 07:55:27 +0000 Subject: [PATCH 1/6] prefix container --- lib/bald/__init__.py | 25 +++++++++++++------ lib/bald/tests/integration/test_netcdf.py | 6 +++-- lib/bald/tests/integration/test_validation.py | 6 +++-- 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/lib/bald/__init__.py b/lib/bald/__init__.py index bcce06c..78ec8f9 100644 --- a/lib/bald/__init__.py +++ b/lib/bald/__init__.py @@ -33,15 +33,18 @@ def check_uri(self, uri): class Subject(object): - def __init__(self, attrs=None): + def __init__(self, attrs=None, prefixes=None): """ A subject of metadata statements. attrs: an dictionary of key value pair attributes """ if attrs is None: - attrs = [] + attrs = {} + if prefixes is None: + prefixes = {} self.attrs = attrs + self._prefixes = prefixes self._prefix_suffix = re.compile('(^(?:(?!__).)*)__((?!.*__).*$)') _http_p = 'http[s]?://.*' self._http_uri = re.compile('{}'.format(_http_p)) @@ -49,7 +52,7 @@ def __init__(self, attrs=None): def prefixes(self): prefixes = {} - for key, value in self.attrs.iteritems(): + for key, value in self._prefixes.iteritems(): if key.endswith('__') and self._http_uri_prefix.match(value): pref = key.rstrip('__') if pref in prefixes: @@ -105,17 +108,21 @@ def validate_netcdf(afilepath): with load(afilepath) as fhandle: sval = bv.StoredValidation() + prefix_group = fhandle[fhandle.bald__prefixes] if hasattr(fhandle, 'bald__prefixes') else {} + prefixes = {} + if prefix_group: + prefixes = dict([(prefix, getattr(prefix_group, prefix)) for prefix in prefix_group.ncattrs()]) attrs = {} for k in fhandle.ncattrs(): attrs[k] = getattr(fhandle, k) - root_container = Subject(attrs) + root_container = Subject(attrs, prefixes=prefixes) root_val = bv.ContainerValidation(subject=root_container, fhandle=fhandle) sval.stored_exceptions += root_val.exceptions() for name in fhandle.variables: sattrs = fhandle.__dict__.copy() sattrs.update(fhandle.variables[name].__dict__.copy()) - var = Subject(sattrs) + var = Subject(sattrs, prefixes=prefixes) var_val = bv.ArrayValidation(name, fhandle.variables[name], fhandle=fhandle, subject=var) sval.stored_exceptions += var_val.exceptions() @@ -132,7 +139,11 @@ def validate_hdf5(afilepath): with load(afilepath) as fhandle: sval = bv.StoredValidation() cache = {} - root_container = Subject(fhandle.attrs) + prefix_group = fhandle.attrs.get('bald__prefixes') + prefixes = {} + if prefix_group: + prefixes = fhandle[prefix_group].attrs + root_container = Subject(fhandle.attrs, prefixes=prefixes) root_val = bv.ContainerValidation(subject=root_container, fhandle=fhandle) sval.stored_exceptions += root_val.exceptions() @@ -144,7 +155,7 @@ def validate_hdf5(afilepath): # # sattrs = dict(fhandle.attrs).copy() sattrs.update(dataset.attrs) - dset = Subject(sattrs) + dset = Subject(sattrs, prefixes) dset_val = bv.ArrayValidation(name, dataset, fhandle=fhandle, subject=dset) sval.stored_exceptions += dset_val.exceptions() diff --git a/lib/bald/tests/integration/test_netcdf.py b/lib/bald/tests/integration/test_netcdf.py index b62bd21..35702ca 100644 --- a/lib/bald/tests/integration/test_netcdf.py +++ b/lib/bald/tests/integration/test_netcdf.py @@ -8,9 +8,11 @@ from bald.tests import BaldTestCase def _fattrs(f): - f.bald__ = 'http://binary-array-ld.net/latest/' - f.rdf__ = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#' f.rdf__type = 'bald__Container' + group_pref = f.createGroup('bald__prefix_list') + group_pref.bald__ = 'http://binary-array-ld.net/latest/' + group_pref.rdf__ = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#' + f.bald__prefixes = 'bald__prefix_list' return f def _create_parent_child(f, pshape, cshape): diff --git a/lib/bald/tests/integration/test_validation.py b/lib/bald/tests/integration/test_validation.py index f4bd796..5a153f9 100644 --- a/lib/bald/tests/integration/test_validation.py +++ b/lib/bald/tests/integration/test_validation.py @@ -7,9 +7,11 @@ from bald.tests import BaldTestCase def _fattrs(f): - f.attrs['bald__'] = 'http://binary-array-ld.net/latest/' - f.attrs['rdf__'] = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#' f.attrs['rdf__type'] = 'bald__Container' + group_pref = f.create_group('bald__prefix_list') + group_pref.attrs['bald__'] = 'http://binary-array-ld.net/latest/' + group_pref.attrs['rdf__'] = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#' + f.attrs['bald__prefixes'] = group_pref.ref return f def _create_parent_child(f, pshape, cshape): From c1f670a69e150e6e6569490659e7d52755906233 Mon Sep 17 00:00:00 2001 From: marqh Date: Sat, 28 May 2016 12:57:24 +0000 Subject: [PATCH 2/6] aliases and netcdf4 --- lib/bald/__init__.py | 8 +- lib/bald/tests/integration/test_aliases.py | 129 ++++++++++++++++++ lib/bald/tests/integration/test_netcdf.py | 2 +- .../tests/integration/test_netcdf4_classic.py | 78 +++++++++++ lib/bald/tests/integration/test_validation.py | 2 +- 5 files changed, 215 insertions(+), 4 deletions(-) create mode 100644 lib/bald/tests/integration/test_aliases.py create mode 100644 lib/bald/tests/integration/test_netcdf4_classic.py diff --git a/lib/bald/__init__.py b/lib/bald/__init__.py index 78ec8f9..7f9400f 100644 --- a/lib/bald/__init__.py +++ b/lib/bald/__init__.py @@ -108,10 +108,14 @@ def validate_netcdf(afilepath): with load(afilepath) as fhandle: sval = bv.StoredValidation() - prefix_group = fhandle[fhandle.bald__prefixes] if hasattr(fhandle, 'bald__prefixes') else {} + prefix_group = fhandle[fhandle.bald__isPrefixedBy] if hasattr(fhandle, 'bald__isPrefixedBy') else {} prefixes = {} if prefix_group: prefixes = dict([(prefix, getattr(prefix_group, prefix)) for prefix in prefix_group.ncattrs()]) + else: + for k in fhandle.ncattrs(): + if k.endswith('__'): + prefixes[k] = getattr(fhandle, k) attrs = {} for k in fhandle.ncattrs(): attrs[k] = getattr(fhandle, k) @@ -139,7 +143,7 @@ def validate_hdf5(afilepath): with load(afilepath) as fhandle: sval = bv.StoredValidation() cache = {} - prefix_group = fhandle.attrs.get('bald__prefixes') + prefix_group = fhandle.attrs.get('bald__isPrefixedBy') prefixes = {} if prefix_group: prefixes = fhandle[prefix_group].attrs diff --git a/lib/bald/tests/integration/test_aliases.py b/lib/bald/tests/integration/test_aliases.py new file mode 100644 index 0000000..c0e0786 --- /dev/null +++ b/lib/bald/tests/integration/test_aliases.py @@ -0,0 +1,129 @@ +import unittest + +import h5py +import numpy as np + +import bald +from bald.tests import BaldTestCase + +def _fattrs(f): + f.attrs['rdf__type'] = 'bald__Container' + group_alias = f.create_group('bald__alias_list') + group_pref.attrs['bald__'] = 'http://binary-array-ld.net/latest/' + group_pref.attrs['rdf__'] = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#' + f.attrs['bald__aliases'] = group_alias.ref + return f + +def _create_parent_child(f, pshape, cshape): + dsetp = f.create_dataset("parent_dataset", pshape, dtype='i') + dsetc = f.create_dataset("child_dataset", cshape, dtype='i') + dsetp.attrs['rdf__type'] = 'bald__Array' + dsetp.attrs['bald__references'] = dsetc.ref + dsetc.attrs['rdf__type'] = 'bald__Array' + dsetc.attrs['rdf__type'] = 'bald__Reference' + dsetc.attrs['bald__array'] = dsetc.ref + return f + + +class Test(BaldTestCase): + + def test_valid_uri(self): + with self.temp_filename('.hdf') as tfile: + f = h5py.File(tfile, "w") + f = _fattrs(f) + f = _create_parent_child(f, (11, 17), (11, 17)) + f.close() + validation = bald.validate_hdf5(tfile) + self.assertTrue(validation.is_valid()) + + def test_invalid_uri(self): + with self.temp_filename('.hdf') as tfile: + f = h5py.File(tfile, "w") + f = _fattrs(f) + f = _create_parent_child(f, (11, 17), (11, 17)) + f.attrs['bald__turtle'] = 'bald__walnut' + f.close() + validation = bald.validate_hdf5(tfile) + self.assertFalse(validation.is_valid()) + +class TestArrayReference(BaldTestCase): + def test_match(self): + with self.temp_filename('.hdf') as tfile: + f = h5py.File(tfile, "w") + f = _fattrs(f) + f = _create_parent_child(f, (11, 17), (11, 17)) + f.close() + validation = bald.validate_hdf5(tfile) + self.assertTrue(validation.is_valid()) + + def test_mismatch_zeroth(self): + with self.temp_filename('.hdf') as tfile: + f = h5py.File(tfile, "w") + f = _fattrs(f) + f = _create_parent_child(f, (11, 17), (11, 13)) + f.close() + validation = bald.validate_hdf5(tfile) + self.assertFalse(validation.is_valid()) + + def test_mismatch_oneth(self): + with self.temp_filename('.hdf') as tfile: + f = h5py.File(tfile, "w") + f = _fattrs(f) + f = _create_parent_child(f, (11, 17), (13, 17)) + f.close() + validation = bald.validate_hdf5(tfile) + self.assertFalse(validation.is_valid()) + + def test_match_plead_dim(self): + with self.temp_filename('.hdf') as tfile: + f = h5py.File(tfile, "w") + f = _fattrs(f) + # parent has leading dimension wrt child + f = _create_parent_child(f, (4, 13, 17), (13, 17)) + f.close() + validation = bald.validate_hdf5(tfile) + self.assertTrue(validation.is_valid()) + + def test_match_clead_dim(self): + with self.temp_filename('.hdf') as tfile: + f = h5py.File(tfile, "w") + f = _fattrs(f) + # child has leading dimension wrt parent + f = _create_parent_child(f, (13, 17), (7, 13, 17)) + f.close() + validation = bald.validate_hdf5(tfile) + self.assertTrue(validation.is_valid()) + + def test_mismatch_pdisjc_lead_dim(self): + with self.temp_filename('.hdf') as tfile: + f = h5py.File(tfile, "w") + f = _fattrs(f) + # child and parent have disjoint leading dimensions + f = _create_parent_child(f, (4, 13, 17), (7, 13, 17)) + + f.close() + validation = bald.validate_hdf5(tfile) + self.assertFalse(validation.is_valid()) + + def test_mismatch_pdisjc_trail_dim(self): + with self.temp_filename('.hdf') as tfile: + f = h5py.File(tfile, "w") + f = _fattrs(f) + # child and parent have disjoint trailing dimensions + f = _create_parent_child(f, (13, 17, 2), (13, 17, 9)) + f.close() + validation = bald.validate_hdf5(tfile) + self.assertFalse(validation.is_valid()) + + + # def test_match_(self): + # with self.temp_filename('.hdf') as tfile: + # f = h5py.File(tfile, "w") + # f = _fattrs(f) + # # + # f = _create_parent_child(f, (), ()) + # f.close() + # self.assert(bald.validate_hdf5(tfile).is_valid()) + +if __name__ == '__main__': + unittest.main() diff --git a/lib/bald/tests/integration/test_netcdf.py b/lib/bald/tests/integration/test_netcdf.py index 35702ca..4a8729c 100644 --- a/lib/bald/tests/integration/test_netcdf.py +++ b/lib/bald/tests/integration/test_netcdf.py @@ -12,7 +12,7 @@ def _fattrs(f): group_pref = f.createGroup('bald__prefix_list') group_pref.bald__ = 'http://binary-array-ld.net/latest/' group_pref.rdf__ = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#' - f.bald__prefixes = 'bald__prefix_list' + f.bald__isPrefixedBy = 'bald__prefix_list' return f def _create_parent_child(f, pshape, cshape): diff --git a/lib/bald/tests/integration/test_netcdf4_classic.py b/lib/bald/tests/integration/test_netcdf4_classic.py new file mode 100644 index 0000000..510b373 --- /dev/null +++ b/lib/bald/tests/integration/test_netcdf4_classic.py @@ -0,0 +1,78 @@ +import unittest + +import h5py +import netCDF4 +import numpy as np + +import bald +from bald.tests import BaldTestCase + +def _fattrs(f): + f.bald__ = 'http://binary-array-ld.net/latest/' + f.rdf__ = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#' + f.rdf__type = 'bald__Container' + return f + +def _create_parent_child(f, pshape, cshape): + for i, pdimsize in enumerate(pshape): + f.createDimension("pdim{}".format(str(i)), pdimsize) + for i, cdimsize in enumerate(cshape): + f.createDimension("cdim{}".format(str(i)), cdimsize) + varp = f.createVariable("parent_variable", 'i4', tuple(["pdim{}".format(str(i)) for i, _ in enumerate(pshape)])) + varc = f.createVariable("child_variable", 'i4', tuple(["cdim{}".format(str(i)) for i, _ in enumerate(cshape)])) + varp.rdf__type = 'bald__Array' + varp.bald__references = "child_variable" + varc.rdf__type = 'bald__Array' + varc.rdf__type = 'bald__Reference' + varc.bald__array = "child_variable" + return f + + +class Test(BaldTestCase): + + def test_valid_uri(self): + with self.temp_filename('.nc') as tfile: + f = netCDF4.Dataset(tfile, "w", format="NETCDF4_CLASSIC") + + f = _fattrs(f) + f.close() + validation = bald.validate_netcdf(tfile) + self.assertTrue(validation.is_valid()) + + def test_invalid_uri(self): + with self.temp_filename('.nc') as tfile: + f = netCDF4.Dataset(tfile, "w", format="NETCDF4_CLASSIC") + + f = _fattrs(f) + setattr(f, 'bald__turtle', 'bald__walnut') + f.close() + validation = bald.validate_netcdf(tfile) + self.assertFalse(validation.is_valid()) + + +class TestArrayReference(BaldTestCase): + def test_match(self): + with self.temp_filename('.nc') as tfile: + f = netCDF4.Dataset(tfile, "w", format="NETCDF4_CLASSIC") + f = _fattrs(f) + f = _create_parent_child(f, (11, 17), (11, 17)) + f.close() + validation = bald.validate_netcdf(tfile) + self.assertTrue(validation.is_valid()) + + def test_mismatch_zeroth(self): + with self.temp_filename('.nc') as tfile: + f = netCDF4.Dataset(tfile, "w", format="NETCDF4_CLASSIC") + f = _fattrs(f) + f = _create_parent_child(f, (11, 17), (11, 13)) + f.close() + validation = bald.validate_netcdf(tfile) + self.assertFalse(validation.is_valid()) + + +if __name__ == '__main__': + unittest.main() + + + + diff --git a/lib/bald/tests/integration/test_validation.py b/lib/bald/tests/integration/test_validation.py index 5a153f9..8434b86 100644 --- a/lib/bald/tests/integration/test_validation.py +++ b/lib/bald/tests/integration/test_validation.py @@ -11,7 +11,7 @@ def _fattrs(f): group_pref = f.create_group('bald__prefix_list') group_pref.attrs['bald__'] = 'http://binary-array-ld.net/latest/' group_pref.attrs['rdf__'] = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#' - f.attrs['bald__prefixes'] = group_pref.ref + f.attrs['bald__isPrefixedBy'] = group_pref.ref return f def _create_parent_child(f, pshape, cshape): From e26d278258ad15f18df93d4b3230d9556a48b6f9 Mon Sep 17 00:00:00 2001 From: marqh Date: Tue, 6 Sep 2016 08:06:26 +0000 Subject: [PATCH 3/6] test ready --- lib/bald/tests/integration/test_aliases.py | 94 +++------------------- 1 file changed, 12 insertions(+), 82 deletions(-) diff --git a/lib/bald/tests/integration/test_aliases.py b/lib/bald/tests/integration/test_aliases.py index c0e0786..de46ebe 100644 --- a/lib/bald/tests/integration/test_aliases.py +++ b/lib/bald/tests/integration/test_aliases.py @@ -8,10 +8,10 @@ def _fattrs(f): f.attrs['rdf__type'] = 'bald__Container' - group_alias = f.create_group('bald__alias_list') + group_pref = f.create_group('bald__prefix_list') group_pref.attrs['bald__'] = 'http://binary-array-ld.net/latest/' group_pref.attrs['rdf__'] = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#' - f.attrs['bald__aliases'] = group_alias.ref + f.attrs['bald__isPrefixedBy'] = group_pref.ref return f def _create_parent_child(f, pshape, cshape): @@ -31,7 +31,11 @@ def test_valid_uri(self): with self.temp_filename('.hdf') as tfile: f = h5py.File(tfile, "w") f = _fattrs(f) - f = _create_parent_child(f, (11, 17), (11, 17)) + group_alias = f.create_group('bald__alias_list') + f.attrs['bald__aliases'] = group_alias.ref + group_alias['skosPrefLabel'] = 'http://www.w3.org/2004/02/skos/core#prefLabel' + dsetp = f.create_dataset("parent_dataset", (11, 17), dtype='i') + dsetp.attrs['skosPrefLabel'] = 'alabel' f.close() validation = bald.validate_hdf5(tfile) self.assertTrue(validation.is_valid()) @@ -40,90 +44,16 @@ def test_invalid_uri(self): with self.temp_filename('.hdf') as tfile: f = h5py.File(tfile, "w") f = _fattrs(f) - f = _create_parent_child(f, (11, 17), (11, 17)) f.attrs['bald__turtle'] = 'bald__walnut' + group_alias = f.create_group('bald__alias_list') + f.attrs['bald__aliases'] = group_alias.ref + group_alias['skosPrefLabel'] = 'http://www.w3.org/2004/02/skos/core#notThisPrefLabel' + dsetp = f.create_dataset("parent_dataset", (11, 17), dtype='i') + dsetp.attrs['skosPrefLabel'] = 'alabel' f.close() validation = bald.validate_hdf5(tfile) self.assertFalse(validation.is_valid()) -class TestArrayReference(BaldTestCase): - def test_match(self): - with self.temp_filename('.hdf') as tfile: - f = h5py.File(tfile, "w") - f = _fattrs(f) - f = _create_parent_child(f, (11, 17), (11, 17)) - f.close() - validation = bald.validate_hdf5(tfile) - self.assertTrue(validation.is_valid()) - - def test_mismatch_zeroth(self): - with self.temp_filename('.hdf') as tfile: - f = h5py.File(tfile, "w") - f = _fattrs(f) - f = _create_parent_child(f, (11, 17), (11, 13)) - f.close() - validation = bald.validate_hdf5(tfile) - self.assertFalse(validation.is_valid()) - - def test_mismatch_oneth(self): - with self.temp_filename('.hdf') as tfile: - f = h5py.File(tfile, "w") - f = _fattrs(f) - f = _create_parent_child(f, (11, 17), (13, 17)) - f.close() - validation = bald.validate_hdf5(tfile) - self.assertFalse(validation.is_valid()) - - def test_match_plead_dim(self): - with self.temp_filename('.hdf') as tfile: - f = h5py.File(tfile, "w") - f = _fattrs(f) - # parent has leading dimension wrt child - f = _create_parent_child(f, (4, 13, 17), (13, 17)) - f.close() - validation = bald.validate_hdf5(tfile) - self.assertTrue(validation.is_valid()) - - def test_match_clead_dim(self): - with self.temp_filename('.hdf') as tfile: - f = h5py.File(tfile, "w") - f = _fattrs(f) - # child has leading dimension wrt parent - f = _create_parent_child(f, (13, 17), (7, 13, 17)) - f.close() - validation = bald.validate_hdf5(tfile) - self.assertTrue(validation.is_valid()) - - def test_mismatch_pdisjc_lead_dim(self): - with self.temp_filename('.hdf') as tfile: - f = h5py.File(tfile, "w") - f = _fattrs(f) - # child and parent have disjoint leading dimensions - f = _create_parent_child(f, (4, 13, 17), (7, 13, 17)) - - f.close() - validation = bald.validate_hdf5(tfile) - self.assertFalse(validation.is_valid()) - - def test_mismatch_pdisjc_trail_dim(self): - with self.temp_filename('.hdf') as tfile: - f = h5py.File(tfile, "w") - f = _fattrs(f) - # child and parent have disjoint trailing dimensions - f = _create_parent_child(f, (13, 17, 2), (13, 17, 9)) - f.close() - validation = bald.validate_hdf5(tfile) - self.assertFalse(validation.is_valid()) - - - # def test_match_(self): - # with self.temp_filename('.hdf') as tfile: - # f = h5py.File(tfile, "w") - # f = _fattrs(f) - # # - # f = _create_parent_child(f, (), ()) - # f.close() - # self.assert(bald.validate_hdf5(tfile).is_valid()) if __name__ == '__main__': unittest.main() From 7adc94ddb387934cb8d525e674ed052ad7e8c06f Mon Sep 17 00:00:00 2001 From: marqh Date: Sat, 1 Oct 2016 11:11:34 +0000 Subject: [PATCH 4/6] aliases --- lib/bald/__init__.py | 20 ++++++++++++++++---- lib/bald/tests/integration/test_aliases.py | 8 ++++---- lib/bald/validation.py | 14 +++++++++++--- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/lib/bald/__init__.py b/lib/bald/__init__.py index 7f9400f..b6bc26f 100644 --- a/lib/bald/__init__.py +++ b/lib/bald/__init__.py @@ -15,9 +15,12 @@ class HttpCache(object): def __init__(self): self.cache = {} + def is_http_uri(self, item): + return item.startswith('http://') or item.startswith('https://') + def __getitem__(self, item): - if not item.startswith('http://') or item.startswith('https://'): + if not self.is_http_uri(item): raise ValueError('{} is not a HTTP URI.'.format(item)) if item not in self.cache: headers = {'Accept': 'text/turtle'} @@ -33,7 +36,7 @@ def check_uri(self, uri): class Subject(object): - def __init__(self, attrs=None, prefixes=None): + def __init__(self, attrs=None, prefixes=None, aliases=None): """ A subject of metadata statements. @@ -43,7 +46,10 @@ def __init__(self, attrs=None, prefixes=None): attrs = {} if prefixes is None: prefixes = {} + if aliases is None: + aliases = {} self.attrs = attrs + self.aliases = aliases self._prefixes = prefixes self._prefix_suffix = re.compile('(^(?:(?!__).)*)__((?!.*__).*$)') _http_p = 'http[s]?://.*' @@ -69,6 +75,8 @@ def unpack_uri(self, astring): if self._http_uri.match(self.prefixes()[prefix]): result = astring.replace('{}__'.format(prefix), self.prefixes()[prefix]) + elif astring in self.aliases: + result = self.aliases[astring] return result @@ -147,7 +155,11 @@ def validate_hdf5(afilepath): prefixes = {} if prefix_group: prefixes = fhandle[prefix_group].attrs - root_container = Subject(fhandle.attrs, prefixes=prefixes) + alias_group = fhandle.attrs.get('bald__isAliasedBy') + aliases = {} + if alias_group: + aliases = dict(fhandle[alias_group].attrs.iteritems()) + root_container = Subject(fhandle.attrs, prefixes=prefixes, aliases=aliases) root_val = bv.ContainerValidation(subject=root_container, fhandle=fhandle) sval.stored_exceptions += root_val.exceptions() @@ -159,7 +171,7 @@ def validate_hdf5(afilepath): # # sattrs = dict(fhandle.attrs).copy() sattrs.update(dataset.attrs) - dset = Subject(sattrs, prefixes) + dset = Subject(sattrs, prefixes, aliases) dset_val = bv.ArrayValidation(name, dataset, fhandle=fhandle, subject=dset) sval.stored_exceptions += dset_val.exceptions() diff --git a/lib/bald/tests/integration/test_aliases.py b/lib/bald/tests/integration/test_aliases.py index de46ebe..4e05677 100644 --- a/lib/bald/tests/integration/test_aliases.py +++ b/lib/bald/tests/integration/test_aliases.py @@ -32,8 +32,8 @@ def test_valid_uri(self): f = h5py.File(tfile, "w") f = _fattrs(f) group_alias = f.create_group('bald__alias_list') - f.attrs['bald__aliases'] = group_alias.ref - group_alias['skosPrefLabel'] = 'http://www.w3.org/2004/02/skos/core#prefLabel' + f.attrs['bald__isAliasedBy'] = group_alias.ref + group_alias.attrs['skosPrefLabel'] = 'http://www.w3.org/2004/02/skos/core#prefLabel' dsetp = f.create_dataset("parent_dataset", (11, 17), dtype='i') dsetp.attrs['skosPrefLabel'] = 'alabel' f.close() @@ -46,8 +46,8 @@ def test_invalid_uri(self): f = _fattrs(f) f.attrs['bald__turtle'] = 'bald__walnut' group_alias = f.create_group('bald__alias_list') - f.attrs['bald__aliases'] = group_alias.ref - group_alias['skosPrefLabel'] = 'http://www.w3.org/2004/02/skos/core#notThisPrefLabel' + f.attrs['bald__isAliasedBy'] = group_alias.ref + group_alias.attrs['skosPrefLabel'] = 'http://www.w3.org/2004/02/skos/core#notThisPrefLabel' dsetp = f.create_dataset("parent_dataset", (11, 17), dtype='i') dsetp.attrs['skosPrefLabel'] = 'alabel' f.close() diff --git a/lib/bald/validation.py b/lib/bald/validation.py index e6c77c9..18893ff 100644 --- a/lib/bald/validation.py +++ b/lib/bald/validation.py @@ -91,12 +91,16 @@ def _check_uri(uri, exceptions): for pref, uri in self.subject.prefixes().iteritems(): exceptions = _check_uri(self.subject.unpack_uri(uri), exceptions) + for alias, uri in self.subject.aliases.iteritems(): + exceptions = _check_uri(self.subject.unpack_uri(uri), + exceptions) for attr, value in self.subject.attrs.iteritems(): exceptions = _check_uri(self.subject.unpack_uri(attr), exceptions) if isinstance(value, str): - exceptions = _check_uri(self.subject.unpack_uri(value), - exceptions) + val = self.subject.unpack_uri(value) + if self.cache.is_http_uri(val): + exceptions = _check_uri(val, exceptions) return exceptions def check_attr_domain_range(self, exceptions): @@ -106,7 +110,11 @@ def check_attr_domain_range(self, exceptions): # thus we have a payload # go rdf g = rdflib.Graph() - g.parse(data=self.cache[uri].text, format="n3") + data=self.cache[uri].text + try: + g.parse(data=self.cache[uri].text, format="n3") + except Exception, e: + g.parse(data=self.cache[uri].text, format="xml") query = ('SELECT ?s \n' '(GROUP_CONCAT(?domain; SEPARATOR=" | ") AS ?domains)' ' \n' From 732ca0fd1d3af79a1f70280ca12b3e7024d73a99 Mon Sep 17 00:00:00 2001 From: marqh Date: Thu, 6 Oct 2016 07:30:42 +0000 Subject: [PATCH 5/6] testing --- .travis.yml | 23 ++++++ README.md | 6 +- conda-requirements.txt | 5 ++ lib/bald/__init__.py | 1 + .../{test_validation.py => test_HttpCache.py} | 11 ++- lib/bald/validation.py | 2 +- setup.py | 79 +++++++++++++++++++ 7 files changed, 120 insertions(+), 7 deletions(-) create mode 100644 .travis.yml create mode 100644 conda-requirements.txt rename lib/bald/tests/unit/{test_validation.py => test_HttpCache.py} (59%) diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..29c722e --- /dev/null +++ b/.travis.yml @@ -0,0 +1,23 @@ +language: python +python: + - "3.4" +install: + - wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh + - bash Miniconda3-latest-Linux-x86_64.sh -b -p $HOME/miniconda + - export PATH="$HOME/miniconda/bin:$PATH" + + - conda config --add channels conda-forge + - conda config --add channels bioconda + - conda config --set always_yes yes --set changeps1 no + - conda config --set show_channel_urls True + + - ENV_NAME='testing' + - conda create --quiet -n $ENV_NAME python=$TRAVIS_PYTHON_VERSION + - source activate $ENV_NAME + - conda install --quiet --file conda-requirements.txt + - conda list + - conda info -a + - python setup.py --quiet install + +script: + python -m unittest discover -s bald.tests -v diff --git a/README.md b/README.md index 032101d..3437776 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ -# bald +# Binary Array Linked Data: bald + [![Build Status](https://api.travis-ci.org/repositories/binary-array-ld/bald.svg?branch=master)](http://travis-ci.org/binary-array-ld/bald/branches) -Python library for validating and managing binary array linked data files. + +A Python library for validating and managing binary array linked data files. diff --git a/conda-requirements.txt b/conda-requirements.txt new file mode 100644 index 0000000..cf30e40 --- /dev/null +++ b/conda-requirements.txt @@ -0,0 +1,5 @@ +numpy +h5py +netCDF4 +requests +rdflib diff --git a/lib/bald/__init__.py b/lib/bald/__init__.py index b6bc26f..cc4e8a0 100644 --- a/lib/bald/__init__.py +++ b/lib/bald/__init__.py @@ -7,6 +7,7 @@ import bald.validation as bv +__version__ = '0.2' class HttpCache(object): """ diff --git a/lib/bald/tests/unit/test_validation.py b/lib/bald/tests/unit/test_HttpCache.py similarity index 59% rename from lib/bald/tests/unit/test_validation.py rename to lib/bald/tests/unit/test_HttpCache.py index 09a1815..e4098bf 100644 --- a/lib/bald/tests/unit/test_validation.py +++ b/lib/bald/tests/unit/test_HttpCache.py @@ -4,16 +4,19 @@ import numpy as np from bald.tests import BaldTestCase -from bald import validation +import bald + +class TestHttpCache(unittest.TestCase): + def setUp(self): + self.cache = bald.HttpCache() -class Test(unittest.TestCase): def test_check_uri_200(self): auri = 'http://binary-array-ld.net/experimental' - self.assertTrue(validation.check_uri(auri)) + self.assertTrue(self.cache.check_uri(auri)) def test_check_uri_404(self): notauri = 'http://binary-array-ld.net/experimentalish' - self.assertFalse(validation.check_uri(notauri)) + self.assertFalse(self.cache.check_uri(notauri)) if __name__ == '__main__': diff --git a/lib/bald/validation.py b/lib/bald/validation.py index 18893ff..b7ee910 100644 --- a/lib/bald/validation.py +++ b/lib/bald/validation.py @@ -113,7 +113,7 @@ def check_attr_domain_range(self, exceptions): data=self.cache[uri].text try: g.parse(data=self.cache[uri].text, format="n3") - except Exception, e: + except Exception: g.parse(data=self.cache[uri].text, format="xml") query = ('SELECT ?s \n' '(GROUP_CONCAT(?domain; SEPARATOR=" | ") AS ?domains)' diff --git a/setup.py b/setup.py index e69de29..ab616d2 100644 --- a/setup.py +++ b/setup.py @@ -0,0 +1,79 @@ +#!/usr/bin/env python +from __future__ import print_function + +import os +from setuptools import setup + + +DESCRIPTION = 'Binary Array Linked Data' +NAME = 'bald' +DIR_ROOT = os.path.abspath(os.path.dirname(__file__)) +DIR_PACKAGE = os.path.join(DIR_ROOT, 'lib', NAME) +#DIR_DATA = os.path.join(DIR_PACKAGE, 'sample_data') + + +def extract_version(): + version = None + fname = os.path.join(DIR_PACKAGE, '__init__.py') + with open(fname) as fin: + for line in fin: + if (line.startswith('__version__')): + _, version = line.split('=') + version = version.strip()[1:-1] # Remove quotation. + break + return version + + +def extract_description(): + description = DESCRIPTION + fname = os.path.join(DIR_ROOT, 'README.rst') + if os.path.isfile(fname): + with open(fname) as fin: + description = fin.read() + return description + + +# def extract_package_data(): +# package_data = [] +# offset = len(os.path.dirname(DIR_DATA)) + 1 +# for dpath, dnames, fnames in os.walk(DIR_DATA): +# globs = set() +# fpath = os.path.join(dpath[offset:]) +# for fname in fnames: +# _, ext = os.path.splitext(fname) +# globs.add('*{}'.format(ext)) +# for glob in globs: +# package_data.append(os.path.join(fpath, glob)) +# return {NAME: package_data} + + +setup_args = dict( + name=NAME, + version=extract_version(), + description=DESCRIPTION, + long_description=extract_description(), + platforms=['Linux', 'Max OS X', 'Windows'], + license='BSD', + url='https://github.com/binary-array-ld/bald', + package_dir={'': 'lib'}, + packages=[NAME], + # package_data=extract_package_data(), + classifiers=[ + # For full license details, see + # http://reference.data.gov.uk/id/open-government-licence + 'License :: Freely Distributable', + 'Development Status :: 1 - Planning Development Status', + 'Programming Language :: Python :: 2.7', + 'Programming Language :: Python :: 3.4', + 'Programming Language :: Python :: 3.5', + 'Operating System :: OS Independent', + 'Intended Audience :: Science/Research', + 'Intended Audience :: Developers', + 'Natural Language :: English', + 'Topic :: Scientific/Engineering', + 'Topic :: Software Development :: Libraries'], +) + + +if __name__ == '__main__': + setup(**setup_args) From ef83273cc1b2534069ab47c652fd34125e3e3765 Mon Sep 17 00:00:00 2001 From: marqh Date: Sun, 16 Oct 2016 09:50:32 +0000 Subject: [PATCH 6/6] use py27 --- .travis.yml | 2 +- setup.py | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 29c722e..0c552a2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,6 @@ language: python python: - - "3.4" + - "2.7" install: - wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh - bash Miniconda3-latest-Linux-x86_64.sh -b -p $HOME/miniconda diff --git a/setup.py b/setup.py index ab616d2..35d36e6 100644 --- a/setup.py +++ b/setup.py @@ -2,8 +2,7 @@ from __future__ import print_function import os -from setuptools import setup - +from distutils.core import setup DESCRIPTION = 'Binary Array Linked Data' NAME = 'bald' @@ -56,7 +55,9 @@ def extract_description(): license='BSD', url='https://github.com/binary-array-ld/bald', package_dir={'': 'lib'}, - packages=[NAME], + packages=[NAME, '{}.tests'.format(NAME), + '{}.tests.unit'.format(NAME), + '{}.tests.integration'.format(NAME)], # package_data=extract_package_data(), classifiers=[ # For full license details, see