six.PY conditionals for utf8 encoding/decoding

This commit is contained in:
Michael James 2018-10-09 14:24:43 -06:00
parent 580453acf0
commit dce5a7a25a
10 changed files with 174 additions and 56 deletions

View file

@ -13,6 +13,7 @@
# #
from awips.dataaccess import IData from awips.dataaccess import IData
import six
class PyData(IData): class PyData(IData):
@ -33,6 +34,10 @@ class PyData(IData):
return self.__time return self.__time
def getLevel(self): def getLevel(self):
if six.PY2:
return self.__level
if type(self.__level) is not str:
return self.__level.decode('utf-8')
return self.__level return self.__level
def getLocationName(self): def getLocationName(self):

View file

@ -19,6 +19,7 @@
from awips.dataaccess import IGeometryData from awips.dataaccess import IGeometryData
from awips.dataaccess import PyData from awips.dataaccess import PyData
import six
class PyGeometryData(IGeometryData, PyData.PyData): class PyGeometryData(IGeometryData, PyData.PyData):
@ -34,15 +35,22 @@ class PyGeometryData(IGeometryData, PyData.PyData):
return self.__geometry return self.__geometry
def getParameters(self): def getParameters(self):
if six.PY2:
return list(self.__dataMap.keys()) return list(self.__dataMap.keys())
else:
return [x.decode('utf-8') for x in list(self.__dataMap.keys())]
def getString(self, param): def getString(self, param):
value = self.__dataMap[param][0] if six.PY2:
return self.__dataMap[param][0]
value = self.__dataMap[param.encode('utf-8')][0]
if value is not None:
return value.decode('utf-8')
return value return value
def getNumber(self, param): def getNumber(self, param):
t = self.getType(param) t = self.getType(param)
value = self.__dataMap[param][0] value = self.__dataMap[param.encode('utf-8')][0]
if t == 'INT' or t == 'SHORT' or t == 'LONG': if t == 'INT' or t == 'SHORT' or t == 'LONG':
return int(value) return int(value)
elif t == 'FLOAT': elif t == 'FLOAT':
@ -54,8 +62,16 @@ class PyGeometryData(IGeometryData, PyData.PyData):
def getUnit(self, param): def getUnit(self, param):
unit = self.__dataMap[param][2] unit = self.__dataMap[param][2]
if six.PY2:
return unit
if unit is not None:
return unit.decode('utf-8')
return unit return unit
def getType(self, param): def getType(self, param):
type = self.__dataMap[param][1] if six.PY2:
return self.__dataMap[param][1]
type = self.__dataMap[param.encode('utf-8')][1]
if type is not None:
return type.decode('utf-8')
return type return type

View file

@ -1,6 +1,3 @@
# #
# #
# #
# Implements IGridData for use by native Python clients to the Data Access # Implements IGridData for use by native Python clients to the Data Access
# Framework. # Framework.
@ -20,6 +17,7 @@
import numpy import numpy
import warnings import warnings
import six
from awips.dataaccess import IGridData from awips.dataaccess import IGridData
from awips.dataaccess import PyData from awips.dataaccess import PyData
@ -46,6 +44,10 @@ class PyGridData(IGridData, PyData.PyData):
return self.__parameter return self.__parameter
def getUnit(self): def getUnit(self):
if six.PY2:
return self.__unit
if self.__unit is not None and type(self.__unit) is not str:
return self.__unit.decode('utf-8')
return self.__unit return self.__unit
def getRawData(self, unit=None): def getRawData(self, unit=None):

View file

