Skip to content

Commit c7c6c0f

Browse files
authored
Merge pull request #1 from marqh/referCache
Refer cache
2 parents e187465 + 6ebff86 commit c7c6c0f

12 files changed

+107
-85
lines changed

lib/bald/__init__.py

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -235,11 +235,15 @@ def __getitem__(self, item):
235235
if not self.is_http_uri(item):
236236
raise ValueError('{} is not a HTTP URI.'.format(item))
237237
if item not in self.cache:
238-
headers = {'Accept': 'text/turtle'}
239238
# import datetime
240239
# now = datetime.datetime.utcnow()
241240
# print('\ndownloading: {}'.format(item))
242-
self.cache[item] = requests.get(item, headers=headers)
241+
try:
242+
headers = {'Accept': 'application/rdf+xml'}
243+
self.cache[item] = requests.get(item, headers=headers)
244+
except Exception:
245+
headers = {'Accept': 'text/html'}
246+
self.cache[item] = requests.get(item, headers=headers)
243247
# then = datetime.datetime.utcnow()
244248
# print('{}s'.format((then-now).total_seconds()))
245249

@@ -387,9 +391,6 @@ def unpack_rdfobject(self, astring, predicate):
387391
# qres = self.alias_graph.query(rdfobj_alias_query)
388392
try:
389393
qres = self.alias_graph.query(rdfobj_alias_query)
390-
# except Exception:
391-
# import pdb; pdb.set_trace()
392-
# qres = self.alias_graph.query(rdfobj_alias_query)
393394
results = list(qres)
394395
if len(results) > 1:
395396
raise ValueError('multiple alias options')
@@ -637,13 +638,16 @@ def load(afilepath):
637638
finally:
638639
f.close()
639640

640-
def load_netcdf(afilepath, baseuri=None, alias_dict=None):
641+
def load_netcdf(afilepath, baseuri=None, alias_dict=None, cache=None):
641642
"""
642643
Load a file with respect to binary-array-linked-data.
643644
Returns a :class:`bald.Collection`
644645
"""
645646
if alias_dict == None:
646647
alias_dict = {}
648+
if cache is None:
649+
cache = HttpCache()
650+
647651
with load(afilepath) as fhandle:
648652
if baseuri is None:
649653
baseuri = 'file://{}'.format(afilepath)
@@ -693,11 +697,21 @@ def load_netcdf(afilepath, baseuri=None, alias_dict=None):
693697
attrs[k] = getattr(fhandle, k)
694698

695699
aliasgraph = rdflib.Graph()
700+
696701
for alias in aliases:
702+
response = cache[aliases[alias]]
697703
try:
698-
aliasgraph.parse(aliases[alias], format='xml')
699-
except TypeError:
700-
pass
704+
aliasgraph.parse(data=response.text, format='xml')
705+
except Exception:
706+
print('Failed to parse: {}'.format(aliases[alias]))
707+
# try:
708+
# import xml.sax._exceptions
709+
# aliasgraph.parse(data=response.text, format='xml')
710+
# except TypeError:
711+
# pass
712+
# except xml.sax._exceptions.SAXParseException:
713+
# import pdb; pdb.set_trace()
714+
# pass
701715
# if hasattr(fhandle, 'Conventions'):
702716
# conventions = [c.strip() for c in fhandle.Conventions.split(',')]
703717
# for conv in conventions:
@@ -715,7 +729,6 @@ def load_netcdf(afilepath, baseuri=None, alias_dict=None):
715729
# if len(set(na_keys)) != len(na_keys):
716730
# raise ValueError('duplicate aliases')
717731
# aliases = careful_update(aliases, dict(new_aliases))
718-
719732
root_container = Container(baseuri, '', attrs, prefixes=prefixes,
720733
aliases=aliases, alias_graph=aliasgraph)
721734

@@ -750,7 +763,11 @@ def load_netcdf(afilepath, baseuri=None, alias_dict=None):
750763

