2
2
import contextlib
3
3
import copy
4
4
from difflib import SequenceMatcher
5
+ import json
5
6
import operator
6
7
import os
7
8
import re
@@ -803,7 +804,7 @@ def load(afilepath):
803
804
except NameError :
804
805
pass
805
806
806
- def _prefixes_and_aliases (fhandle , identity , alias_dict , cache ):
807
+ def _prefixes_and_aliases (fhandle , identity , alias_dict , prefix_contexts , cache ):
807
808
# prefixes are defined as group attributes in a dedicated group, and/or
808
809
# by external resources
809
810
prefix_var_name = None
@@ -833,25 +834,52 @@ def _prefixes_and_aliases(fhandle, identity, alias_dict, cache):
833
834
# if k.endswith('__'):
834
835
# prefixes[k] = getattr(fhandle, k)
835
836
836
- prefix_graph = rdflib .Graph ()
837
- for prefix_url in prefix_urls :
838
- res = cache [prefix_url ]
839
- try :
840
- prefix_graph .parse (data = res .text , format = 'xml' )
841
- except Exception :
842
- print ('Failed to parse: {} for prefixes.' .format (prefix_url ))
843
-
844
- qres = prefix_graph .query ("select ?prefix ?uri where \n "
845
- "{\n "
846
- "?s <http://purl.org/vocab/vann/preferredNamespacePrefix> ?prefix ;\n "
847
- "<http://purl.org/vocab/vann/preferredNamespaceUri> ?uri . \n "
848
- "}" )
849
- for res in qres :
850
- key , value = (str (res [0 ]), str (res [1 ]))
851
- if key in prefixes and value != prefixes [key ]:
852
- prefixes .pop (key )
837
+ # prefix_graph = rdflib.Graph()
838
+ # for prefix_url in prefix_urls:
839
+ # res = cache[prefix_url]
840
+ # try:
841
+ # prefix_graph.parse(data=res.text, format='xml')
842
+ # except Exception:
843
+ # print('Failed to parse: {} for prefixes.'.format(prefix_url))
844
+
845
+ # qres = prefix_graph.query("select ?prefix ?uri where \n"
846
+ # "{\n"
847
+ # "?s <http://purl.org/vocab/vann/preferredNamespacePrefix> ?prefix ;\n"
848
+ # "<http://purl.org/vocab/vann/preferredNamespaceUri> ?uri . \n"
849
+ # "}")
850
+ # for res in qres:
851
+ # key, value = (str(res[0]), str(res[1]))
852
+ # if key in prefixes and value !=prefixes[key]:
853
+ # prefixes.pop(key)
854
+ # else:
855
+ # prefixes[key] = value
856
+
857
+ # # check that default set is handled, i.e. bald__ and rdf__
858
+ # if 'bald__' not in prefixes:
859
+ # prefixes['bald__'] = "https://www.opengis.net/def/binary-array-ld/"
860
+
861
+ # if 'rdf__' not in prefixes:
862
+ # prefixes['rdf__'] = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
863
+
864
+ ## query keep above
865
+ context_prefixes = {}
866
+ for prefix_context in prefix_contexts :
867
+ if prefix_context .startswith ('http://' ) or prefix_context .startswith ('https://' ):
868
+ prefcon = json .loads (cache [prefix_context ].text )
853
869
else :
854
- prefixes [key ] = value
870
+ prefcon = json .loads (prefix_context )
871
+ if '@context' in prefcon :
872
+ for key in prefcon ['@context' ]:
873
+ pref = '{}__' .format (key )
874
+ if pref in context_prefixes and context_prefixes [pref ] != prefcon ['@context' ][key ]:
875
+ context_prefixes [pref ] = None
876
+ else :
877
+ context_prefixes [pref ] = prefcon ['@context' ][key ]
878
+ for akey in context_prefixes :
879
+ if context_prefixes [akey ] is None :
880
+ context_prefixes .pop (akey )
881
+
882
+ precedence_update (prefixes , context_prefixes )
855
883
856
884
# check that default set is handled, i.e. bald__ and rdf__
857
885
if 'bald__' not in prefixes :
@@ -960,18 +988,18 @@ def _load_netcdf_group_vars(fhandle, agroup, root_container, baseuri, identity_p
960
988
if isinstance (sattrs ['bald__first_value' ], str ):
961
989
pass
962
990
963
- elif np .issubdtype (sattrs ['bald__first_value' ], np .integer ):
991
+ elif np .issubdtype (sattrs ['bald__first_value' ]. dtype , np .integer ):
964
992
sattrs ['bald__first_value' ] = int (sattrs ['bald__first_value' ])
965
- elif np .issubdtype (sattrs ['bald__first_value' ], np .floating ):
993
+ elif np .issubdtype (sattrs ['bald__first_value' ]. dtype , np .floating ):
966
994
sattrs ['bald__first_value' ] = float (sattrs ['bald__first_value' ])
967
995
if (len (agroup .variables [name ]) > 1 and
968
996
not isinstance (agroup .variables [name ][- 1 ], np .ma .core .MaskedConstant )):
969
997
sattrs ['bald__last_value' ] = agroup .variables [name ][- 1 ]
970
998
if isinstance (sattrs ['bald__last_value' ], str ):
971
999
pass
972
- elif np .issubdtype (sattrs ['bald__last_value' ], np .integer ):
1000
+ elif np .issubdtype (sattrs ['bald__last_value' ]. dtype , np .integer ):
973
1001
sattrs ['bald__last_value' ] = int (sattrs ['bald__last_value' ])
974
- elif np .issubdtype (sattrs ['bald__last_value' ], np .floating ):
1002
+ elif np .issubdtype (sattrs ['bald__last_value' ]. dtype , np .floating ):
975
1003
sattrs ['bald__last_value' ] = float (sattrs ['bald__last_value' ])
976
1004
977
1005
# datetime special case
@@ -1162,14 +1190,18 @@ def _load_netcdf_group_vars(fhandle, agroup, root_container, baseuri, identity_p
1162
1190
aliases , aliasgraph )
1163
1191
1164
1192
1165
- def load_netcdf (afilepath , baseuri = None , alias_dict = None , cache = None , file_locator = None ):
1193
+ def load_netcdf (afilepath , baseuri = None , alias_dict = None , prefix_contexts = None , cache = None , file_locator = None ):
1166
1194
"""
1167
1195
Load a file with respect to binary-array-linked-data.
1168
1196
Returns a :class:`bald.Collection`
1169
1197
"""
1170
1198
1171
- if alias_dict == None :
1199
+ if alias_dict is None :
1172
1200
alias_dict = {}
1201
+ if isinstance (prefix_contexts , str ):
1202
+ prefix_contexts = [prefix_contexts ]
1203
+ elif prefix_contexts is None :
1204
+ prefix_contexts = []
1173
1205
if cache is None :
1174
1206
cache = HttpCache ()
1175
1207
@@ -1183,7 +1215,8 @@ def load_netcdf(afilepath, baseuri=None, alias_dict=None, cache=None, file_locat
1183
1215
1184
1216
identity = baseuri
1185
1217
1186
- prefixes , aliases , aliasgraph , prefix_group_name = _prefixes_and_aliases (fhandle , identity , alias_dict , cache )
1218
+ prefixes , aliases , aliasgraph , prefix_group_name = _prefixes_and_aliases (fhandle , identity , alias_dict ,
1219
+ prefix_contexts , cache )
1187
1220
1188
1221
attrs = {}
1189
1222
for k in fhandle .ncattrs ():
@@ -1337,6 +1370,16 @@ def careful_update(adict, bdict):
1337
1370
adict .update (bdict )
1338
1371
return adict
1339
1372
1373
+ def precedence_update (maindict , updatingdict ):
1374
+ """
1375
+ Carefully updates a main dictionary with an updating dictionary,
1376
+ only inputting new values, and never overwriting values.
1377
+
1378
+ """
1379
+ for akey in updatingdict :
1380
+ if akey not in maindict :
1381
+ maindict [akey ] = updatingdict [akey ]
1382
+
1340
1383
def load_hdf5 (afilepath , baseuri = None , alias_dict = None , cache = None ):
1341
1384
if cache is None :
1342
1385
cache = HttpCache ()
0 commit comments