pyangbind icon indicating copy to clipboard operation
pyangbind copied to clipboard

pybindIETFXMLEncoder fails to serialize the object into XML

Open muffizone opened this issue 7 years ago • 3 comments

Hi,

I was trying to convert the python object into XML using the new XMLEncoder but it is failing with an attribute error.

In [60]: oclr = openconfig_local_routing()
    ...: rt = oclr.local_routes.static_routes.static.add("192.0.2.1/32")
    ...: rt.config.set_tag = 42
    ...:
    ...:

In [61]: from pyangbind.lib.serialise import pybindIETFXMLEncoder
    ...: print(pybindIETFXMLEncoder.serialise(oclr))
    ...:
    ...:
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-61-1e1f0873410f> in <module>()
      1 from pyangbind.lib.serialise import pybindIETFXMLEncoder
----> 2 print(pybindIETFXMLEncoder.serialise(oclr))

/usr/local/lib/python3.4/dist-packages/pyangbind/lib/serialise.py in serialise(cls, obj, filter, pretty_print)
    378     def serialise(cls, obj, filter=True, pretty_print=True):
    379         """return the complete XML document, as pretty-printed string"""
--> 380         doc = cls.encode(obj, filter=filter)
    381         return etree.tostring(doc, pretty_print=pretty_print).decode("utf8")
    382

/usr/local/lib/python3.4/dist-packages/pyangbind/lib/serialise.py in encode(cls, obj, filter)
    373         tree = ietf_tree_xml_func(obj, flt=filter)
    374         preprocessed = XmlYangDataSerialiser().preprocess_element(tree)
--> 375         return cls.generate_xml_tree(obj._yang_name, obj._yang_namespace, preprocessed)
    376
    377     @classmethod

AttributeError: 'openconfig_local_routing' object has no attribute '_yang_namespace'

In [62]:

I was checking the available attributes for oclr object and it does not seem to have any attribute by the name of _yang_namespace. I am guessing this attribute is used for creating the xml namespace for the root element.

muffizone avatar Sep 18 '18 16:09 muffizone

I had the same error, after installing pyangbind (pip install pyangbind) and I check the installed files on mycomputer I find out that we do not have the same version as the one existing on github. to be able to use it what I did was delete pyang (pip uninstall pyangbind), download the folder from github, put it in the same project and it worked

Muna24 avatar Jan 10 '19 10:01 Muna24

I think this may just be a typo. After some debugging, I renamed _yang_namespace to _namespace, and now it works for me. There is no _yang_namespace in the object YANGBaseClass in yangtypes.py, but there is an _namespace. Other variables like _yang_type, are there in the object.

igrush avatar Feb 08 '19 14:02 igrush

I was facing the same error and found out that this error happened because of a version mismatch between the pyangbind used to serialize in XML and the version used to autogenerate the classes.

I first generated the classes from the YANG model using the pyangbind version obtained through pip install. Then I found out that that version does not have the XML serializer, so I updated pyangbind to the master version and tried to serialize an object created with those old autogenerated classes, getting the `object has no attribute '_yang_namespace'' error. After I re-autogenerated the classes with the new pyangbind version, the error disappeared.

AmedeoSapio avatar Jan 24 '20 23:01 AmedeoSapio