751764
reference_prefixes = dict()
752765
reference_graph = copy.copy(aliasgraph)
753-
reference_graph.parse('http://binary-array-ld.net/latest?_format=ttl')
766+
767+
response = cache['http://binary-array-ld.net/latest']
768+
reference_graph.parse(data=response.text, format='xml')
769+
770+
# reference_graph.parse('http://binary-array-ld.net/latest?_format=ttl')
754771
qstr = ('prefix bald: <http://binary-array-ld.net/latest/> '
755772
'prefix skos: <http://www.w3.org/2004/02/skos/core#> '
756773
'select ?s '
@@ -842,24 +859,24 @@ def load_netcdf(afilepath, baseuri=None, alias_dict=None):
842859
return root_container
843860

844861

845-
def validate_netcdf(afilepath, cache=None, baseuri=None):
862+
def validate_netcdf(afilepath, baseuri=None, cache=None):
846863
"""
847864
Validate a file with respect to binary-array-linked-data.
848865
Returns a :class:`bald.validation.Validation`
849866
850867
"""
851-
root_container = load_netcdf(afilepath, baseuri=baseuri)
868+
root_container = load_netcdf(afilepath, baseuri=baseuri, cache=cache)
852869
return validate(root_container, cache=cache)
853870

854871

855-
def validate_hdf5(afilepath, cache=None, baseuri=None):
872+
def validate_hdf5(afilepath, baseuri=None, cache=None):
856873
"""
857874
Validate a file with respect to binary-array-linked-data.
858875
Returns a :class:`bald.validation.Validation`
859876
860877
"""
861-
root_container = load_hdf5(afilepath, baseuri=baseuri)
862-
return validate(root_container)
878+
root_container = load_hdf5(afilepath, baseuri=baseuri, cache=cache)
879+
return validate(root_container, cache=cache)
863880

864881
def validate(root_container, sval=None, cache=None):
865882
"""
@@ -896,20 +913,24 @@ def careful_update(adict, bdict):
896913
adict.update(bdict)
897914
return adict
898915

899-
def load_hdf5(afilepath, baseuri=None, alias_dict=None):
916+
def load_hdf5(afilepath, baseuri=None, alias_dict=None, cache=None):
917+
if cache is None:
918+
cache = HttpCache()
900919
with load(afilepath) as fhandle:
901920
# unused?
902921
cache = {}
903922
if baseuri is None:
904923
baseuri = 'file://{}'.format(afilepath)
905924

906925
root_container, file_variables = _hdf_group(fhandle, baseuri=baseuri,
907-
alias_dict=alias_dict)
926+
alias_dict=alias_dict, cache=cache)
908927
_hdf_references(fhandle, root_container, file_variables)
909928
return root_container
910929

911930
def _hdf_group(fhandle, identity='root', baseuri=None, prefixes=None,
912-
aliases=None, alias_dict=None):
931+
aliases=None, alias_dict=None, cache=None):
932+
if cache is None:
933+
cache = HttpCache()
913934

914935
prefix_group = fhandle.attrs.get('bald__isPrefixedBy')
915936
if prefixes is None:

lib/bald/tests/integration/HTML/hdf_container_nest.html

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -174,33 +174,33 @@
174174
return cell;
175175
};
176176

177-
var file:///tmp/tmpk7nc3lox.hdf/alocation = instance('file:///tmp/tmpk7nc3lox.hdf/alocation:<a xlink:href="http://binary-array-ld.net/latest/Array" xlink:show=new text-decoration="underline">bald__Array</a>, <a xlink:href="http://binary-array-ld.net/latest/Reference" xlink:show=new text-decoration="underline">bald__Reference</a>', ['<a xlink:href="http://binary-array-ld.net/latest/array" xlink:show=new text-decoration="underline">bald__array</a>: |', '<a xlink:href="http://binary-array-ld.net/latest/references" xlink:show=new text-decoration="underline">bald__references</a>: |', '<a xlink:href="http://binary-array-ld.net/latest/shape" xlink:show=new text-decoration="underline">bald__shape</a>: 11, 17'], '#878800');
178-
var file:///tmp/tmpk7nc3lox.hdf/anotherpair = instance('file:///tmp/tmpk7nc3lox.hdf/anotherpair:<a xlink:href="http://binary-array-ld.net/latest/Array" xlink:show=new text-decoration="underline">bald__Array</a>, <a xlink:href="http://binary-array-ld.net/latest/Reference" xlink:show=new text-decoration="underline">bald__Reference</a>', ['<a xlink:href="http://binary-array-ld.net/latest/array" xlink:show=new text-decoration="underline">bald__array</a>: |', '<a xlink:href="http://binary-array-ld.net/latest/shape" xlink:show=new text-decoration="underline">bald__shape</a>: 2'], '#878800');
179-
var file:///tmp/tmpk7nc3lox.hdf/apair = instance('file:///tmp/tmpk7nc3lox.hdf/apair:<a xlink:href="http://binary-array-ld.net/latest/Array" xlink:show=new text-decoration="underline">bald__Array</a>', ['<a xlink:href="http://binary-array-ld.net/latest/references" xlink:show=new text-decoration="underline">bald__references</a>: |', '<a xlink:href="http://binary-array-ld.net/latest/shape" xlink:show=new text-decoration="underline">bald__shape</a>: 2'], '#878800');
180-
var file:///tmp/tmpk7nc3lox.hdf/data = instance('file:///tmp/tmpk7nc3lox.hdf/data:<a xlink:href="http://binary-array-ld.net/latest/Array" xlink:show=new text-decoration="underline">bald__Array</a>', ['<a xlink:href="http://binary-array-ld.net/latest/references" xlink:show=new text-decoration="underline">bald__references</a>: |', '<a xlink:href="http://binary-array-ld.net/latest/shape" xlink:show=new text-decoration="underline">bald__shape</a>: 11, 17'], '#878800');
181-
var file:///tmp/tmpk7nc3lox.hdf/discovery = instance('file:///tmp/tmpk7nc3lox.hdf/discovery:<a xlink:href="http://binary-array-ld.net/latest/Container" xlink:show=new text-decoration="underline">bald__Container</a>', ['<a xlink:href="http://binary-array-ld.net/latest/contains" xlink:show=new text-decoration="underline">bald__contains</a>: |'], '#878800');
182-
var file:///tmp/tmpk7nc3lox.hdf/institution = instance('file:///tmp/tmpk7nc3lox.hdf/institution:<a xlink:href="http://binary-array-ld.net/latest/Array" xlink:show=new text-decoration="underline">bald__Array</a>', ['<a xlink:href="http://binary-array-ld.net/latest/shape" xlink:show=new text-decoration="underline">bald__shape</a>: |', '<a xlink:href="http://www.w3.org/2004/02/skos/core#prefLabel" xlink:show=new text-decoration="underline">skos__prefLabel</a>: a quality establishment'], '#878800');
183-
var file:///tmp/tmpk7nc3lox.hdf/locref = instance('file:///tmp/tmpk7nc3lox.hdf/locref:<a xlink:href="http://binary-array-ld.net/latest/Array" xlink:show=new text-decoration="underline">bald__Array</a>', ['<a xlink:href="http://binary-array-ld.net/latest/shape" xlink:show=new text-decoration="underline">bald__shape</a>: |', '<a xlink:href="http://www.w3.org/2004/02/skos/core#prefLabel" xlink:show=new text-decoration="underline">skos__prefLabel</a>: for locational purposes'], '#878800');
184-
var file:///tmp/tmpk7nc3lox.hdf/locref2 = instance('file:///tmp/tmpk7nc3lox.hdf/locref2:<a xlink:href="http://binary-array-ld.net/latest/Array" xlink:show=new text-decoration="underline">bald__Array</a>', ['<a xlink:href="http://binary-array-ld.net/latest/shape" xlink:show=new text-decoration="underline">bald__shape</a>: |', '<a xlink:href="http://www.w3.org/2004/02/skos/core#prefLabel" xlink:show=new text-decoration="underline">skos__prefLabel</a>: for more locational purposes'], '#878800');
185-
var file:///tmp/tmpk7nc3lox.hdf/referencing = instance('file:///tmp/tmpk7nc3lox.hdf/referencing:<a xlink:href="http://binary-array-ld.net/latest/Container" xlink:show=new text-decoration="underline">bald__Container</a>', ['<a xlink:href="http://binary-array-ld.net/latest/contains" xlink:show=new text-decoration="underline">bald__contains</a>: |'], '#878800');
186-
var file:///tmp/tmpk7nc3lox.hdf/root = instance('file:///tmp/tmpk7nc3lox.hdf/root:<a xlink:href="http://binary-array-ld.net/latest/Container" xlink:show=new text-decoration="underline">bald__Container</a>', ['<a xlink:href="http://binary-array-ld.net/latest/contains" xlink:show=new text-decoration="underline">bald__contains</a>: |'], '#878800');
187-
var file:///tmp/tmpk7nc3lox.hdf/source = instance('file:///tmp/tmpk7nc3lox.hdf/source:<a xlink:href="http://binary-array-ld.net/latest/Container" xlink:show=new text-decoration="underline">bald__Container</a>', ['<a xlink:href="http://binary-array-ld.net/latest/contains" xlink:show=new text-decoration="underline">bald__contains</a>: |'], '#878800');
188-
link(file:///tmp/tmpk7nc3lox.hdf/alocation, file:///tmp/tmpk7nc3lox.hdf/alocation, 'bald__array', 'bottom');
189-
link(file:///tmp/tmpk7nc3lox.hdf/alocation, file:///tmp/tmpk7nc3lox.hdf/locref, 'bald__references');
190-
link(file:///tmp/tmpk7nc3lox.hdf/alocation, file:///tmp/tmpk7nc3lox.hdf/locref2, 'bald__references');
191-
link(file:///tmp/tmpk7nc3lox.hdf/anotherpair, file:///tmp/tmpk7nc3lox.hdf/anotherpair, 'bald__array', 'bottom');
192-
link(file:///tmp/tmpk7nc3lox.hdf/apair, file:///tmp/tmpk7nc3lox.hdf/anotherpair, 'bald__references');
193-
link(file:///tmp/tmpk7nc3lox.hdf/data, file:///tmp/tmpk7nc3lox.hdf/alocation, 'bald__references');
194-
link(file:///tmp/tmpk7nc3lox.hdf/discovery, file:///tmp/tmpk7nc3lox.hdf/anotherpair, 'bald__contains', 'top', true);
195-
link(file:///tmp/tmpk7nc3lox.hdf/discovery, file:///tmp/tmpk7nc3lox.hdf/apair, 'bald__contains', 'top', true);
196-
link(file:///tmp/tmpk7nc3lox.hdf/discovery, file:///tmp/tmpk7nc3lox.hdf/source, 'bald__contains', 'top', true);
197-
link(file:///tmp/tmpk7nc3lox.hdf/referencing, file:///tmp/tmpk7nc3lox.hdf/locref, 'bald__contains', 'top', true);
198-
link(file:///tmp/tmpk7nc3lox.hdf/referencing, file:///tmp/tmpk7nc3lox.hdf/locref2, 'bald__contains', 'top', true);
199-
link(file:///tmp/tmpk7nc3lox.hdf/root, file:///tmp/tmpk7nc3lox.hdf/alocation, 'bald__contains', 'top', true);
200-
link(file:///tmp/tmpk7nc3lox.hdf/root, file:///tmp/tmpk7nc3lox.hdf/data, 'bald__contains', 'top', true);
201-
link(file:///tmp/tmpk7nc3lox.hdf/root, file:///tmp/tmpk7nc3lox.hdf/discovery, 'bald__contains', 'top', true);
202-
link(file:///tmp/tmpk7nc3lox.hdf/root, file:///tmp/tmpk7nc3lox.hdf/referencing, 'bald__contains', 'top', true);
203-
link(file:///tmp/tmpk7nc3lox.hdf/source, file:///tmp/tmpk7nc3lox.hdf/institution, 'bald__contains', 'top', true);
177+
var file:///tmp/tmp0ed878yz.hdf/alocation = instance('file:///tmp/tmp0ed878yz.hdf/alocation:<a xlink:href="http://binary-array-ld.net/latest/Array" xlink:show=new text-decoration="underline">bald__Array</a>, <a xlink:href="http://binary-array-ld.net/latest/Reference" xlink:show=new text-decoration="underline">bald__Reference</a>', ['<a xlink:href="http://binary-array-ld.net/latest/array" xlink:show=new text-decoration="underline">bald__array</a>: |', '<a xlink:href="http://binary-array-ld.net/latest/references" xlink:show=new text-decoration="underline">bald__references</a>: |', '<a xlink:href="http://binary-array-ld.net/latest/shape" xlink:show=new text-decoration="underline">bald__shape</a>: 11, 17'], '#878800');
178+
var file:///tmp/tmp0ed878yz.hdf/anotherpair = instance('file:///tmp/tmp0ed878yz.hdf/anotherpair:<a xlink:href="http://binary-array-ld.net/latest/Array" xlink:show=new text-decoration="underline">bald__Array</a>, <a xlink:href="http://binary-array-ld.net/latest/Reference" xlink:show=new text-decoration="underline">bald__Reference</a>', ['<a xlink:href="http://binary-array-ld.net/latest/array" xlink:show=new text-decoration="underline">bald__array</a>: |', '<a xlink:href="http://binary-array-ld.net/latest/shape" xlink:show=new text-decoration="underline">bald__shape</a>: 2'], '#878800');
179+
var file:///tmp/tmp0ed878yz.hdf/apair = instance('file:///tmp/tmp0ed878yz.hdf/apair:<a xlink:href="http://binary-array-ld.net/latest/Array" xlink:show=new text-decoration="underline">bald__Array</a>', ['<a xlink:href="http://binary-array-ld.net/latest/references" xlink:show=new text-decoration="underline">bald__references</a>: |', '<a xlink:href="http://binary-array-ld.net/latest/shape" xlink:show=new text-decoration="underline">bald__shape</a>: 2'], '#878800');
180+
var file:///tmp/tmp0ed878yz.hdf/data = instance('file:///tmp/tmp0ed878yz.hdf/data:<a xlink:href="http://binary-array-ld.net/latest/Array" xlink:show=new text-decoration="underline">bald__Array</a>', ['<a xlink:href="http://binary-array-ld.net/latest/references" xlink:show=new text-decoration="underline">bald__references</a>: |', '<a xlink:href="http://binary-array-ld.net/latest/shape" xlink:show=new text-decoration="underline">bald__shape</a>: 11, 17'], '#878800');
181+
var file:///tmp/tmp0ed878yz.hdf/discovery = instance('file:///tmp/tmp0ed878yz.hdf/discovery:<a xlink:href="http://binary-array-ld.net/latest/Container" xlink:show=new text-decoration="underline">bald__Container</a>', ['<a xlink:href="http://binary-array-ld.net/latest/contains" xlink:show=new text-decoration="underline">bald__contains</a>: |'], '#878800');
182+
var file:///tmp/tmp0ed878yz.hdf/institution = instance('file:///tmp/tmp0ed878yz.hdf/institution:<a xlink:href="http://binary-array-ld.net/latest/Array" xlink:show=new text-decoration="underline">bald__Array</a>', ['<a xlink:href="http://binary-array-ld.net/latest/shape" xlink:show=new text-decoration="underline">bald__shape</a>: |', '<a xlink:href="http://www.w3.org/2004/02/skos/core#prefLabel" xlink:show=new text-decoration="underline">skos__prefLabel</a>: a quality establishment'], '#878800');
183+
var file:///tmp/tmp0ed878yz.hdf/locref = instance('file:///tmp/tmp0ed878yz.hdf/locref:<a xlink:href="http://binary-array-ld.net/latest/Array" xlink:show=new text-decoration="underline">bald__Array</a>', ['<a xlink:href="http://binary-array-ld.net/latest/shape" xlink:show=new text-decoration="underline">bald__shape</a>: |', '<a xlink:href="http://www.w3.org/2004/02/skos/core#prefLabel" xlink:show=new text-decoration="underline">skos__prefLabel</a>: for locational purposes'], '#878800');
184+
var file:///tmp/tmp0ed878yz.hdf/locref2 = instance('file:///tmp/tmp0ed878yz.hdf/locref2:<a xlink:href="http://binary-array-ld.net/latest/Array" xlink:show=new text-decoration="underline">bald__Array</a>', ['<a xlink:href="http://binary-array-ld.net/latest/shape" xlink:show=new text-decoration="underline">bald__shape</a>: |', '<a xlink:href="http://www.w3.org/2004/02/skos/core#prefLabel" xlink:show=new text-decoration="underline">skos__prefLabel</a>: for more locational purposes'], '#878800');
185+
var file:///tmp/tmp0ed878yz.hdf/referencing = instance('file:///tmp/tmp0ed878yz.hdf/referencing:<a xlink:href="http://binary-array-ld.net/latest/Container" xlink:show=new text-decoration="underline">bald__Container</a>', ['<a xlink:href="http://binary-array-ld.net/latest/contains" xlink:show=new text-decoration="underline">bald__contains</a>: |'], '#878800');
186+
var file:///tmp/tmp0ed878yz.hdf/root = instance('file:///tmp/tmp0ed878yz.hdf/root:<a xlink:href="http://binary-array-ld.net/latest/Container" xlink:show=new text-decoration="underline">bald__Container</a>', ['<a xlink:href="http://binary-array-ld.net/latest/contains" xlink:show=new text-decoration="underline">bald__contains</a>: |'], '#878800');
187+
var file:///tmp/tmp0ed878yz.hdf/source = instance('file:///tmp/tmp0ed878yz.hdf/source:<a xlink:href="http://binary-array-ld.net/latest/Container" xlink:show=new text-decoration="underline">bald__Container</a>', ['<a xlink:href="http://binary-array-ld.net/latest/contains" xlink:show=new text-decoration="underline">bald__contains</a>: |'], '#878800');
188+
link(file:///tmp/tmp0ed878yz.hdf/alocation, file:///tmp/tmp0ed878yz.hdf/alocation, 'bald__array', 'bottom');
189+
link(file:///tmp/tmp0ed878yz.hdf/alocation, file:///tmp/tmp0ed878yz.hdf/locref, 'bald__references');
190+
link(file:///tmp/tmp0ed878yz.hdf/alocation, file:///tmp/tmp0ed878yz.hdf/locref2, 'bald__references');
191+
link(file:///tmp/tmp0ed878yz.hdf/anotherpair, file:///tmp/tmp0ed878yz.hdf/anotherpair, 'bald__array', 'bottom');
192+
link(file:///tmp/tmp0ed878yz.hdf/apair, file:///tmp/tmp0ed878yz.hdf/anotherpair, 'bald__references');
193+
link(file:///tmp/tmp0ed878yz.hdf/data, file:///tmp/tmp0ed878yz.hdf/alocation, 'bald__references');
194+
link(file:///tmp/tmp0ed878yz.hdf/discovery, file:///tmp/tmp0ed878yz.hdf/anotherpair, 'bald__contains', 'top', true);
195+
link(file:///tmp/tmp0ed878yz.hdf/discovery, file:///tmp/tmp0ed878yz.hdf/apair, 'bald__contains', 'top', true);
196+
link(file:///tmp/tmp0ed878yz.hdf/discovery, file:///tmp/tmp0ed878yz.hdf/source, 'bald__contains', 'top', true);
197+
link(file:///tmp/tmp0ed878yz.hdf/referencing, file:///tmp/tmp0ed878yz.hdf/locref, 'bald__contains', 'top', true);
198+
link(file:///tmp/tmp0ed878yz.hdf/referencing, file:///tmp/tmp0ed878yz.hdf/locref2, 'bald__contains', 'top', true);
199+
link(file:///tmp/tmp0ed878yz.hdf/root, file:///tmp/tmp0ed878yz.hdf/alocation, 'bald__contains', 'top', true);
200+
link(file:///tmp/tmp0ed878yz.hdf/root, file:///tmp/tmp0ed878yz.hdf/data, 'bald__contains', 'top', true);
201+
link(file:///tmp/tmp0ed878yz.hdf/root, file:///tmp/tmp0ed878yz.hdf/discovery, 'bald__contains', 'top', true);
202+
link(file:///tmp/tmp0ed878yz.hdf/root, file:///tmp/tmp0ed878yz.hdf/referencing, 'bald__contains', 'top', true);
203+
link(file:///tmp/tmp0ed878yz.hdf/source, file:///tmp/tmp0ed878yz.hdf/institution, 'bald__contains', 'top', true);
204204
joint.layout.DirectedGraph.layout(graph, { setLinkVertices: false,
205205
nodeSep: 150, rankSep: 100,
206206
marginX: 100, marginY: 100,

0 commit comments

Comments
 (0)