예제 #1
0
    def match(self, path, value=None):
        """Return the matches of the path expression 'path'. The returned paths
        are sufficiently qualified to make sure that they match exactly one
        node in the current tree.

        Path expressions use a very simple subset of XPath: the path 'path'
        consists of a number of segments, separated by '/'; each segment can
        either be a '*', matching any tree node, or a string, optionally
        followed by an index in brackets, matching tree nodes labelled with
        exactly that string. If no index is specified, the expression matches
        all nodes with that label; the index can be a positive number N, which
        matches exactly the Nth node with that label (counting from 1), or the
        special expression 'last()' which matches the last node with the given
        label. All matches are done in fixed positions in the tree, and nothing
        matches more than one path segment."""

        # Sanity checks
        if not isinstance(path, string_types):
            raise TypeError("path MUST be a string!")
        if not isinstance(value, string_types) and value is not None:
            raise TypeError("value MUST be a string or None!")
        if not self.__handle:
            raise RuntimeError("The Augeas object has already been closed!")

        parray = ffi.new('char***')

        ret = lib.aug_match(self.__handle, enc(path), parray)
        if ret < 0:
            raise RuntimeError("Error during match procedure!", path)

        # Loop through the string array
        array = parray[0]
        matches = []
        for i in range(ret):
            if array[i] != ffi.NULL:
                # Create a python string and append it to our matches list
                item = ffi.string(array[i])
                if value is not None:
                    matched_value = self.get(item)
                    if matched_value == value:
                        matches.append(dec(item))
                else:
                    matches.append(dec(item))

                lib.free(array[i])
        lib.free(array)
        return matches
예제 #2
0
    def match(self, path):
        """
        Return the matches of the path expression `path`. The returned paths
        are sufficiently qualified to make sure that they match exactly one
        node in the current tree.

        Path expressions use a very simple subset of XPath: the path `path`
        consists of a number of segments, separated by :samp:`/`; each segment
        can either be a :samp:`*`, matching any tree node, or a string,
        optionally followed by an index in brackets, matching tree nodes
        labelled with exactly that string. If no index is specified, the
        expression matches all nodes with that label; the index can be a
        positive number N, which matches exactly the *N*-th node with that
        label (counting from 1), or the special expression :samp:`last()` which
        matches the last node with the given label. All matches are done in
        fixed positions in the tree, and nothing matches more than one path
        segment.
        """

        # Sanity checks
        if not isinstance(path, string_types):
            raise TypeError("path MUST be a string!")
        if not self.__handle:
            raise RuntimeError("The Augeas object has already been closed!")

        parray = ffi.new('char***')

        ret = lib.aug_match(self.__handle, enc(path), parray)
        if ret < 0:
            self._raise_error(AugeasRuntimeError,
                              "Augeas.match() failed: %s", path)

        # Loop through the string array
        array = parray[0]
        matches = []
        for i in range(ret):
            if array[i] != ffi.NULL:
                # Create a python string and append it to our matches list
                item = ffi.string(array[i])
                matches.append(dec(item))
                lib.free(array[i])
        lib.free(array)
        return matches