Ack.
-----Original Message----- From: Thien Minh Huynh <thien.m.hu...@dektech.com.au> Sent: Thursday, January 14, 2021 5:07 PM To: Thang Duc Nguyen <thang.d.ngu...@dektech.com.au>; Thanh Nguyen <thanh.ngu...@dektech.com.au> Cc: opensaf-devel@lists.sourceforge.net; Thien Minh Huynh <thien.m.hu...@dektech.com.au> Subject: [PATCH 1/1] pyosaf: update pyosaf support python3 [#3247] - check data type before implicit type conversion - add more encode/decode byte-string - update iterable dict items for python 2 and 3 --- python/pyosaf/saClm.py | 34 +++++++++++++------- python/pyosaf/utils/clm/__init__.py | 4 +-- python/pyosaf/utils/immoi/agent.py | 6 ++-- python/pyosaf/utils/immoi/implementer.py | 14 ++++---- python/pyosaf/utils/immom/agent.py | 6 ++-- python/pyosaf/utils/immom/iterator.py | 8 ++--- python/pyosaf/utils/immom/object.py | 10 +++--- python/pyosaf/utils/ntf/subscriber.py | 7 ++-- python/samples/imm-listener | 2 +- python/samples/imm-listener-inheritance-impl | 2 +- python/samples/immbase.py | 4 +-- python/samples/immlist | 2 +- python/samples/scale_opensaf | 2 +- 13 files changed, 58 insertions(+), 43 deletions(-) diff --git a/python/pyosaf/saClm.py b/python/pyosaf/saClm.py index f140d4484..a044360a0 100644 --- a/python/pyosaf/saClm.py +++ b/python/pyosaf/saClm.py @@ -19,7 +19,7 @@ from ctypes import POINTER, CDLL, Structure, CFUNCTYPE from pyosaf.saAis import SaUint64T, SaUint32T, Const, SaEnumT, Enumeration, \ SaBoolT, SaTimeT, SaNameT, SaAisErrorT, SaInvocationT, BYREF, \ SaVersionT, SaSelectionObjectT, SaInt8T, SaDispatchFlagsT, SaUint8T, \ - SaUint16T + SaUint16T, PY3 from pyosaf import saNtf clmdll = CDLL('libSaClm.so.0') @@ -81,17 +81,27 @@ eSaClmAdditionalInfoIdT_4 = Enumeration(( )) class SaClmNodeAddressT(Structure): - """Contain string representation of communication address associated - with cluster node. - """ - _fields_ = [('family', SaClmNodeAddressFamilyT), - ('length', SaUint16T), - ('value', SaInt8T*saClm.SA_CLM_MAX_ADDRESS_LENGTH)] - def __init__(self, family=0, address=''): - """Construct instance of 'family' with contents of 'name'. - """ - super(SaClmNodeAddressT, self).__init__(family, - len(address), address) + """Contain string representation of communication address associated + with cluster node. + """ + _fields_ = [('family', SaClmNodeAddressFamilyT), + ('length', SaUint16T), + ('value', SaInt8T * saClm.SA_CLM_MAX_ADDRESS_LENGTH)] + + def __init__(self, family=0, address=''): + """Construct instance of 'family' with contents of 'name'. + """ + if PY3: + address = address.encode('utf-8') + super(SaClmNodeAddressT, self).__init__(family, + len(address), address) + + def __str__(self): + """Returns the content of SaClmNodeAddressT + """ + if PY3: + return self.value.decode('utf-8') + return self.value SaClmChangeStepT = SaEnumT eSaClmChangeStepT = Enumeration(( diff --git a/python/pyosaf/utils/clm/__init__.py b/python/pyosaf/utils/clm/__init__.py index 5461d4a3c..496f4be2a 100644 --- a/python/pyosaf/utils/clm/__init__.py +++ b/python/pyosaf/utils/clm/__init__.py @@ -67,9 +67,9 @@ class ClusterNode(object): cluster membership """ self.node_id = node_id - self.node_address_value = node_address.value + self.node_address_value = str(node_address) self.node_address_family = node_address.family - self.node_name = node_name.value + self.node_name = str(node_name) self.execution_environment = execution_environment self.member = member self.boot_timestamp = boot_timestamp diff --git a/python/pyosaf/utils/immoi/agent.py b/python/pyosaf/utils/immoi/agent.py index e0697b1ef..19c453fa2 100644 --- a/python/pyosaf/utils/immoi/agent.py +++ b/python/pyosaf/utils/immoi/agent.py @@ -26,7 +26,7 @@ Supported functions: """ from __future__ import print_function from copy import deepcopy -from ctypes import c_char_p, c_void_p, cast, pointer +from ctypes import c_void_p, cast, pointer from pyosaf.saAis import SaStringT, SaVersionT, SaNameT, SaSelectionObjectT, \ eSaDispatchFlagsT, eSaAisErrorT @@ -386,9 +386,9 @@ class OiAgent(object): list: List of object names """ # Marshall the search parameter - c_class_name = c_char_p(class_name) + c_class_name = SaStringT(class_name) c_search_param = SaImmSearchParametersT_2() - c_search_param.searchOneAttr.attrName = "SaImmAttrClassName" + c_search_param.searchOneAttr.attrName = + SaStringT("SaImmAttrClassName") c_search_param.searchOneAttr.attrValueType = \ eSaImmValueTypeT.SA_IMM_ATTR_SASTRINGT c_search_param.searchOneAttr.attrValue = \ diff --git a/python/pyosaf/utils/immoi/implementer.py b/python/pyosaf/utils/immoi/implementer.py index f7f7a83ad..61a7fe6ce 100755 --- a/python/pyosaf/utils/immoi/implementer.py +++ b/python/pyosaf/utils/immoi/implementer.py @@ -25,7 +25,7 @@ from pyosaf.saAis import eSaAisErrorT, unmarshalNullArray, \ unmarshalSaStringTArray from pyosaf import saImm, saImmOi from pyosaf.saImm import eSaImmValueTypeT, eSaImmAttrModificationTypeT, \ - eSaImmClassCategoryT, SaImmClassNameT, unmarshalSaImmValue + eSaImmClassCategoryT, SaImmClassNameT, unmarshalSaImmValue, + SaStringT, PY3 from pyosaf.saImmOi import SaImmOiImplementerNameT from pyosaf.utils import SafException, decorate, bad_handle_retry, log_err from pyosaf.utils.immoi import OiAgent @@ -285,7 +285,7 @@ class Implementer(OiAgent): if not c_array: return [] ctype = c_array[0].__class__ - if ctype is str: + if ctype is str or (PY3 and ctype is SaStringT): return unmarshalSaStringTArray(c_array) val_list = [] i = 0 @@ -489,7 +489,7 @@ class Implementer(OiAgent): implementer_objection = None for attr in unmarshalNullArray(c_attr_modification): - attr_name = attr.modAttr.attrName + attr_name = str(attr.modAttr.attrName) attr_type = attr.modAttr.attrValueType mod_type = attr.modType attr_values = self.unmarshal_len_array( @@ -541,7 +541,7 @@ class Implementer(OiAgent): attributes = {} for attr in unmarshalNullArray(c_attr_values): - attr_name = attr.attrName + attr_name = str(attr.attrName) attr_type = attr.attrValueType nr_values = attr.attrValuesNumber @@ -556,8 +556,8 @@ class Implementer(OiAgent): _, description = self.imm_om.get_class_description(class_name) for attribute in description: - if attribute.attrName not in attributes: - attributes[attribute.attrName] = None + if str(attribute.attrName) not in attributes: + attributes[str(attribute.attrName)] = None # Create a new CCB in the cache if needed if ccb_id not in list(self.ccbs.keys()): @@ -566,7 +566,7 @@ class Implementer(OiAgent): # Cache the create operation self.ccbs[ccb_id].append({'type': 'CREATE', 'parent': parent, - 'className': class_name, + 'className': str(class_name), 'attributes': attributes}) # Tell the implementer about the operation diff --git a/python/pyosaf/utils/immom/agent.py b/python/pyosaf/utils/immom/agent.py index 0413f1bd1..b5a710628 100644 --- a/python/pyosaf/utils/immom/agent.py +++ b/python/pyosaf/utils/immom/agent.py @@ -22,7 +22,7 @@ from ctypes import pointer from pyosaf import saImmOm, saImm from pyosaf.saAis import saAis, SaVersionT, SaNameT, SaAisErrorT, \ - eSaAisErrorT, eSaBoolT, unmarshalNullArray + eSaAisErrorT, eSaBoolT, unmarshalNullArray, SaStringT from pyosaf.saImm import eSaImmScopeT, SaImmClassNameT, SaImmAttrNameT from pyosaf.utils import decorate, initialize_decorate, log_err @@ -221,8 +221,10 @@ class ImmOmAgent(OmAgentManager): class_attrs = [] attr_defs = pointer(pointer(saImm.SaImmAttrDefinitionT_2())) category = saImm.SaImmClassCategoryT() + c_class_name = class_name if isinstance( + class_name, SaStringT) else SaImmClassNameT(class_name) rc = saImmOmClassDescriptionGet_2(self.handle, - SaImmClassNameT(class_name), + c_class_name, category, attr_defs) if rc != eSaAisErrorT.SA_AIS_OK: diff --git a/python/pyosaf/utils/immom/iterator.py b/python/pyosaf/utils/immom/iterator.py index 91c5751c0..7b0e0a8a4 100644 --- a/python/pyosaf/utils/immom/iterator.py +++ b/python/pyosaf/utils/immom/iterator.py @@ -18,9 +18,9 @@ """ IMM Search iterators """ from __future__ import print_function from collections import Iterator -from ctypes import pointer, c_char_p, cast, c_void_p +from ctypes import pointer, cast, c_void_p -from pyosaf.saAis import eSaAisErrorT, SaNameT, unmarshalNullArray +from pyosaf.saAis import eSaAisErrorT, SaNameT, unmarshalNullArray, +SaStringT from pyosaf.saImm import saImm, eSaImmScopeT, eSaImmValueTypeT, \ unmarshalSaImmValue, SaImmAttrNameT, SaImmSearchParametersT_2, \ SaImmAttrValuesT_2 @@ -138,9 +138,9 @@ class SearchIterator(agent.OmAgentManager, Iterator): class InstanceIterator(SearchIterator): """ Iterator over instances of a class """ def __init__(self, class_name, root_name=None): - name = c_char_p(class_name) + name = SaStringT(class_name) search_param = SaImmSearchParametersT_2() - search_param.searchOneAttr.attrName = "SaImmAttrClassName" + search_param.searchOneAttr.attrName = + SaStringT("SaImmAttrClassName") search_param.searchOneAttr.attrValueType = \ eSaImmValueTypeT.SA_IMM_ATTR_SASTRINGT search_param.searchOneAttr.attrValue = cast(pointer(name), c_void_p) diff --git a/python/pyosaf/utils/immom/object.py b/python/pyosaf/utils/immom/object.py index 6e80abfae..f668c3e0d 100644 --- a/python/pyosaf/utils/immom/object.py +++ b/python/pyosaf/utils/immom/object.py @@ -55,7 +55,7 @@ class ImmObject(object): rc, class_desc = _imm_om.get_class_description(class_name) if rc != eSaAisErrorT.SA_AIS_OK: raise SafException(rc) - self.class_desc[class_name] = class_desc + self.class_desc[str(class_name)] = class_desc elif attributes is not None: assert class_name is None self.__dict__["attrs"] = attributes @@ -65,7 +65,7 @@ class ImmObject(object): rc, class_desc = _imm_om.get_class_description(class_name) if rc != eSaAisErrorT.SA_AIS_OK: raise SafException(rc) - self.class_desc[class_name] = class_desc + self.class_desc[str(class_name)] = class_desc else: raise ValueError("Class and attributes are None") @@ -81,7 +81,7 @@ class ImmObject(object): Returns: SaImmAttrNameT: Attribute value type """ - for attr_def in self.class_desc[self.class_name]: + for attr_def in self.class_desc[str(self.class_name)]: if str(attr_def.attrName) == attr_name: return attr_def.attrValueType @@ -94,8 +94,8 @@ class ImmObject(object): Returns: SaImmAttrFlagsT: Attribute flag """ - for attr_def in self.class_desc[self.class_name]: - if attr_def.attrName == attr_name: + for attr_def in self.class_desc[str(self.class_name)]: + if str(attr_def.attrName) == attr_name: return attr_def.attrFlags & saImm.SA_IMM_ATTR_MULTI_VALUE def __getattr__(self, name): diff --git a/python/pyosaf/utils/ntf/subscriber.py b/python/pyosaf/utils/ntf/subscriber.py index 05596dc08..e41efa068 100644 --- a/python/pyosaf/utils/ntf/subscriber.py +++ b/python/pyosaf/utils/ntf/subscriber.py @@ -23,7 +23,7 @@ from copy import deepcopy from pyosaf import saNtf from pyosaf.saAis import BYREF, eSaAisErrorT, SaVoidPtr, SaNameT, SaUint16T, \ - SaStringT + SaStringT, PY3 from pyosaf.utils import bad_handle_retry, log_err, log_warn from pyosaf.utils.ntf import agent as ntf @@ -457,6 +457,8 @@ class NtfConsumer(ntf.NtfAgent): rc = ntf.saNtfPtrValGet(ntf_handle, value, data_ptr, data_size) if rc != eSaAisErrorT.SA_AIS_OK: log_warn("saNtfPtrValGet FAILED - %s" % (eSaAisErrorT.whatis(rc))) + if PY3: + return str(ctypes.cast(data_ptr, SaStringT)) return ctypes.cast(data_ptr, SaStringT).value def _get_ntf_value(self, ntf_handle, value, value_type): @@ -508,7 +510,8 @@ class NtfConsumer(ntf.NtfAgent): ctypes.create_string_buffer(ntf_header.lengthAdditionalText) ctypes.memmove(additional_text_str, ntf_header.additionalText, ntf_header.lengthAdditionalText) - ntf_info.additional_text = additional_text_str.value + ntf_info.additional_text = additional_text_str.value.decode( + 'utf-8') if PY3 else additional_text_str.value for i in range(ntf_header.numAdditionalInfo): c_add_info = ntf_header.additionalInfo[i] diff --git a/python/samples/imm-listener b/python/samples/imm-listener index db8bbea34..138e8da70 100755 --- a/python/samples/imm-listener +++ b/python/samples/imm-listener @@ -39,7 +39,7 @@ def print_object(obj): print("\t%s" % obj.dn) # Print attributes - for name, type_value_pair in obj.attrs.iteritems(): + for name, type_value_pair in obj.attrs.items(): value = type_value_pair[1] if len(value) == 1: diff --git a/python/samples/imm-listener-inheritance-impl b/python/samples/imm-listener-inheritance-impl index 5a0df8df6..bf32cc1c0 100755 --- a/python/samples/imm-listener-inheritance-impl +++ b/python/samples/imm-listener-inheritance-impl @@ -63,7 +63,7 @@ class ImmListener(Applier): print("\t%s" % obj.dn) # Print attributes - for name, type_value_pair in obj.attrs.iteritems(): + for name, type_value_pair in obj.attrs.items(): value = type_value_pair[1] if len(value) == 1: diff --git a/python/samples/immbase.py b/python/samples/immbase.py index 2f6afba0c..21500f4cf 100644 --- a/python/samples/immbase.py +++ b/python/samples/immbase.py @@ -77,7 +77,7 @@ class SafObject(object): @staticmethod def resolveStates(attribs): - for (attr, vals) in attribs.iteritems(): + for (attr, vals) in attribs.items(): enum = SafObject.resolver.get(attr) if enum: vals[1] = [enum.whatis(val) for val in vals[1]] @@ -92,7 +92,7 @@ class SafObject(object): attrList = unmarshalNullArray(attribs) for attr in attrList: attrRange = range(attr.attrValuesNumber) - self.attribs[attr.attrName] = [ + self.attribs[str(attr.attrName)] = [ saImm.eSaImmValueTypeT.whatis(attr.attrValueType), [saImm.unmarshalSaImmValue( attr.attrValues[val], diff --git a/python/samples/immlist b/python/samples/immlist index 1034421c9..5fac6eaea 100755 --- a/python/samples/immlist +++ b/python/samples/immlist @@ -36,7 +36,7 @@ def immlist(args): _, obj = accessor.get(dn, attr_name_list=args.attributes) if obj: attributes = {} - for name, value_pair in obj.attrs.iteritems(): + for name, value_pair in obj.attrs.items(): type_str = saImm.eSaImmValueTypeT.whatis(value_pair[0]) value = value_pair[1] attributes[name] = [type_str, value] diff --git a/python/samples/scale_opensaf b/python/samples/scale_opensaf index 3de863210..5a191802d 100755 --- a/python/samples/scale_opensaf +++ b/python/samples/scale_opensaf @@ -47,7 +47,7 @@ def print_object(immobj, new_dn=None): print(new_dn) else: print(immobj.dn) - for key, value in immobj.attrs.iteritems(): + for key, value in immobj.attrs.items(): val = '' if value[1]: val = value[1] -- 2.25.1 _______________________________________________ Opensaf-devel mailing list Opensaf-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/opensaf-devel