Skip to content

Commit

Permalink
Merge pull request #324 from mtusnio/python_api
Browse files Browse the repository at this point in the history
Python API: Make the API Python 2 & 3 compatible
  • Loading branch information
boyvinall authored Jan 10, 2017
2 parents 3059298 + be71fa9 commit 4ef5c22
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
27 changes: 16 additions & 11 deletions api/python/ipc_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,26 @@
IPC Core Functionality
"""

from lxml import etree
try:
import lxml.etree as ElementTree
except ImportError:
import xml.etree.ElementTree as ElementTree

from future.utils import iteritems

g_debug = True
def debug(msg):
if g_debug:
print(msg)

def _serialize(header, msgType, sessionID, content):
e_root = etree.Element(header)
e_root = ElementTree.Element(header)
e_root.append(TElement("Type", str(msgType)))
if sessionID is not None:
e_root.append(TElement("SessionID", str(sessionID)))
if content is not None:
e_root.append(content)
return etree.tostring(e_root, pretty_print=True)
return ElementTree.tostring(e_root)

def is_sequence(arg):
"""Test for sequence, excluding strings: http://stackoverflow.com/a/1835259/143397"""
Expand Down Expand Up @@ -76,7 +81,7 @@ def add(self, path, value, label=None):
self._model.add(path, value, label)

def getElement(self):
e_content = etree.Element("Content")
e_content = ElementTree.Element("Content")
e_model = self._model.getElement()
# strip the outer container
for child in e_model.getchildren():
Expand Down Expand Up @@ -134,7 +139,7 @@ def __str__(self):

def parseXml(self, xml):
#debug("RECEIVED XML: %s\n" % (xml, ))
e_response = etree.fromstring(xml)
e_response = ElementTree.fromstring(xml)
if e_response.tag != "Response":
raise IpcError("Invalid response")
try:
Expand Down Expand Up @@ -202,7 +207,7 @@ def TElement(tag, text=None, tail=None, parent=None, attrib={}, **extra):
"""Helper function to create an etree.Element with text and tail.
Ensure that text is a string.
"""
element = etree.Element(tag, attrib, **extra)
element = ElementTree.Element(tag, attrib, **extra)
if text is not None:
assert isinstance(text, str), "text must be of type str"
element.text = text
Expand Down Expand Up @@ -306,7 +311,7 @@ def __str__(self):
s += "value %s, " % (self._value,)
if self._children:
s += "children "
for key, value in self._children.iteritems():
for key, value in iteritems(self._children):
s += str(key) + ":%x" % (id(value),) + " "
s += "]"
return s
Expand Down Expand Up @@ -377,7 +382,7 @@ def __init__(self):

def _createElement(self, treeNode, e_parent, tag, IDTag, valueTag=None):
# create a new element based on treeNode, and add to parentElement
e_element = etree.Element(tag)
e_element = ElementTree.Element(tag)

ID = treeNode.getID()
if ID is not None:
Expand All @@ -393,7 +398,7 @@ def _createElement(self, treeNode, e_parent, tag, IDTag, valueTag=None):

label = treeNode.getLabel()
if label is not None:
e_element.append(etree.Element(label))
e_element.append(ElementTree.Element(label))

debug("Adding %s to %s" % (e_element, e_parent))
e_parent.append(e_element)
Expand Down Expand Up @@ -474,8 +479,8 @@ def add(self, path, value=None, label=None):

def getElement(self):
"""Convert the data model into an Element."""
node = etree.Element("Model")
objects = etree.Element("Objects")
node = ElementTree.Element("Model")
objects = ElementTree.Element("Objects")
node.append(objects)
if self._root is not None:
processor = etreeProcessor()
Expand Down
6 changes: 5 additions & 1 deletion api/python/ipc_lwm2m_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@
LWM2M Client IPC Interface
"""

from lxml import etree
try:
import lxml.etree as ElementTree
except ImportError:
import xml.etree.ElementTree as ElementTree

from ipc_core import IpcRequest, IpcResponse, IpcNotification


Expand Down
12 changes: 8 additions & 4 deletions api/python/ipc_lwm2m_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@
LWM2M Server IPC Interface
"""

from lxml import etree
try:
import lxml.etree as ElementTree
except ImportError:
import xml.etree.ElementTree as ElementTree

import ipc_core

class IpcError(Exception):
Expand Down Expand Up @@ -96,10 +100,10 @@ def add(self, clientID, path, value, label):
model.add(path, value, label)

def getElement(self):
e_content = etree.Element("Content")
e_clients = etree.Element("Clients")
e_content = ElementTree.Element("Content")
e_clients = ElementTree.Element("Clients")
for client_id in self._clients:
e_client = etree.Element("Client")
e_client = ElementTree.Element("Client")
e_client.append(ipc_core.TElement("ID", str(client_id)))
e_model = self._clients[client_id].model.getElement()
# strip the outer container
Expand Down
1 change: 1 addition & 0 deletions ci/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mercurial==3.7
nose==1.3.7
lxml==3.6.0
future==0.16.0

0 comments on commit 4ef5c22

Please sign in to comment.