@@ -193,38 +193,123 @@ def loadAliasDict(aliasFilePath):
193
193
return aliasDict
194
194
195
195
196
- def makeHotLink (word , pattern ):
196
+ def makeURL (word , pattern ):
197
197
'''
198
- Create an <a> hot-link string from the word and pattern.
198
+ Create a URL from the word and pattern.
199
199
200
200
word [in] The word to build the hot-link around.
201
201
pattern [in] The URL pattern to reference.
202
- returns An <a> hot-link string .
202
+ returns A URL .
203
203
'''
204
204
205
205
# Insert the word into any replaceable part in the pattern.
206
206
#
207
207
theURL = pattern .format (word )
208
208
209
- # Create the hot-link string. Make it open a new tab when dereferenced if
210
- # the URL is not relative to the current document.
209
+ # Return the URL
211
210
#
212
- if '#' != theURL [0 ]:
213
- hotLink = '<a href="{}" target="_blank">{}</a>' .format (theURL , word )
214
- else :
215
- hotLink = '<a href="{}">{}</a>' .format (theURL , word )
211
+ return theURL
212
+
213
+
214
+ def resolveName (name , aliasDict ):
215
+ '''
216
+ Determine if the name has a context part (the form is <context>__<name>).
217
+ If it does and the context alias exists, use it to build a URL.
218
+ If not, attempt to resolve the name into a URL using the names
219
+ part of the alias dictionary.
220
+
221
+ name [in] A name to attempt to resolve into a hot-link string.
222
+ aliasDict [in] A dictionary of URI patterns keyed by the elements they
223
+ replace.
224
+ returns A URL, or None if there was no resolution.
225
+ '''
226
+
227
+ # Start with the result equal to None.
228
+ #
229
+ result = None
216
230
217
- # Return the hot-link string .
231
+ # Split the name on '__' .
218
232
#
219
- return hotLink
233
+ nameParts = name .split ('__' )
234
+
235
+ # Breakout context.
236
+ #
237
+ for _x in [0 ]:
238
+ # If there is a context part, attempt to use it.
239
+ #
240
+ if 2 == len (nameParts ):
241
+ # Get the name and context parts.
242
+ #
243
+ contextPart = nameParts [0 ]
244
+ namePart = nameParts [1 ]
245
+
246
+ # If the context exists in the alias dictionary, create a hot-link
247
+ # string using the pattern for the context and the name part.
248
+ #
249
+ if contextPart in aliasDict ['contexts' ]:
250
+ pattern = aliasDict ['contexts' ][contextPart ]
251
+
252
+ result = makeURL (namePart , pattern )
253
+
254
+ break
255
+
256
+ # If the name exists in the alias dictionary, create a hot-link string
257
+ # using the pattern for the name.
258
+ #
259
+ if name in aliasDict ['names' ]:
260
+ pattern = aliasDict ['names' ][name ]
261
+
262
+ result = makeURL (name , pattern )
263
+
264
+ break
265
+
266
+ # Return the resolved URL if one was found.
267
+ #
268
+ return result
269
+
270
+
271
+ def resolveValue (name , value , aliasDict ):
272
+ '''
273
+ Determine if the value associated with the name has an entry in the alias
274
+ dictionary. If it does, build a URL and return it.
275
+
276
+ name [in] A name to attempt to resolve into a hot-link string.
277
+ value [in] A value to attempt to resolve into a hot-link string.
278
+ aliasDict [in] A dictionary of URI patterns keyed by the elements they
279
+ replace.
280
+ returns A URL, or None if there was no resolution.
281
+ '''
282
+
283
+ # Start with the result equal to None.
284
+ #
285
+ result = None
286
+
287
+ # If the name exists in the alias dictionary, and if the value exists in
288
+ # the sub-dictionary for the name, create a hot-link string using the
289
+ # pattern for the value. A wildcard (*) for a value key in the dictionary
290
+ # matches any value.
291
+ #
292
+ if name in aliasDict ['values' ]:
293
+ subDict = aliasDict ['values' ][name ]
294
+
295
+ if value in subDict :
296
+ pattern = subDict [value ]
297
+
298
+ result = makeURL (value , pattern )
299
+
300
+ # Return the resolved name if one was found.
301
+ #
302
+ return result
220
303
221
304
222
305
def parseAttributes (ncObj , aliasDict ):
223
306
'''
224
307
Build a list of dictionaries for each netCDF attribute on the object.
225
308
226
- ncObj [in] A netCDF object with attributes.
227
- returns A list of dictionaries for each attribute.
309
+ ncObj [in] A netCDF object with attributes.
310
+ aliasDict [in] A dictionary of URI patterns keyed by the elements they
311
+ replace.
312
+ returns A list of dictionaries for each attribute.
228
313
'''
229
314
230
315
# Create the attribute list.
@@ -234,72 +319,51 @@ def parseAttributes(ncObj, aliasDict):
234
319
# Fill the list with dictionaries describing each attribute.
235
320
#
236
321
for attrName in ncObj .ncattrs ():
322
+ # Get the value and type for the attribute.
323
+ #
237
324
attrValue = ncObj .getncattr (attrName )
238
325
attrType = parseType (attrValue )
239
326
240
- # If the value is an array, create a list for the contents .
327
+ # If the value is not an array, make it a list.
241
328
#
242
- if True == isinstance (attrValue , numpy .ndarray ):
243
- valueList = []
244
-
245
- for value in attrValue :
246
- # If the attribute name and value are in the alias dictionary
247
- # values section, get a hot-link string for it.
248
- #
249
- if attrName in aliasDict ['values' ]:
250
- subDict = aliasDict ['values' ][attrName ]
251
-
252
- # If the key '*' is present in the sub-dictionary, get the
253
- # item, otherwise get the item keyed by the value.
254
- #
255
- if '*' in subDict :
256
- pattern = subDict ['*' ]
257
- else :
258
- pattern = subDict [value ]
259
-
260
- value = makeHotLink (value , pattern )
261
-
262
- # If the value is a string, wrap it in '"' characters.
263
- #
264
- if True == isinstance (value , str ) or True == isinstance (value , unicode ):
265
- valueList .append ('"' + str (value ) + '"' )
266
- else :
267
- valueList .append (str (value ))
329
+ if False == isinstance (attrValue , numpy .ndarray ):
330
+ attrValue = [attrValue ]
268
331
269
- attrValue = valueList
270
- else :
271
- # If the attribute name and value are in the alias dictionary
272
- # values section, get a hot-link string for it.
332
+ # Get the URL (if any) for the attribute.
333
+ #
334
+ nameURL = resolveName (attrName , aliasDict )
335
+
336
+ # Construct a list of attribute value and URL pairs.
337
+ #
338
+ valueList = []
339
+
340
+ for value in attrValue :
341
+ # Get the URL (if any) for the value.
273
342
#
274
- if attrName in aliasDict ['values' ]:
275
- subDict = aliasDict ['values' ][attrName ]
276
-
277
- # If the key '*' is present in the sub-dictionary, get the
278
- # item, otherwise get the item keyed by the value.
279
- #
280
- if '*' in subDict :
281
- pattern = subDict ['*' ]
282
- else :
283
- pattern = subDict [attrValue ]
284
-
285
- attrValue = makeHotLink (attrValue , pattern )
286
-
343
+ valueURL = resolveValue (attrName , value , aliasDict )
344
+
287
345
# If the value is a string, wrap it in '"' characters.
288
346
#
289
- if True == isinstance (attrValue , str ) or True == isinstance (attrValue , unicode ):
290
- attrValue = '"' + str (attrValue ) + '"'
291
- else :
292
- attrValue = str (attrValue )
347
+ if True == isinstance (value , str ) or True == isinstance (value , unicode ):
348
+ value = '"' + str (value ) + '"'
349
+
350
+ valueEntry = { 'element' : value }
351
+
352
+ if valueURL is not None :
353
+ valueEntry ['url' ] = valueURL
354
+
355
+ valueList .append (valueEntry )
293
356
294
- # If the attribute name is in the alias dictionary names section, get a
295
- # hot-link string for it.
357
+ # Build the attribute entry. If there is a name URL add it.
296
358
#
297
- if attrName in aliasDict ['names' ]:
298
- pattern = aliasDict ['names' ][attrName ]
299
-
300
- attrName = makeHotLink (attrName , pattern )
359
+ attrEntry = {'name' : attrName , 'value' : valueList , 'type' : attrType }
301
360
302
- attrList .append ({'name' : attrName , 'value' : attrValue , 'type' : attrType })
361
+ if nameURL is not None :
362
+ attrEntry ['url' ] = nameURL
363
+
364
+ # Add the entry to the list.
365
+ #
366
+ attrList .append (attrEntry )
303
367
304
368
# Return the list.
305
369
#
@@ -310,16 +374,18 @@ def parseNcObj(ncObj, aliasDict):
310
374
'''
311
375
Build dimension, variable, and attribute lists for the object.
312
376
313
- ncObj [in] The netCDF4 object to parse.
314
- returns A nested set of dictionaries and lists describing the object
315
- contents.
377
+ ncObj [in] The netCDF4 object to parse.
378
+ aliasDict [in] A dictionary of URI patterns keyed by the elements they
379
+ replace.
380
+ returns A nested set of dictionaries and lists describing the object
381
+ contents.
316
382
'''
317
383
318
384
# Create the top-level dictionary.
319
385
#
320
386
dataDict = {}
321
387
322
- # If there are any dimensions, add and populate a dimensions element .
388
+ # If there are any dimensions, add and populate a dimensions entry .
323
389
#
324
390
dimList = []
325
391
@@ -340,7 +406,7 @@ def parseNcObj(ncObj, aliasDict):
340
406
if 0 < len (dimList ):
341
407
dataDict ['dimensions' ] = dimList
342
408
343
- # If there are any variables, add and populate a variables element .
409
+ # If there are any variables, add and populate a variables entry .
344
410
#
345
411
varList = []
346
412
@@ -355,26 +421,23 @@ def parseNcObj(ncObj, aliasDict):
355
421
for dimName in varObj .dimensions :
356
422
dimSize = ncObj .dimensions [dimName ].size
357
423
358
- dimList .append ('{}={}' . format ( dimName , dimSize ) )
424
+ dimList .append (dimName )
359
425
360
426
if 0 < len (dimList ):
361
427
varEntry ['dimensions' ] = dimList
362
428
363
429
# If the variable name is in the alias dictionary names section,
364
- # get a hot-link string for it and add it to the entry for the
365
- # variable. If not, just use the name.
430
+ # get a URL for it and add it to the entry for the variable.
366
431
#
367
- hotLink = varName
368
-
369
432
if varName in aliasDict ['names' ]:
370
433
pattern = aliasDict ['names' ][varName ]
371
434
372
- hotLink = makeHotLink (varName , pattern )
435
+ theURL = makeURL (varName , pattern )
373
436
374
- varEntry ['hotLink ' ] = hotLink
437
+ varEntry ['url ' ] = theURL
375
438
376
439
# If there are any attributes add and populate an attributes
377
- # element .
440
+ # entry .
378
441
#
379
442
attrList = parseAttributes (varObj , aliasDict )
380
443
@@ -389,7 +452,7 @@ def parseNcObj(ncObj, aliasDict):
389
452
dataDict ['variables' ] = varList
390
453
391
454
# If there are any group-level attributes, add and populate an attributes
392
- # element .
455
+ # entry .
393
456
#
394
457
attrList = parseAttributes (ncObj , aliasDict )
395
458
0 commit comments