adds changes for handling different string handling in python3

This commit is contained in:
Ronald Steinke 2019-01-11 17:47:07 +01:00
parent e23d6b7e9f
commit 2720e7d65d
8 changed files with 37 additions and 37 deletions

View File

@ -115,7 +115,7 @@ class mqttConnector(XAE):
# check payload
try:
readings = json_decode(
base64decode(json_decode(payload)['m2m:cin']['con']))
base64decode(json_decode(payload)['m2m:cin']['con']).decode('utf-8'))
except (ValueError, KeyError, TypeError):
self.logger.error('Damaged payload; discarding')
return

View File

@ -203,7 +203,7 @@ class OneM2MHTTPClient(OneM2MClient):
get_response_status(rsc),
request=onem2m_request,
rsc=rsc,
pc=decode_onem2m_content(response.read(), response.get("content-type"))
pc=decode_onem2m_content(response.read().decode("utf-8"), response.get("content-type"))
)
def send_onem2m_request(self, onem2m_request):

View File

@ -2,7 +2,8 @@ from enum import IntEnum, unique
from openmtc.model import (Resource as Res, UnicodeAttribute, DatetimeAttribute,
Attribute, ListAttribute, Entity, EntityAttribute,
AnyURI, StringListAttribute, ContentResource)
AnyURI, StringListAttribute, ContentResource,
BytesAttribute)
from openmtc.model.exc import ModelTypeError
from futile import issubclass
@ -600,7 +601,7 @@ class ResourceC(LabeledResource):
typename = None
resourceName = UnicodeAttribute(accesstype=Attribute.WO)
resourceName = UnicodeAttribute(accesstype=Attribute.WO, mandatory=False)
resourceType = EntityAttribute(ResourceTypeE, accesstype=Attribute.RO)
resourceID = IDS(accesstype=Attribute.RO)
@ -789,8 +790,7 @@ class Subscription(RegularResourceC):
notificationForwardingURI = Attribute(AnyURI)
batchNotify = EntityAttribute(BatchNotify)
rateLimit = EntityAttribute(RateLimit)
preSubscriptionNotify = Attribute(int, accesstype=Attribute.WO,
mandatory=False)
preSubscriptionNotify = Attribute(int, accesstype=Attribute.WO, mandatory=False)
pendingNotification = Attribute(PendingNotificationE)
notificationStoragePriority = Attribute(int)
latestNotify = Attribute(bool)
@ -991,8 +991,8 @@ class ContentInstance(AnnounceableSubordinateResourceC,
# ex: application/json:1
contentInfo = UnicodeAttribute() # m2m:contentInfo
contentSize = Attribute(int, accesstype=Attribute.RO)
ontologyRef = UnicodeAttribute(accesstype=Attribute.WO)
content = Attribute(bytes, accesstype=Attribute.WO, mandatory=True)
ontologyRef = UnicodeAttribute(accesstype=Attribute.WO, mandatory=False)
content = BytesAttribute(accesstype=Attribute.WO, mandatory=True)
__child_types__ = (
Subscription,
@ -1004,8 +1004,8 @@ class ContentInstanceAnnc(AnnouncedSubordinateResourceC):
stateTag = Attribute(int, accesstype=Attribute.RO)
contentInfo = UnicodeAttribute(EncodingTypeE) # m2m:contentInfo
contentSize = Attribute(int, accesstype=Attribute.WO)
ontologyRef = UnicodeAttribute(accesstype=Attribute.WO)
content = Attribute(bytes, accesstype=Attribute.WO, mandatory=True)
ontologyRef = UnicodeAttribute(accesstype=Attribute.WO, mandatory=False)
content = BytesAttribute(accesstype=Attribute.WO, mandatory=True)
################################################################################
@ -1107,7 +1107,7 @@ class AEAnnc(AnnouncedResourceC, SubscribableResource):
typename = "AEAnnc"
appName = UnicodeAttribute(accesstype=Attribute.WO)
appName = UnicodeAttribute(accesstype=Attribute.WO, mandatory=False)
App_ID = UnicodeAttribute()
AE_ID = UnicodeAttribute()
pointOfAccess = StringListAttribute()

View File

@ -33,6 +33,8 @@ def _default(x):
return x.strftime("%Y%m%dT%H%M%S")
elif isinstance(x, ContentInstance):
return x.resourceID
elif isinstance(x, bytes):
return x.decode('utf-8')
else:
try: # handle model classes
return x.values

View File

@ -6,7 +6,7 @@ logger = get_logger(__name__)
def decode_onem2m_content(content, content_type):
if content == "":
if not content:
content = None
if content_type and content is not None:
serializer = get_onem2m_decoder(content_type)

View File

@ -76,7 +76,7 @@ class Collection(Sequence, Mapping):
class Member(LoggerMixin):
def __init__(self, type=unicode, version="1.0", *args, **kw):
def __init__(self, type=str, version="1.0", *args, **kw):
super(Member, self).__init__(*args, **kw)
self.type = type
self.version = version
@ -109,7 +109,7 @@ class Attribute(Member):
RO = "RO"
WO = "WO"
def __init__(self, type=unicode, default=None,
def __init__(self, type=str, default=None,
accesstype=None, mandatory=None,
update_mandatory=None,
id_attribute=None, path_attribute=None,
@ -157,24 +157,22 @@ class Attribute(Member):
return self.default
try:
unicode
class BytesAttribute(Attribute):
def __init__(self, default=None, accesstype=None,
mandatory=None, *args, **kw):
super(BytesAttribute, self).__init__(type=bytes,
default=default,
accesstype=accesstype,
mandatory=mandatory, *args,
**kw)
class UnicodeAttribute(Attribute):
def __init__(self, default=None, accesstype=None,
mandatory=False, *args, **kw):
super(UnicodeAttribute, self).__init__(type=unicode,
default=default,
accesstype=accesstype,
mandatory=mandatory, *args,
**kw)
def convert(self, value, instance):
if isinstance(value, str):
return bytes(value, "utf-8")
return super(BytesAttribute, self).convert(value, instance)
def convert(self, value, instance):
if isinstance(value, str):
return value.decode("utf-8")
return super(UnicodeAttribute, self).convert(value, instance)
except NameError:
UnicodeAttribute = Attribute
UnicodeAttribute = Attribute
class DatetimeAttribute(Attribute):
@ -187,7 +185,7 @@ class DatetimeAttribute(Attribute):
**kw)
def convert(self, value, instance):
if isinstance(value, basestring):
if isinstance(value, str):
try:
return parse_date(value)
except ParseError as e:
@ -196,7 +194,7 @@ class DatetimeAttribute(Attribute):
class ListAttribute(Attribute):
def __init__(self, content_type=unicode, type=list,
def __init__(self, content_type=str, type=list,
default=NOT_SET, *args, **kw):
super(ListAttribute, self).__init__(type=type,
default=default, *args, **kw)
@ -239,7 +237,7 @@ class ListAttribute(Attribute):
class StringListAttribute(Attribute):
def __init__(self, content_type=unicode, type=list,
def __init__(self, content_type=str, type=list,
default=NOT_SET, *args, **kw):
super(StringListAttribute, self).__init__(type=type, default=default,
*args, **kw)

View File

@ -499,14 +499,14 @@ class XAE(LoggerMixin):
cnf = fmt + ':' + str(EncodingTypeE.plain.value)
# raise CSENotImplemented("Only json as b64 is supported!")
else:
con = b64encode(json_dumps(content))
con = b64encode(json_dumps(content).encode('utf-8'))
cnf = fmt + ':' + str(EncodingTypeE.base64String.value)
elif fmt == 'text/plain':
if text:
con = content
cnf = fmt + ':' + str(EncodingTypeE.plain.value)
else:
con = b64encode(content)
con = b64encode(content.encode('utf-8'))
cnf = fmt + ':' + str(EncodingTypeE.base64String.value)
else:
# TODO(rst): add handling of other formats or raise not implemented

View File

@ -1358,7 +1358,7 @@ class ContentInstanceController(OneM2MDefaultController):
super(ContentInstanceController,
self)._set_mandatory_create_attributes(vals)
vals["contentSize"] = len(vals["content"].encode('utf-8'))
vals["contentSize"] = len(vals["content"])
if not vals.get("contentInfo"):
vals["contentInfo"] = 'text/plain:0'
@ -1421,7 +1421,7 @@ class SemanticDescriptorController(OneM2MDefaultController):
@staticmethod
def _check_descriptor_data(descriptor_data):
try:
data = base64.b64decode(descriptor_data)
data = base64.b64decode(descriptor_data).decode('utf-8')
except binascii.Error:
raise CSEContentsUnacceptable("The descriptor was not correctly base64 encoded.")