@ -1,6 +1,3 @@
# #
# #
# #
# Routes requests to the Data Access Framework through Python Thrift. # Routes requests to the Data Access Framework through Python Thrift.
# #
@ -28,6 +25,7 @@
import numpy import numpy
import six
import shapely.wkb import shapely.wkb
from dynamicserialize.dstypes.com.raytheon.uf.common.dataaccess.impl import DefaultDataRequest from dynamicserialize.dstypes.com.raytheon.uf.common.dataaccess.impl import DefaultDataRequest
@ -125,6 +123,9 @@ class ThriftClientRouter(object):
retVal = [] retVal = []
for gridDataRecord in response.getGridData(): for gridDataRecord in response.getGridData():
locationName = gridDataRecord.getLocationName() locationName = gridDataRecord.getLocationName()
if locationName is not None:
locData = locSpecificData[locationName.encode('utf-8')]
else:
locData = locSpecificData[locationName] locData = locSpecificData[locationName]
if self._lazyLoadGridLatLon: if self._lazyLoadGridLatLon:
retVal.append(PyGridData.PyGridData(gridDataRecord, locData[ retVal.append(PyGridData.PyGridData(gridDataRecord, locData[
@ -163,12 +164,20 @@ class ThriftClientRouter(object):
locNamesRequest = GetAvailableLocationNamesRequest() locNamesRequest = GetAvailableLocationNamesRequest()
locNamesRequest.setRequestParameters(request) locNamesRequest.setRequestParameters(request)
response = self._client.sendRequest(locNamesRequest) response = self._client.sendRequest(locNamesRequest)
return [item.decode('utf8') for item in response] if six.PY2:
return response
if response is not None:
return [x.decode('utf-8') for x in response]
return response
def getAvailableParameters(self, request): def getAvailableParameters(self, request):
paramReq = GetAvailableParametersRequest() paramReq = GetAvailableParametersRequest()
paramReq.setRequestParameters(request) paramReq.setRequestParameters(request)
response = self._client.sendRequest(paramReq) response = self._client.sendRequest(paramReq)
if six.PY2:
return response
if response is not None:
return [x.decode('utf-8') for x in response]
return response return response
def getAvailableLevels(self, request): def getAvailableLevels(self, request):
@ -184,6 +193,10 @@ class ThriftClientRouter(object):
idReq = GetRequiredIdentifiersRequest() idReq = GetRequiredIdentifiersRequest()
idReq.setRequest(request) idReq.setRequest(request)
response = self._client.sendRequest(idReq) response = self._client.sendRequest(idReq)
if six.PY2:
return response
if response is not None:
return [x.decode('utf-8') for x in response]
return response return response
def getOptionalIdentifiers(self, request): def getOptionalIdentifiers(self, request):
@ -193,6 +206,10 @@ class ThriftClientRouter(object):
idReq = GetOptionalIdentifiersRequest() idReq = GetOptionalIdentifiersRequest()
idReq.setRequest(request) idReq.setRequest(request)
response = self._client.sendRequest(idReq) response = self._client.sendRequest(idReq)
if six.PY2:
return response
if response is not None:
return [x.decode('utf-8') for x in response]
return response return response
def getIdentifierValues(self, request, identifierKey): def getIdentifierValues(self, request, identifierKey):
@ -200,6 +217,10 @@ class ThriftClientRouter(object):
idValReq.setIdentifierKey(identifierKey) idValReq.setIdentifierKey(identifierKey)
idValReq.setRequestParameters(request) idValReq.setRequestParameters(request)
response = self._client.sendRequest(idValReq) response = self._client.sendRequest(idValReq)
if six.PY2:
return response
if response is not None:
return [x.decode('utf-8') for x in response]
return response return response
def newDataRequest(self, datatype, parameters=[], levels=[], locationNames=[], envelope=None, **kwargs): def newDataRequest(self, datatype, parameters=[], levels=[], locationNames=[], envelope=None, **kwargs):
@ -221,6 +242,10 @@ class ThriftClientRouter(object):
def getSupportedDatatypes(self): def getSupportedDatatypes(self):
response = self._client.sendRequest(GetSupportedDatatypesRequest()) response = self._client.sendRequest(GetSupportedDatatypesRequest())
if six.PY2:
return response
if response is not None:
return [x.decode('utf-8') for x in response]
return response return response
def getNotificationFilter(self, request): def getNotificationFilter(self, request):

View file

@ -73,7 +73,7 @@ class DafTestCase(unittest.TestCase):
return times return times
def testDatatypeIsSupported(self): def testDatatypeIsSupported(self):
allSupported = (item.lower().decode('utf-8') for item in DAL.getSupportedDatatypes()) allSupported = DAL.getSupportedDatatypes()
self.assertIn(self.datatype.lower(), allSupported) self.assertIn(self.datatype.lower(), allSupported)
def testGetRequiredIdentifiers(self): def testGetRequiredIdentifiers(self):

View file

@ -17,6 +17,7 @@ import time, sys
import threading import threading
import dynamicserialize import dynamicserialize
from io import open
TIME_TO_SLEEP = 300 TIME_TO_SLEEP = 300

View file

@ -24,6 +24,8 @@
from thrift.Thrift import TType from thrift.Thrift import TType
import inspect import inspect
import sys import sys
import types
import six
import numpy import numpy
import dynamicserialize import dynamicserialize
from dynamicserialize import dstypes, adapters from dynamicserialize import dstypes, adapters
@ -49,6 +51,31 @@ def buildObjMap(module):
buildObjMap(dstypes) buildObjMap(dstypes)
if six.PY2:
pythonToThriftMap = {
types.StringType: TType.STRING,
types.IntType: TType.I32,
types.LongType: TType.I64,
types.ListType: TType.LIST,
unicode: TType.STRING,
types.DictionaryType: TType.MAP,
type(set([])): TType.SET,
types.FloatType: SelfDescribingBinaryProtocol.FLOAT,
# types.FloatType: TType.DOUBLE,
types.BooleanType: TType.BOOL,
types.InstanceType: TType.STRUCT,
types.NoneType: TType.VOID,
numpy.float32: SelfDescribingBinaryProtocol.FLOAT,
numpy.int32: TType.I32,
numpy.ndarray: TType.LIST,
numpy.object_: TType.STRING, # making an assumption here
numpy.string_: TType.STRING,
numpy.float64: TType.DOUBLE,
numpy.int16: TType.I16,
numpy.int8: TType.BYTE,
numpy.int64: TType.I64
}
else:
pythonToThriftMap = { pythonToThriftMap = {
bytes: TType.STRING, bytes: TType.STRING,
int: TType.I32, int: TType.I32,
@ -227,6 +254,9 @@ class ThriftSerializationContext(object):
if pyt in pythonToThriftMap: if pyt in pythonToThriftMap:
return pythonToThriftMap[pyt] return pythonToThriftMap[pyt]
elif pyt.__module__[:DS_LEN - 1] == ('dynamicserialize.dstypes'): elif pyt.__module__[:DS_LEN - 1] == ('dynamicserialize.dstypes'):
if six.PY2:
return pythonToThriftMap[types.InstanceType]
else:
return pythonToThriftMap[object] return pythonToThriftMap[object]
else: else:
raise dynamicserialize.SerializationException( raise dynamicserialize.SerializationException(

View file

@ -1,12 +1,10 @@
##
##
# File auto-generated against equivalent DynamicSerialize Java class # File auto-generated against equivalent DynamicSerialize Java class
import abc import abc
from six import with_metaclass import six
class AbstractResponseData(with_metaclass(abc.ABCMeta, object)):
class AbstractResponseData(six.with_metaclass(abc.ABCMeta, object)):
@abc.abstractmethod @abc.abstractmethod
def __init__(self): def __init__(self):
self.time = None self.time = None
@ -27,13 +25,20 @@ class AbstractResponseData(with_metaclass(abc.ABCMeta, object)):
self.level = level self.level = level
def getLocationName(self): def getLocationName(self):
if six.PY2:
return self.locationName
if self.locationName is not None:
return self.locationName.decode('utf-8')
return self.locationName return self.locationName
def setLocationName(self, locationName): def setLocationName(self, locationName):
self.locationName = locationName self.locationName = locationName
def getAttributes(self): def getAttributes(self):
if six.PY2:
return self.attributes return self.attributes
else:
return [item.decode('utf-8') for item in self.attributes]
def setAttributes(self, attributes): def setAttributes(self, attributes):
self.attributes = attributes self.attributes = attributes

View file

@ -1,6 +1,3 @@
##
##
# File auto-generated against equivalent DynamicSerialize Java class # File auto-generated against equivalent DynamicSerialize Java class
# and then modified post-generation to use AbstractResponseData. # and then modified post-generation to use AbstractResponseData.
# #
@ -12,8 +9,9 @@
# #
# #
from dynamicserialize.dstypes.com.raytheon.uf.common.dataaccess.response import AbstractResponseData from dynamicserialize.dstypes.com.raytheon.uf.common.dataaccess.response import AbstractResponseData
import six
class GridResponseData(AbstractResponseData): class GridResponseData(AbstractResponseData):
@ -24,12 +22,20 @@ class GridResponseData(AbstractResponseData):
self.gridData = None self.gridData = None
def getParameter(self): def getParameter(self):
if six.PY2:
return self.parameter
if self.parameter is not None:
return self.parameter.decode('utf-8')
return self.parameter return self.parameter
def setParameter(self, parameter): def setParameter(self, parameter):
self.parameter = parameter self.parameter = parameter
def getUnit(self): def getUnit(self):
if six.PY2:
return self.unit
if self.unit is not None:
return self.unit.decode('utf-8')
return self.unit return self.unit
def setUnit(self, unit): def setUnit(self, unit):

View file

@ -1,6 +1,3 @@
##
##
# File auto-generated against equivalent DynamicSerialize Java class # File auto-generated against equivalent DynamicSerialize Java class
# and then modified post-generation to add additional features to better # and then modified post-generation to add additional features to better
# match Java implementation. # match Java implementation.
@ -15,6 +12,9 @@
# #
# #
import six
class MasterLevel(object): class MasterLevel(object):
def __init__(self, name=None): def __init__(self, name=None):
@ -37,39 +37,67 @@ class MasterLevel(object):
return not self.__eq__(other) return not self.__eq__(other)
def __str__(self): def __str__(self):
if six.PY2:
retVal = "MasterLevel[" retVal = "MasterLevel["
retVal += "name=" + str(self.name) + "," retVal += "name=" + str(self.name) + ","
retVal += "type=" + str(self.type) + "," retVal += "type=" + str(self.type) + ","
retVal += "unit=" + str(self.unitString) + "," retVal += "unit=" + str(self.unitString) + ","
retVal += "description=" + str(self.description) retVal += "description=" + str(self.description)
retVal += "]" retVal += "]"
else:
retVal = "MasterLevel["
retVal += "name=" + str(self.name.decode('utf-8')) + ","
retVal += "type=" + str(self.type.decode('utf-8')) + ","
retVal += "unit=" + str(self.unitString.decode('utf-8')) + ","
retVal += "description=" + str(self.description.decode('utf-8'))
retVal += "]"
return retVal return retVal
def getName(self): def getName(self):
if six.PY2:
return self.name
if self.name is not None and type(self.name) is not str:
return self.name.decode('utf-8')
return self.name return self.name
def setName(self, name): def setName(self, name):
self.name = name self.name = name
def getDescription(self): def getDescription(self):
if six.PY2:
return self.description
if self.description is not None:
return self.description.decode('utf-8')
return self.description return self.description
def setDescription(self, description): def setDescription(self, description):
self.description = description self.description = description
def getUnitString(self): def getUnitString(self):
if six.PY2:
return self.unitString
if self.unitString is not None:
return self.unitString.decode('utf-8')
return self.unitString return self.unitString
def setUnitString(self, unitString): def setUnitString(self, unitString):
self.unitString = unitString self.unitString = unitString
def getType(self): def getType(self):
if six.PY2:
return self.type
if self.type is not None:
return self.type.decode('utf-8')
return self.type return self.type
def setType(self, type): def setType(self, type):
self.type = type self.type = type
def getIdentifier(self): def getIdentifier(self):
if six.PY2:
return self.identifier
if self.identifier is not None:
return self.identifier.decode('utf-8')
return self.identifier return self.identifier
def setIdentifier(self, identifier): def setIdentifier(self, identifier):