Commits

Ali Afshar committed 6bb5975

proper child handling

Comments (0)

Files changed (3)

gtkarchitect/model/entity.py

         entity.append(prop_element)
     prop_element.text = value
 
+def add_entity_child(entity, child_entity, internal=None, no_child=False):
+    if no_child:
+        child = entity
+    else:
+        child = Element('child')
+        if internal is not None:
+            child.set('internal-child', internal)
+        entity.append(child)
+    child.append(child_entity)
+
 def get_entity_type(entity):
     return entity.attrib.get('class')
 

gtkarchitect/model/project.py

 
 from lxml.etree import Element
 
+from .entity import add_entity_child
+
 import gobject
 
 from kiwi.utils import gsignal
 
     def add_entity(self, entity, parent=None):
         if parent is None:
-            parent = self.tree
-        parent.append(entity)
+            parent_entity = self.tree
+            no_child = True
+        else:
+            parent_entity = parent
+            no_child = False
+        add_entity_child(parent_entity, entity, no_child=no_child)
         self.emit('entity-added', entity, parent)
 
     def create_entity(self, definition, parent=None):

tests/model/test_entity.py

         get_entity_type,
         get_entity_prop,
         set_entity_prop,
+        add_entity_child,
         _fix_indent,
         tostring,
         )
     print result
     assert result == output
 
+def test_add_child():
+    e = Element('test')
+    c = Element('testchild')
+    add_entity_child(e, c)
+    print tostring(e)
+    assert tostring(e) == '<test><child><testchild/></child></test>'
+
+def test_add_internal_child():
+    e = Element('test')
+    c = Element('testchild')
+    add_entity_child(e, c, internal='internal_name')
+    print tostring(e)
+    assert tostring(e) == '<test><child internal-child="internal_name"><testchild/></child></test>'
+
+def test_add_nochild_child():
+    e = Element('test')
+    c = Element('testchild')
+    add_entity_child(e, c, no_child=True)
+    print tostring(e)
+    assert tostring(e) == '<test><testchild/></test>'
+