1
1
from __future__ import print_function
2
+ from six import string_types , PY2
2
3
3
4
import argparse
4
5
import jinja2
9
10
import re
10
11
import sys
11
12
13
+ import pprint
14
+ import traceback
12
15
13
16
def parseArgs (args ):
14
17
'''
@@ -100,9 +103,7 @@ def parseType(obj):
100
103
#
101
104
result = '?'
102
105
103
- if True == isinstance (obj , unicode ):
104
- result = ''
105
- elif True == isinstance (obj , str ):
106
+ if True == isinstance (obj , string_types ):
106
107
result = ''
107
108
elif True == isinstance (obj , numpy .int8 ):
108
109
result = 'b'
@@ -135,24 +136,27 @@ def parseType(obj):
135
136
def convertToStringHook (item , ignoreDicts = False ):
136
137
'''
137
138
This function is passed to the json load function as an object hook. It
138
- converts any unicode strings into ASCII strings.
139
+ converts any string_types strings into ASCII strings.
139
140
140
141
item [in] An item passed in for processing.
141
142
ignoreDicts [in] If this is set to True ignore any dict objects passed in.
142
- returns Items with any unicode strings converted to ASCII.
143
+ returns Items with any string_types strings converted to ASCII.
143
144
'''
144
145
145
- # If this is a unicode string, convert it. If this is a list, convert any
146
- # contained unicode strings. If this is a dict and it hasn't been converted
147
- # already, convert any contained unicode strings. Otherwise, leave the item
146
+ # If this is a string_types string, convert it. If this is a list, convert any
147
+ # contained string_types strings. If this is a dict and it hasn't been converted
148
+ # already, convert any contained string_types strings. Otherwise, leave the item
148
149
# alone.
149
150
#
150
- if isinstance (item , unicode ):
151
- result = item .encode ('utf-8' )
151
+ if isinstance (item , string_types ):
152
+ if PY2 :
153
+ result = item .encode ('utf-8' )
154
+ else :
155
+ result = item
152
156
elif isinstance (item , list ):
153
157
result = [ convertToStringHook (element , True ) for element in item ]
154
158
elif isinstance (item , dict ) and not ignoreDicts :
155
- result = { convertToStringHook (key , True ) : convertToStringHook (value , True ) for key , value in item .iteritems () }
159
+ result = { convertToStringHook (key , True ) : convertToStringHook (value , True ) for key , value in item .items () }
156
160
else :
157
161
result = item
158
162
@@ -294,7 +298,7 @@ def resolveValue(name, value, aliasDict):
294
298
295
299
# If the value is not a string, get a string representation.
296
300
#
297
- if False == isinstance (value , str ) and False == isinstance (value , unicode ):
301
+ if False == isinstance (value , str ) and False == isinstance (value , string_types ):
298
302
value = str (value )
299
303
300
304
# If the value starts with 'http', interpret the entire string as a
@@ -379,7 +383,7 @@ def parseAttributes(ncObj, aliasDict):
379
383
380
384
# If the value is a string, wrap it in '"' characters.
381
385
#
382
- if True == isinstance (attrValue , str ) or True == isinstance (attrValue , unicode ):
386
+ if True == isinstance (attrValue , str ) or True == isinstance (attrValue , string_types ):
383
387
attrValue = '"' + str (attrValue ) + '"'
384
388
385
389
valueEntry = { 'element' : attrValue }
@@ -424,7 +428,7 @@ def parseGroup(ncObj, aliasDict):
424
428
dimList = []
425
429
426
430
try :
427
- for dimName , dimObj in ncObj .dimensions .iteritems ():
431
+ for dimName , dimObj in ncObj .dimensions .items ():
428
432
dimEntry = {'name' : dimName }
429
433
430
434
if True == dimObj .isunlimited ():
@@ -445,7 +449,7 @@ def parseGroup(ncObj, aliasDict):
445
449
varList = []
446
450
447
451
try :
448
- for varName , varObj in ncObj .variables .iteritems ():
452
+ for varName , varObj in ncObj .variables .items ():
449
453
varType = parseDtype (varObj .dtype )
450
454
451
455
varEntry = {'name' : varName , 'type' : varType }
@@ -480,6 +484,8 @@ def parseGroup(ncObj, aliasDict):
480
484
481
485
varList .append (varEntry )
482
486
except :
487
+ #type_, value_, traceback_ = sys.exc_info()
488
+ #tb = traceback.format_tb(traceback_)
483
489
pass
484
490
485
491
if 0 < len (varList ):
@@ -522,7 +528,7 @@ def parseDataset(ncObj, aliasDict):
522
528
523
529
# If there are any other groups, add them as well.
524
530
#
525
- for groupName , groupObj in ncObj .groups .iteritems ():
531
+ for groupName , groupObj in ncObj .groups .items ():
526
532
groupEntry = parseGroup (groupObj , aliasDict )
527
533
528
534
groupEntry ['groupName' ] = groupName
0 commit comments