Skip to content

Commit 7eb741d

Browse files
committed
Cleaned up and made better URL handling.
1 parent 98d5764 commit 7eb741d

File tree

2 files changed

+193
-97
lines changed

2 files changed

+193
-97
lines changed

ncldDump/ncldDump.py

Lines changed: 145 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -193,38 +193,123 @@ def loadAliasDict(aliasFilePath):
193193
return aliasDict
194194

195195

196-
def makeHotLink(word, pattern):
196+
def makeURL(word, pattern):
197197
'''
198-
Create an <a> hot-link string from the word and pattern.
198+
Create a URL from the word and pattern.
199199
200200
word [in] The word to build the hot-link around.
201201
pattern [in] The URL pattern to reference.
202-
returns An <a> hot-link string.
202+
returns A URL.
203203
'''
204204

205205
# Insert the word into any replaceable part in the pattern.
206206
#
207207
theURL = pattern.format(word)
208208

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
211210
#
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
216230

217-
# Return the hot-link string.
231+
# Split the name on '__'.
218232
#
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
220303

221304

222305
def parseAttributes(ncObj, aliasDict):
223306
'''
224307
Build a list of dictionaries for each netCDF attribute on the object.
225308
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.
228313
'''
229314

230315
# Create the attribute list.
@@ -234,72 +319,51 @@ def parseAttributes(ncObj, aliasDict):
234319
# Fill the list with dictionaries describing each attribute.
235320
#
236321
for attrName in ncObj.ncattrs():
322+
# Get the value and type for the attribute.
323+
#
237324
attrValue = ncObj.getncattr(attrName)
238325
attrType = parseType(attrValue)
239326

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.
241328
#
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]
268331

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.
273342
#
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+
287345
# If the value is a string, wrap it in '"' characters.
288346
#
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)
293356

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.
296358
#
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}
301360

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)
303367

304368
# Return the list.
305369
#
@@ -310,16 +374,18 @@ def parseNcObj(ncObj, aliasDict):
310374
'''
311375
Build dimension, variable, and attribute lists for the object.
312376
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.
316382
'''
317383

318384
# Create the top-level dictionary.
319385
#
320386
dataDict = {}
321387

322-
# If there are any dimensions, add and populate a dimensions element.
388+
# If there are any dimensions, add and populate a dimensions entry.
323389
#
324390
dimList = []
325391

@@ -340,7 +406,7 @@ def parseNcObj(ncObj, aliasDict):
340406
if 0 < len(dimList):
341407
dataDict['dimensions'] = dimList
342408

343-
# If there are any variables, add and populate a variables element.
409+
# If there are any variables, add and populate a variables entry.
344410
#
345411
varList = []
346412

@@ -355,26 +421,23 @@ def parseNcObj(ncObj, aliasDict):
355421
for dimName in varObj.dimensions:
356422
dimSize = ncObj.dimensions[dimName].size
357423

358-
dimList.append('{}={}'.format(dimName, dimSize))
424+
dimList.append(dimName)
359425

360426
if 0 < len(dimList):
361427
varEntry['dimensions'] = dimList
362428

363429
# 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.
366431
#
367-
hotLink = varName
368-
369432
if varName in aliasDict['names']:
370433
pattern = aliasDict['names'][varName]
371434

372-
hotLink = makeHotLink(varName, pattern)
435+
theURL = makeURL(varName, pattern)
373436

374-
varEntry['hotLink'] = hotLink
437+
varEntry['url'] = theURL
375438

376439
# If there are any attributes add and populate an attributes
377-
# element.
440+
# entry.
378441
#
379442
attrList = parseAttributes(varObj, aliasDict)
380443

@@ -389,7 +452,7 @@ def parseNcObj(ncObj, aliasDict):
389452
dataDict['variables'] = varList
390453

391454
# If there are any group-level attributes, add and populate an attributes
392-
# element.
455+
# entry.
393456
#
394457
attrList = parseAttributes(ncObj, aliasDict)
395458

0 commit comments

Comments
 (0)