Source

gtkarchitect-main / gtkarchitect / model / entity.py



from lxml.etree import ElementTree, Element, SubElement, tostring
from StringIO import StringIO


def _get_entity_prop_element(entity, property):
    for element in entity.iter('property'):
        if element.get('name') == property:
            return element

def get_entity_prop(entity, property):
    prop_element = _get_entity_prop_element(entity, property)
    if prop_element is not None:
        return prop_element.text

def set_entity_prop(entity, property, value):
    prop_element = _get_entity_prop_element(entity, property)
    if prop_element is None:
        prop_element = Element('property', name=property)
        #XXX: this doesn't take ordering conventions into account
        entity.append(prop_element)
    prop_element.text = value

def get_entity_type(entity):
    return entity.attrib.get('class')

def _fix_indent(elem, level=0, indent=4):
    i = '\n' + ' '* level*indent
    if len(elem):
        elem.text = i+ ' '*indent
        for e in elem:
            _fix_indent(e, level=level+1, indent=indent)
        e.tail = i
    else:
        if level:
            elem.tail = i