Exemple #1
0
    def setvaluenode(self,
                     node,
                     name,
                     value=None,
                     attribs=None,
                     hsdblock=False):
        """Creates a child with a node with a given name as only child.

        Parameters
        ----------
        node : Element
            Parent node.
        name : str
            Name of the child to create.
        value : str, optional
            Name of the node to create as child of the child node. If not
            specified, no subchild node is created.
        attribs : dict, optional
            Attributes of the child created.
        hsdblock : bool, optional
            Whether the given value should be added in hsd block notation
            (enclosed in curly braces) instead of an assignment.
        """
        child = Element(name, attribs or {})
        self.markprocessed(child)
        if value is not None:
            valuenode = Element(value)
            self.markprocessed(valuenode)
            child.append(valuenode)
        else:
            valuenode = None
        if not hsdblock:
            child.sethsd(hsd.HSDATTR_EQUAL, True)
        node.append(child)
        return child, valuenode
Exemple #2
0
    def __init__(self, root=None, query=None):
        """Initializes an SkgenPaths instance.

        Args:
            root: Root of the hsd tree storing the paths (default=None).
            query: Query object to use to query the path tree (default=None).
        """
        if root is None:
            self._root = Element("hsd")
        else:
            self._root = root
        if query is None:
            self._query = HSDQuery(checkuniqueness=True)
        else:
            self._query = query
        self._onecenter_nodes = {}
        self._twocenter_nodes = {}
        self._store_nodes()
Exemple #3
0
    def setchild(self, node, name, attribs=None):
        """Creates an empty child with given name and attributes.

        Parameters
        ----------
        node : Element
            Parent node
        name : str
            Name of the child to create.
        attribs : dict
            Dictionary of attributes for the child.

        Returns
        -------
        child : Element
            Node which had been created and added to the tree.
        """
        child = Element(name, attribs or {})
        self.markprocessed(child)
        node.append(child)
        return child
Exemple #4
0
    def setvalue(self,
                 node,
                 name,
                 converter,
                 value,
                 attribs=None,
                 hsdblock=False):
        """Creates a child with the given value.

        Parameters
        ----------
        node : Element
            Parent node.
        name : str
            Name of the child node to create.
        converter : converter object
            Object with methods fromhsd() and tohsd() which can
            convert between the hsd element and the desired type. See
            converters in hsd.converter for examples.
        value : arbitrary
            Value which should be converted to text by the converter.
        attribs : dict
            Dictionary with attributes for the child.
        hsdblock : bool, optional
            Whether the given value should be added in hsd block notation
            (enclosed in curly braces) instead of an assignment.

        Returns
        -------
        child : Element
            The child node which had been created and added.
        """

        child = Element(name, attribs or {})
        if converter:
            child.text = converter.tohsd(value)
        else:
            child.text = value
        self.markprocessed(child)
        if not hsdblock:
            child.sethsd(hsd.HSDATTR_EQUAL, True)
        node.append(child)
        return child
Exemple #5
0
    def getvaluenode(self,
                     node,
                     name,
                     defvalue=None,
                     defvaluename=None,
                     defattribs=None,
                     hsdblock=False,
                     allowtextvalue=False,
                     returnchild=False):
        """Returns the value node stored in a child with a given
        name. The child should contain either no nodes at all or only this one.

        Parameters
        ----------
        node : Element
            Parent node.
        name : string
            Name of the child to look for.
        defvalue : Element, optional
            If child is not found, it will be created and contain the specified
            node as subnode.
        defvaluename : string, optional
            If child is not found, it will be created and contain a subnode
            with the given name. If name is "", the child node will not
            contain a subnode. It is ignored, if defvalue had been specified.
        defattribs : dict, optional
            Default attribute dictionary used if child has not been found.
        hsdblock : bool, optional
            Whether the given value should be added in hsd block notation
            (enclosed in curly braces) instead of an assignment.
        allowtextvalue : bool, optional
            If set to yes, the child (if it exists) is allowed to have
            no subnode, but a text value instead. In that case the text
            value will be deleted and converted into an empty node.
        returnchild : bool, optional

        Returns
        -------
        node : Element or None
            The child node's first child or a node with the specified default
            name if child had not been found. In latter case, an
            appropriate child node with this as subnode is inserted into the
            tree.
        child : Element, optional
            The child not itself. Only returned if `returnchild=Yes` was set.

        Raises
        ------
        HSDMissingTagException
            If child was not found and no default value had been specified.
        HSDInvalidTagException
            If child has more than one child.

        Notes
        -----
        This routine should be used, if the child is not a leaf but
        contains a further child.
        """
        optional = defvalue is not None or defvaluename is not None
        child = self.findchild(node, name, optional)
        if child is not None:
            if len(child) > 1:
                raise hsd.HSDInvalidTagException(
                    "Tag '{}' is not allowed to have"
                    " more than one child".format(child.tag),
                    node=child)
            self.markprocessed(child)
            if len(child):
                self.markprocessed(child[0])
                return (child[0], child) if returnchild else child[0]
            elif allowtextvalue and child.text:
                valuenode = Element(child.text)
                child.text = ""
                child.append(valuenode)
                self.markprocessed(valuenode)
                return (valuenode, child) if returnchild else valuenode
            else:
                return (None, child) if returnchild else None
        else:
            if defvalue is None:
                defvalue = Element(defvaluename)
            child, valuenode = self.setvaluenode(node, name, defvalue,
                                                 defattribs, hsdblock)
            return (valuenode, child) if returnchild else valuenode