def _remove(self, path):
        """
        Remove a child object by it path.
        """
        # if the patherence is a long patherence (dotted name)
        # first get the _parent object and call the remove to the _parent
        (parentref, name) = utils.dottedref.psplit_ref(path)
        if parentref != "":
            self._get(parentref)._remove(name)
        elif utils.dottedref.get_index(path) != None and \
             self._get(utils.dottedref.get_name(path)):
            # Delete If the given pathelem is referring to a list
            name = utils.dottedref.get_name(path)
            index = utils.dottedref.get_index(path)
            del self._children[name][index]
            if len(self._children[name]) == 0:
                del self._order[self._order.index(name)]
        elif self._get(path) != None:  # delete if the child is found
            del self._children[path]
            # hidded children are not added to the order list
            if not path.startswith('?'):
                del self._order[self._order.index(path)]

        else:
            raise exceptions.NotFound("Child %s not found!" % path)
    def _get_index(self, name):
        """
        Get the index of a child object by its name. The index matches the index
        of the child object in the _children array. 
        @return: integer. 
        @raise NotFound: when object is not found from the children.  
        """

        try:
            return self._order.index(name)
        except KeyError:
            raise exceptions.NotFound("Child %s not found!" % name)
Beispiel #3
0
    def __getitem__(self, name):
        if self._feature is not None:
            if name == '_name': return self._feature.name
            elif name == '_namespace': return self._feature.namespace
            elif name == '_value': return self._feature.get_value()
            elif name == '_fqr': return self._feature.fqr
            elif name == '_type': return self._feature.type

        try:
            return self._children[name]
        except KeyError:
            if self._feature:
                msg = "Feature '%s.%s' not found" % (self._feature.fqr, name)
            else:
                msg = "Feature '%s' not found" % name
            raise exceptions.NotFound(msg)
 def _find_parent(self, **kwargs):
     """
     find a _parent object by arguments. You can define any number of object attributes that 
     have to match to the object. 
     Example1:
        _find_parent(foobar=True) searches for a _parent
       object which has a member attribute foobar and its value is True. 
     Example2:
        _find_parent(name="test") searches for a _parent
       object which has a member attribute name and its value is "test". 
     Example3: type is a special case
        _find_parent(type=Configuration) searches for a _parent
       object which is an instance of Configuration (checked with isinstance). 
     @param kwargs: 
     @return: The object that matches the arguments
     @raise exceptions.NotFound: When no matching parent is found
     """
     type = kwargs.get('type', None)
     if hasattr(self, '_parent') and self._parent != None:
         found = True
         for key in kwargs.keys():
             try:
                 # handle type as a special case
                 if key == 'type':
                     if not isinstance(self._parent, kwargs.get(key)):
                         found = False
                         break
                 elif key == 'match':
                     if not self._parent == kwargs.get(key):
                         found = False
                         break
                 elif not getattr(self._parent, key) == kwargs.get(key):
                     found = False
                     break
             except AttributeError:
                 found = False
                 break
         if found:
             return self._parent
         else:
             return self._parent._find_parent(**kwargs)
     else:
         raise exceptions.NotFound("Parent not found!")
    def _get(self, path):
        """
        Get a child object by it path.
        @return: The child object if it is found. 
        @raise NotFound: when object is not found from the children.  
        """

        try:
            # traverse to the actual child element
            curelem = self
            for pathelem in utils.dottedref.split_ref(path):
                if utils.dottedref.get_index(pathelem) == None:
                    curelem = curelem._children[pathelem]
                else:
                    # If the given pathelem is referring to a list
                    name = utils.dottedref.get_name(pathelem)
                    index = utils.dottedref.get_index(pathelem)
                    curelem = utils.get_list(curelem._children[name])[index]
            return curelem
        # Catch the KeyError exception from dict and IndexError from list
        except (KeyError, IndexError), e:
            raise exceptions.NotFound("Child %s not found from %s! %s" %
                                      (path, self, e))
 def get_feature(self, ref):
     if ref in VALUES: return DummyFeature(ref)
     else: raise exceptions.NotFound()
 def get_resource(self, ref):
     if ref in self.resources:
         return DummyResource(self.resources[ref])
     else:
         raise exceptions.NotFound("No such resource '%s'!" % ref)