Changeset 4 for trunk/python/Rappture


Ignore:
Timestamp:
Mar 18, 2005 10:04:28 AM (19 years ago)
Author:
mmc
Message:

Added "type#" capability to the put() method, so you can put
additional tags of the same type within a parent node. Also,
added a README file with some examples.

File:
1 edited

Legend:

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

    r3 r4  
    2121    re_xml = re.compile('<\?[Xx][Mm][Ll]')
    2222    re_dots = re.compile('(\([^\)]*)\.([^\)]*\))')
    23     re_pathElem = re.compile('^(([a-zA-Z_]+)([0-9]*))?(\(([^\)]+)\))?$')
     23    re_pathElem = re.compile('^(([a-zA-Z_]+#?)([0-9]*))?(\(([^\)]+)\))?$')
    2424    re_pathCreateElem = re.compile('^([^\(]+)\(([^\)]+)\)$')
    2525
     
    5151        By default, this method returns an object representing the
    5252        DOM node referenced by the path.  This is changed by setting
    53         the "flavor" argument to "name" (for name of the tail element),
     53        the "flavor" argument to "id" (for name of the tail element),
    5454        to "type" (for the type of the tail element), to "component"
    55         (for the component name "type(name)"), or to "object"
     55        (for the component name "type(id)"), or to "object"
    5656        for the default (an object representing the tail element).
    5757        """
     
    6565        elif flavor == 'component':
    6666            return self._node2comp(node)
    67         elif flavor == 'name':
     67        elif flavor == 'id':
    6868            return self._node2name(node)
    6969        elif flavor == 'type':
    7070            return node.tagName
    7171
    72         raise ValueError, "bad flavor '%s': should be object, name, type" % flavor
     72        raise ValueError, "bad flavor '%s': should be object, id, type" % flavor
    7373
    7474    # ------------------------------------------------------------------
     
    8484        By default, this method returns a list of objects representing
    8585        the children.  This is changed by setting the "flavor" argument
    86         to "name" (for tail names of all children), to "type" (for the
     86        to "id" (for tail names of all children), to "type" (for the
    8787        types of all children), to "component" (for the path component
    8888        names of all children), or to "object" for the default (a list
     
    103103        elif flavor == 'component':
    104104            return [self._node2comp(n) for n in nlist]
    105         elif flavor == 'name':
     105        elif flavor == 'id':
    106106            return [self._node2name(n) for n in nlist]
    107107        elif flavor == 'type':
     
    224224        """
    225225        Used internally to create a path component name for the
    226         specified node.  A path component name has the form "type(name)"
     226        specified node.  A path component name has the form "type(id)"
    227227        or just "type##" if the node doesn't have a name.  This name
    228228        can be used in a path to uniquely address the component.
     
    254254        Used internally to find a particular element within the
    255255        root node according to the path, which is a string of
    256         the form "type##(name).type##(name). ...", where each
    257         "type" is a tag <type>; if the optional ## is specified,
     256        the form "typeNN(id).typeNN(id). ...", where each
     257        "type" is a tag <type>; if the optional NN is specified,
    258258        it indicates an index for the <type> tag within its parent;
    259         if the optional (name) part is included, it indicates a
    260         tag of the form <type id="name">.
     259        if the optional (id) part is included, it indicates a
     260        tag of the form <type id="id">.
    261261
    262262        By default, it looks for an element along the path and
     
    264264        it creates various elements along the path as it goes.
    265265        This is useful for "put" operations.
     266
     267        If you include "#" instead of a specific number, a node
     268        will be created automatically with a new number.  For example,
     269        the path "foo.bar#" called the first time will create "foo.bar",
     270        the second time "foo.bar1", the third time "foo.bar2" and
     271        so forth.
    266272
    267273        Returns an object representing the element indicated by
     
    280286        for part in path:
    281287            #
    282             # Pick apart "type123(name)" for this part.
     288            # Pick apart "type123(id)" for this part.
    283289            #
    284290            match = self.re_pathElem.search(part)
    285291            if not match:
    286                 raise ValueError, "bad path component '%s': should have the form 'type123(name)'" % part
     292                raise ValueError, "bad path component '%s': should have the form 'type123(id)'" % part
    287293
    288294            (dummy, type, index, dummy, name) = match.groups()
     
    306312            else:
    307313                #
    308                 # If the name is like "type(name)", then look for elements
     314                # If the name is like "type(id)", then look for elements
    309315                # that match the type and see if one has the requested name.
    310                 # if the name is like "(name)", then look for any elements
     316                # if the name is like "(id)", then look for any elements
    311317                # with the requested name.
    312318                #
     
    333339                #
    334340                # If the "create" flag is set, then create a node
    335                 # with the specified "type(name)" and continue on.
     341                # with the specified "type(id)" and continue on.
     342                # If the type is "type#", then create a node with
     343                # an automatic number.
    336344                #
    337345                match = self.re_pathCreateElem.search(part)
     
    342350                    name = ""
    343351
    344                 node = self.doc.createElement(type)
    345                 lastnode.appendChild(node)
     352                if type.endswith('#'):
     353                    type = type.rstrip('#')
     354                    node = self.doc.createElement(type)
     355
     356                    # find the last node of same type and append there
     357                    pos = None
     358                    for n in lastnode.childNodes:
     359                        if n.nodeName == type:
     360                            pos = n
     361                    if pos:
     362                        pos = pos.nextSibling
     363
     364                    if pos:
     365                        lastnode.insertBefore(node,pos)
     366                    else:
     367                        lastnode.appendChild(node)
     368                else:
     369                    node = self.doc.createElement(type)
     370                    lastnode.appendChild(node)
    346371
    347372                if name:
    348                     node.setAttribute("name",name)
     373                    node.setAttribute("id",name)
    349374
    350375            lastnode = node
Note: See TracChangeset for help on using the changeset viewer.