Changeset 3


Ignore:
Timestamp:
Mar 17, 2005, 8:43:22 PM (20 years ago)
Author:
mmc
Message:

Added put() and remove() options to the Rappture.library,
so you can add and remove information from the tree.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/python/Rappture/library.py

    r2 r3  
    117117        """
    118118
    119         if path == "":
    120             node = self.node
    121         else:
    122             node = self._find(path)
    123 
     119        node = self._find(path)
    124120        if not node:
    125121            return None
     
    130126
    131127        return None
     128
     129    # ------------------------------------------------------------------
     130    def put(self, path="", value="", id=None, append=0):
     131        """
     132        Clients use this to set the value of a node.  If the path
     133        is not specified, it sets the value for the root node.
     134        Otherwise, it sets the value for the element specified
     135        by the path.  If the value is a string, then it is treated
     136        as the text within the tag at the tail of the path.  If it
     137        is a DOM node or a library, then it is inserted into the
     138        tree at the specified path.
     139
     140        If the optional id is specified, then it sets the identifier
     141        for the tag at the tail of the path.  If the optional append
     142        flag is specified, then the value is appended to the current
     143        value.  Otherwise, the value replaces the current value.
     144        """
     145
     146        node = self._find(path,create=1)
     147
     148        if append:
     149            nlist = [n for n in node.childNodes
     150                       if not n.nodeType == n.TEXT_NODE]
     151        else:
     152            nlist = node.childNodes
     153
     154        for n in nlist:
     155            node.removeChild(n)
     156
     157        if value:
     158            # if there's a value, then add it to the node
     159            if isinstance(value, library):
     160                node.appendChild(value.node)
     161            elif isinstance(value, minidom.Node):
     162                node.appendChild(value)
     163            elif value and value != '':
     164                n = self.doc.createTextNode(value)
     165                node.appendChild(n)
     166
     167        # if there's an id, then set it on the node
     168        if id:
     169            node.setAttribute("id",id)
     170
     171    # ------------------------------------------------------------------
     172    def remove(self, path=""):
     173        """
     174        Clients use this to remove the specified node.  Removes the
     175        node from the tree and returns it as an element, so you can
     176        put it somewhere else if you like.
     177        """
     178
     179        node = self._find(path)
     180        if not node:
     181            return None
     182
     183        rval = library('<?xml version="1.0"?>' + node.toxml())
     184        node.parentNode.removeChild(node)
     185
     186        return rval
    132187
    133188    # ------------------------------------------------------------------
Note: See TracChangeset for help on using the changeset viewer.