mirror of
https://github.com/OpenMTC/OpenMTC.git
synced 2024-12-19 04:58:01 +00:00
adds changes for handling different string handling in python3
This commit is contained in:
parent
e23d6b7e9f
commit
2720e7d65d
@ -115,7 +115,7 @@ class mqttConnector(XAE):
|
|||||||
# check payload
|
# check payload
|
||||||
try:
|
try:
|
||||||
readings = json_decode(
|
readings = json_decode(
|
||||||
base64decode(json_decode(payload)['m2m:cin']['con']))
|
base64decode(json_decode(payload)['m2m:cin']['con']).decode('utf-8'))
|
||||||
except (ValueError, KeyError, TypeError):
|
except (ValueError, KeyError, TypeError):
|
||||||
self.logger.error('Damaged payload; discarding')
|
self.logger.error('Damaged payload; discarding')
|
||||||
return
|
return
|
||||||
|
@ -203,7 +203,7 @@ class OneM2MHTTPClient(OneM2MClient):
|
|||||||
get_response_status(rsc),
|
get_response_status(rsc),
|
||||||
request=onem2m_request,
|
request=onem2m_request,
|
||||||
rsc=rsc,
|
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):
|
def send_onem2m_request(self, onem2m_request):
|
||||||
|
@ -2,7 +2,8 @@ from enum import IntEnum, unique
|
|||||||
|
|
||||||
from openmtc.model import (Resource as Res, UnicodeAttribute, DatetimeAttribute,
|
from openmtc.model import (Resource as Res, UnicodeAttribute, DatetimeAttribute,
|
||||||
Attribute, ListAttribute, Entity, EntityAttribute,
|
Attribute, ListAttribute, Entity, EntityAttribute,
|
||||||
AnyURI, StringListAttribute, ContentResource)
|
AnyURI, StringListAttribute, ContentResource,
|
||||||
|
BytesAttribute)
|
||||||
from openmtc.model.exc import ModelTypeError
|
from openmtc.model.exc import ModelTypeError
|
||||||
from futile import issubclass
|
from futile import issubclass
|
||||||
|
|
||||||
@ -600,7 +601,7 @@ class ResourceC(LabeledResource):
|
|||||||
|
|
||||||
typename = None
|
typename = None
|
||||||
|
|
||||||
resourceName = UnicodeAttribute(accesstype=Attribute.WO)
|
resourceName = UnicodeAttribute(accesstype=Attribute.WO, mandatory=False)
|
||||||
|
|
||||||
resourceType = EntityAttribute(ResourceTypeE, accesstype=Attribute.RO)
|
resourceType = EntityAttribute(ResourceTypeE, accesstype=Attribute.RO)
|
||||||
resourceID = IDS(accesstype=Attribute.RO)
|
resourceID = IDS(accesstype=Attribute.RO)
|
||||||
@ -789,8 +790,7 @@ class Subscription(RegularResourceC):
|
|||||||
notificationForwardingURI = Attribute(AnyURI)
|
notificationForwardingURI = Attribute(AnyURI)
|
||||||
batchNotify = EntityAttribute(BatchNotify)
|
batchNotify = EntityAttribute(BatchNotify)
|
||||||
rateLimit = EntityAttribute(RateLimit)
|
rateLimit = EntityAttribute(RateLimit)
|
||||||
preSubscriptionNotify = Attribute(int, accesstype=Attribute.WO,
|
preSubscriptionNotify = Attribute(int, accesstype=Attribute.WO, mandatory=False)
|
||||||
mandatory=False)
|
|
||||||
pendingNotification = Attribute(PendingNotificationE)
|
pendingNotification = Attribute(PendingNotificationE)
|
||||||
notificationStoragePriority = Attribute(int)
|
notificationStoragePriority = Attribute(int)
|
||||||
latestNotify = Attribute(bool)
|
latestNotify = Attribute(bool)
|
||||||
@ -991,8 +991,8 @@ class ContentInstance(AnnounceableSubordinateResourceC,
|
|||||||
# ex: application/json:1
|
# ex: application/json:1
|
||||||
contentInfo = UnicodeAttribute() # m2m:contentInfo
|
contentInfo = UnicodeAttribute() # m2m:contentInfo
|
||||||
contentSize = Attribute(int, accesstype=Attribute.RO)
|
contentSize = Attribute(int, accesstype=Attribute.RO)
|
||||||
ontologyRef = UnicodeAttribute(accesstype=Attribute.WO)
|
ontologyRef = UnicodeAttribute(accesstype=Attribute.WO, mandatory=False)
|
||||||
content = Attribute(bytes, accesstype=Attribute.WO, mandatory=True)
|
content = BytesAttribute(accesstype=Attribute.WO, mandatory=True)
|
||||||
|
|
||||||
__child_types__ = (
|
__child_types__ = (
|
||||||
Subscription,
|
Subscription,
|
||||||
@ -1004,8 +1004,8 @@ class ContentInstanceAnnc(AnnouncedSubordinateResourceC):
|
|||||||
stateTag = Attribute(int, accesstype=Attribute.RO)
|
stateTag = Attribute(int, accesstype=Attribute.RO)
|
||||||
contentInfo = UnicodeAttribute(EncodingTypeE) # m2m:contentInfo
|
contentInfo = UnicodeAttribute(EncodingTypeE) # m2m:contentInfo
|
||||||
contentSize = Attribute(int, accesstype=Attribute.WO)
|
contentSize = Attribute(int, accesstype=Attribute.WO)
|
||||||
ontologyRef = UnicodeAttribute(accesstype=Attribute.WO)
|
ontologyRef = UnicodeAttribute(accesstype=Attribute.WO, mandatory=False)
|
||||||
content = Attribute(bytes, accesstype=Attribute.WO, mandatory=True)
|
content = BytesAttribute(accesstype=Attribute.WO, mandatory=True)
|
||||||
|
|
||||||
|
|
||||||
################################################################################
|
################################################################################
|
||||||
@ -1107,7 +1107,7 @@ class AEAnnc(AnnouncedResourceC, SubscribableResource):
|
|||||||
|
|
||||||
typename = "AEAnnc"
|
typename = "AEAnnc"
|
||||||
|
|
||||||
appName = UnicodeAttribute(accesstype=Attribute.WO)
|
appName = UnicodeAttribute(accesstype=Attribute.WO, mandatory=False)
|
||||||
App_ID = UnicodeAttribute()
|
App_ID = UnicodeAttribute()
|
||||||
AE_ID = UnicodeAttribute()
|
AE_ID = UnicodeAttribute()
|
||||||
pointOfAccess = StringListAttribute()
|
pointOfAccess = StringListAttribute()
|
||||||
|
@ -33,6 +33,8 @@ def _default(x):
|
|||||||
return x.strftime("%Y%m%dT%H%M%S")
|
return x.strftime("%Y%m%dT%H%M%S")
|
||||||
elif isinstance(x, ContentInstance):
|
elif isinstance(x, ContentInstance):
|
||||||
return x.resourceID
|
return x.resourceID
|
||||||
|
elif isinstance(x, bytes):
|
||||||
|
return x.decode('utf-8')
|
||||||
else:
|
else:
|
||||||
try: # handle model classes
|
try: # handle model classes
|
||||||
return x.values
|
return x.values
|
||||||
|
@ -6,7 +6,7 @@ logger = get_logger(__name__)
|
|||||||
|
|
||||||
|
|
||||||
def decode_onem2m_content(content, content_type):
|
def decode_onem2m_content(content, content_type):
|
||||||
if content == "":
|
if not content:
|
||||||
content = None
|
content = None
|
||||||
if content_type and content is not None:
|
if content_type and content is not None:
|
||||||
serializer = get_onem2m_decoder(content_type)
|
serializer = get_onem2m_decoder(content_type)
|
||||||
|
@ -76,7 +76,7 @@ class Collection(Sequence, Mapping):
|
|||||||
|
|
||||||
|
|
||||||
class Member(LoggerMixin):
|
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)
|
super(Member, self).__init__(*args, **kw)
|
||||||
self.type = type
|
self.type = type
|
||||||
self.version = version
|
self.version = version
|
||||||
@ -109,7 +109,7 @@ class Attribute(Member):
|
|||||||
RO = "RO"
|
RO = "RO"
|
||||||
WO = "WO"
|
WO = "WO"
|
||||||
|
|
||||||
def __init__(self, type=unicode, default=None,
|
def __init__(self, type=str, default=None,
|
||||||
accesstype=None, mandatory=None,
|
accesstype=None, mandatory=None,
|
||||||
update_mandatory=None,
|
update_mandatory=None,
|
||||||
id_attribute=None, path_attribute=None,
|
id_attribute=None, path_attribute=None,
|
||||||
@ -157,24 +157,22 @@ class Attribute(Member):
|
|||||||
return self.default
|
return self.default
|
||||||
|
|
||||||
|
|
||||||
try:
|
class BytesAttribute(Attribute):
|
||||||
unicode
|
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 convert(self, value, instance):
|
||||||
def __init__(self, default=None, accesstype=None,
|
if isinstance(value, str):
|
||||||
mandatory=False, *args, **kw):
|
return bytes(value, "utf-8")
|
||||||
super(UnicodeAttribute, self).__init__(type=unicode,
|
return super(BytesAttribute, self).convert(value, instance)
|
||||||
default=default,
|
|
||||||
accesstype=accesstype,
|
|
||||||
mandatory=mandatory, *args,
|
|
||||||
**kw)
|
|
||||||
|
|
||||||
def convert(self, value, instance):
|
|
||||||
if isinstance(value, str):
|
UnicodeAttribute = Attribute
|
||||||
return value.decode("utf-8")
|
|
||||||
return super(UnicodeAttribute, self).convert(value, instance)
|
|
||||||
except NameError:
|
|
||||||
UnicodeAttribute = Attribute
|
|
||||||
|
|
||||||
|
|
||||||
class DatetimeAttribute(Attribute):
|
class DatetimeAttribute(Attribute):
|
||||||
@ -187,7 +185,7 @@ class DatetimeAttribute(Attribute):
|
|||||||
**kw)
|
**kw)
|
||||||
|
|
||||||
def convert(self, value, instance):
|
def convert(self, value, instance):
|
||||||
if isinstance(value, basestring):
|
if isinstance(value, str):
|
||||||
try:
|
try:
|
||||||
return parse_date(value)
|
return parse_date(value)
|
||||||
except ParseError as e:
|
except ParseError as e:
|
||||||
@ -196,7 +194,7 @@ class DatetimeAttribute(Attribute):
|
|||||||
|
|
||||||
|
|
||||||
class ListAttribute(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):
|
default=NOT_SET, *args, **kw):
|
||||||
super(ListAttribute, self).__init__(type=type,
|
super(ListAttribute, self).__init__(type=type,
|
||||||
default=default, *args, **kw)
|
default=default, *args, **kw)
|
||||||
@ -239,7 +237,7 @@ class ListAttribute(Attribute):
|
|||||||
|
|
||||||
|
|
||||||
class StringListAttribute(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):
|
default=NOT_SET, *args, **kw):
|
||||||
super(StringListAttribute, self).__init__(type=type, default=default,
|
super(StringListAttribute, self).__init__(type=type, default=default,
|
||||||
*args, **kw)
|
*args, **kw)
|
||||||
|
@ -499,14 +499,14 @@ class XAE(LoggerMixin):
|
|||||||
cnf = fmt + ':' + str(EncodingTypeE.plain.value)
|
cnf = fmt + ':' + str(EncodingTypeE.plain.value)
|
||||||
# raise CSENotImplemented("Only json as b64 is supported!")
|
# raise CSENotImplemented("Only json as b64 is supported!")
|
||||||
else:
|
else:
|
||||||
con = b64encode(json_dumps(content))
|
con = b64encode(json_dumps(content).encode('utf-8'))
|
||||||
cnf = fmt + ':' + str(EncodingTypeE.base64String.value)
|
cnf = fmt + ':' + str(EncodingTypeE.base64String.value)
|
||||||
elif fmt == 'text/plain':
|
elif fmt == 'text/plain':
|
||||||
if text:
|
if text:
|
||||||
con = content
|
con = content
|
||||||
cnf = fmt + ':' + str(EncodingTypeE.plain.value)
|
cnf = fmt + ':' + str(EncodingTypeE.plain.value)
|
||||||
else:
|
else:
|
||||||
con = b64encode(content)
|
con = b64encode(content.encode('utf-8'))
|
||||||
cnf = fmt + ':' + str(EncodingTypeE.base64String.value)
|
cnf = fmt + ':' + str(EncodingTypeE.base64String.value)
|
||||||
else:
|
else:
|
||||||
# TODO(rst): add handling of other formats or raise not implemented
|
# TODO(rst): add handling of other formats or raise not implemented
|
||||||
|
@ -1358,7 +1358,7 @@ class ContentInstanceController(OneM2MDefaultController):
|
|||||||
super(ContentInstanceController,
|
super(ContentInstanceController,
|
||||||
self)._set_mandatory_create_attributes(vals)
|
self)._set_mandatory_create_attributes(vals)
|
||||||
|
|
||||||
vals["contentSize"] = len(vals["content"].encode('utf-8'))
|
vals["contentSize"] = len(vals["content"])
|
||||||
if not vals.get("contentInfo"):
|
if not vals.get("contentInfo"):
|
||||||
vals["contentInfo"] = 'text/plain:0'
|
vals["contentInfo"] = 'text/plain:0'
|
||||||
|
|
||||||
@ -1421,7 +1421,7 @@ class SemanticDescriptorController(OneM2MDefaultController):
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
def _check_descriptor_data(descriptor_data):
|
def _check_descriptor_data(descriptor_data):
|
||||||
try:
|
try:
|
||||||
data = base64.b64decode(descriptor_data)
|
data = base64.b64decode(descriptor_data).decode('utf-8')
|
||||||
except binascii.Error:
|
except binascii.Error:
|
||||||
raise CSEContentsUnacceptable("The descriptor was not correctly base64 encoded.")
|
raise CSEContentsUnacceptable("The descriptor was not correctly base64 encoded.")
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user