summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFlorian Bruhin <me@the-compiler.org>2012-06-22 08:30:37 +0200
committerFlorian Bruhin <me@the-compiler.org>2012-06-22 08:30:37 +0200
commitc216e839024736fa91f7b7f9d93d330bdb7bface (patch)
tree8abe56a8b47c8811a0db5701ce46edf85144737c
parent1946197fb0abb9334733f2e27a57cc4abcd14d8c (diff)
downloadxdd2esi-c216e839024736fa91f7b7f9d93d330bdb7bface.tar.gz
xdd2esi-c216e839024736fa91f7b7f9d93d330bdb7bface.zip
Make _append_elements() a function, not a method
-rw-r--r--esilib.py84
1 files changed, 39 insertions, 45 deletions
diff --git a/esilib.py b/esilib.py
index 51f103d..62c34bc 100644
--- a/esilib.py
+++ b/esilib.py
@@ -141,7 +141,7 @@ class ESIFile:
if esitype not in self.bitsizes: # Datatype does not already exist
properties = [("Name", esitype), ("BitSize", esisize)]
_append_comment(self.dt_simple, esitype)
- self._append_elements(self.dt_simple, "DataType", properties)
+ _append_elements(self.dt_simple, "DataType", properties)
self.bitsizes[esitype] = esisize
return (esitype, esisize)
@@ -154,8 +154,7 @@ class ESIFile:
# Bitsize will be set later
properties = [ ("Name", esiname), ("BitSize", 'x') ]
_append_comment(self.dt_complex, esiname)
- record = self._append_elements(self.dt_complex, "DataType",
- properties)
+ record = _append_elements(self.dt_complex, "DataType", properties)
return record
def add_array_datatype(self, esiname, basetype, elements, lbound=0,
@@ -183,8 +182,8 @@ class ESIFile:
]
arrayinfo = [ ("LBound", lbound), ("Elements", elements) ]
_append_comment(target, esiname)
- array = self._append_elements(target, "DataType", properties)
- self._append_elements(array, "ArrayInfo", arrayinfo)
+ array = _append_elements(target, "DataType", properties)
+ _append_elements(array, "ArrayInfo", arrayinfo)
self.bitsizes[esiname] = bitsize
retval = array
else:
@@ -218,8 +217,8 @@ class ESIFile:
obj = self._add_to_xml(self.objtree, "Object", properties, ignored)
if canobj.infocomment is not None:
_append_comment(obj, canobj.infocomment)
- self._append_elements(obj, "Info", info, force=True)
- self._append_elements(obj, "Flags", flags)
+ _append_elements(obj, "Info", info, force=True)
+ _append_elements(obj, "Flags", flags)
return obj
def add_subobject(self, canobj, parent=None):
@@ -243,7 +242,7 @@ class ESIFile:
("objFlags setting (see CiA-306)", canobj.objflags)
]
obj = self._add_to_xml(parent, "SubItem", properties, ignored)
- self._append_elements(obj, "Flags", flags)
+ _append_elements(obj, "Flags", flags)
self._set_bitoffset(parent, esiname, bitoffset, esisize)
return parent
@@ -258,8 +257,8 @@ class ESIFile:
name = [('Name', subobj.name)]
info = self._get_info_tree(subobj)
if info is not None:
- subitem = self._append_elements(infotree, "SubItem", name)
- self._append_elements(subitem, "Info", info)
+ subitem = _append_elements(infotree, "SubItem", name)
+ _append_elements(subitem, "Info", info)
def _set_bitoffset(self, parent, esiname, bitoffset, esisize):
""" Sets the bitoffset of an esiname """
@@ -293,7 +292,7 @@ class ESIFile:
def _add_to_xml(self, xmlparent, xmlnodename, properties, ignored):
""" Adds a new node with it's properties to the XML """
- obj = self._append_elements(xmlparent, xmlnodename, properties)
+ obj = _append_elements(xmlparent, xmlnodename, properties)
for elem in ignored:
if elem[1] is not None:
_append_comment(obj, "ignored: {} (value: {})".format(elem[0],
@@ -320,40 +319,6 @@ class ESIFile:
]
return info
- def _append_elements(self, root, subnode, attrs, force=False):
- """
- Appends new datatype or object to the tree.
-
- root: dttree, objtree, ...
- subnode: "DataType", "Object", "SubItem", ...
- attrs: [ ("Name", "UDINT"), ("BitSize", 32), ... ]
- force: If true, add subnode even if attrs are None
-
- An example: If the objtree is empty at the moment, and you do:
- _append_elements(objtree, "Object", [("Name", "foobar")]
- it gets:
- <Object><Name>foobar</Name></Object>
- """
- # If all values in properties are None,
- # we don't create the subtree-tag
- if ((attrs is not None and any(attr[1] is not None for attr in attrs))
- or force):
- subtree = etree.SubElement(root, subnode)
- for attr in attrs:
- # If the value in the attr is None, the tag is omitted
- if attr[1] is not None:
- elem = etree.SubElement(subtree, str(attr[0]))
- elem.text = str(attr[1])
- retval = subtree
- else:
- retval = None
- return retval
-
- def _append_comment(self, root, comment):
- """ Appends a comment to the tree """
- root.append(etree.Comment(comment))
-
-
def type_to_esitype(datatype):
""" Takes a py-datatype and returns the corresponding esi-type """
esitype = ""
@@ -444,3 +409,32 @@ def _esitype_oct(datatype):
def _append_comment(root, comment):
""" Appends a comment to the tree """
root.append(etree.Comment(comment))
+
+def _append_elements(root, subnode, attrs, force=False):
+ """
+ Appends new datatype or object to the tree.
+
+ root: dttree, objtree, ...
+ subnode: "DataType", "Object", "SubItem", ...
+ attrs: [ ("Name", "UDINT"), ("BitSize", 32), ... ]
+ force: If true, add subnode even if attrs are None
+
+ An example: If the objtree is empty at the moment, and you do:
+ _append_elements(objtree, "Object", [("Name", "foobar")]
+ it gets:
+ <Object><Name>foobar</Name></Object>
+ """
+ # If all values in properties are None,
+ # we don't create the subtree-tag
+ if ((attrs is not None and any(attr[1] is not None for attr in attrs))
+ or force):
+ subtree = etree.SubElement(root, subnode)
+ for attr in attrs:
+ # If the value in the attr is None, the tag is omitted
+ if attr[1] is not None:
+ elem = etree.SubElement(subtree, str(attr[0]))
+ elem.text = str(attr[1])
+ retval = subtree
+ else:
+ retval = None
+ return retval