Ejemplo n.º 1
0
 def to_tree(self, tagname, obj, namespace=None):
     tagname = namespaced(self, tagname, namespace)
     container = Element(tagname)
     if self.count:
         container.set('count', str(len(obj)))
     for v in obj:
         container.append(v.to_tree())
     return container
Ejemplo n.º 2
0
    def to_tree(self):
        tree = Element("Relationships", xmlns=PKG_REL_NS)
        for idx, rel in enumerate(self.Relationship, 1):
            if not rel.Id:
                rel.Id = "rId{0}".format(idx)
            tree.append(rel.to_tree())

        return tree
Ejemplo n.º 3
0
 def to_tree(self, tagname=None, idx=None):
     parent = Element("fill")
     el = Element(self.tagname)
     if self.patternType is not None:
         el.set('patternType', self.patternType)
     for c in self.__elements__:
         value = getattr(self, c)
         if value != Color():
             el.append(value.to_tree(c))
     parent.append(el)
     return parent
Ejemplo n.º 4
0
    def to_tree(self, tagname=None, idx=None, namespace=None):

        if tagname is None:
            tagname = self.tagname

        # keywords have to be masked
        if tagname.startswith("_"):
            tagname = tagname[1:]

        tagname = namespaced(self, tagname, namespace)
        namespace = getattr(self, "namespace", namespace)

        attrs = dict(self)
        for key, ns in self.__namespaced__:
            if key in attrs:
                attrs[ns] = attrs[key]
                del attrs[key]

        el = Element(tagname, attrs)
        if "attr_text" in self.__attrs__:
            el.text = safe_string(getattr(self, "attr_text"))

        for child_tag in self.__elements__:
            desc = getattr(self.__class__, child_tag, None)
            obj = getattr(self, child_tag)

            if isinstance(obj, seq_types):
                if isinstance(desc, NestedSequence):
                    # wrap sequence in container
                    if not obj:
                        continue
                    nodes = [desc.to_tree(child_tag, obj, namespace)]
                elif isinstance(desc, Sequence):
                    # sequence
                    desc.idx_base = self.idx_base
                    nodes = (desc.to_tree(child_tag, obj, namespace))
                else: # property
                    nodes = (v.to_tree(child_tag, namespace) for v in obj)
                for node in nodes:
                    el.append(node)
            else:
                if child_tag in self.__nested__:
                    node = desc.to_tree(child_tag, obj, namespace)
                elif obj is None:
                    continue
                else:
                    node = obj.to_tree(child_tag)
                if node is not None:
                    el.append(node)
        return el
Ejemplo n.º 5
0
    def to_tree(self):
        def sorter(value):
            value.reindex()
            return value.min

        el = Element('cols')
        obj = None
        outlines = set()

        for col in sorted(self.values(), key=sorter):
            obj = col.to_tree()
            outlines.add(col.outlineLevel)
            if obj is not None:
                el.append(obj)

        if outlines:
            self.max_outline = max(outlines)

        if obj is not None:
            return el
Ejemplo n.º 6
0
    def test_to_tree(self, Dummy):

        dummy = Dummy([1, '2', 3])

        root = Element("root")
        for node in Dummy.value.to_tree(
                "el",
                dummy.value,
        ):
            root.append(node)

        xml = tostring(root)
        expected = """
        <root>
          <el>1</el>
          <el>2</el>
          <el>3</el>
        </root>
        """
        diff = compare_xml(xml, expected)
        assert diff is None, diff
Ejemplo n.º 7
0
    def test_to_tree(self, Stooge):

        dummy = Stooge()
        dummy._stooges = [Larry(1), Curly(2), Larry(3), Mo(4)]

        root = Element("root")
        for node in Stooge._stooges.to_tree("el", dummy._stooges):
            root.append(node)

        tree = dummy.to_tree("root")
        xml = tostring(root)
        expected = """
        <root>
            <l value="1"></l>
            <c hair="2"></c>
            <l value="3"></l>
            <m cap="4"></m>
        </root>
        """
        diff = compare_xml(xml, expected)
        assert diff is None, diff
Ejemplo n.º 8
0
    def test_to_tree(self, Sequence):
        class Dummy:

            vals = Sequence(expected_type=SomeType, name="vals")

        dummy = Dummy()
        dummy.vals = [SomeType(1), SomeType(2), SomeType(3)]

        root = Element("root")
        for node in Dummy.vals.to_tree("el", dummy.vals):
            root.append(node)

        xml = tostring(root)
        expected = """
        <root>
          <el value="1"></el>
          <el value="2"></el>
          <el value="3"></el>
        </root>
        """
        diff = compare_xml(xml, expected)
        assert diff is None, diff
Ejemplo n.º 9
0
 def to_tree(self, tagname=None, namespace=None, idx=None):
     parent = Element("fill")
     el = super(GradientFill, self).to_tree()
     parent.append(el)
     return parent