Example #1
0
    def _get_closing_bracket_index(self, index, s, location, lineno):
        """ private method used by _replace_vars to count the closing brackets.
            
            Args:
               index. The index from where to look for a closing bracket
               s. The string to parse
               group. group and options that are substituted. Mainly used to create a nice exception message
               option. option that is substituted. Mainly used to create a nice exception message
               
            Returns: the index of the found closing bracket
        
            Raises:
               exception NoSectionError if the section cannot be found
        """

        tolook = s[index + 2:]

        opening_brack = 1
        closing_brack_index = index + 2

        i = 0
        for _ch in tolook:
            if _ch == ')':
                if opening_brack == 1:
                    return closing_brack_index
                else:
                    opening_brack -= 1

            elif _ch == '(':
                if tolook[i - 1] == '%':
                    opening_brack += 1

            # inc index
            closing_brack_index += 1
            i += 1

        raise exceptions.SubstitutionError(
            lineno, location, "Missing a closing bracket in %s" % (tolook))
Example #2
0
    def _replace_vars(self, a_str, location, lineno=-1):
        """ private replacing all variables. A variable will be in the from of %(group[option]).
            Multiple variables are supported, ex /foo/%(group1[opt1])/%(group2[opt2])/bar
            Nested variables are also supported, ex /foo/%(group[%(group1[opt1]].
            Note that the group part cannot be substituted, only the option can. This is because of the Regular Expression _SUBSGROUPRE that accepts only words as values.
            
            Args:
               index. The index from where to look for a closing bracket
               s. The string to parse
               
            Returns: the final string with the replacements
        
            Raises:
               exception NoSectionError if the section cannot be found
        """

        toparse = a_str

        index = toparse.find("%(")

        # if found opening %( look for end bracket)
        if index >= 0:
            # look for closing brackets while counting openings one
            closing_brack_index = self._get_closing_bracket_index(
                index, a_str, location, lineno)

            #print "closing bracket %d"%(closing_brack_index)
            var = toparse[index:closing_brack_index + 1]

            dummy = None

            m = self._SUBSGROUPRE.match(var)

            if m == None:
                raise exceptions.SubstitutionError(
                    lineno, location,
                    "Cannot match a group[option] in %s but found an opening bracket (. Malformated expression "
                    % (var))
            else:

                # recursive calls
                g = self._replace_vars(m.group('group'), location, -1)
                o = self._replace_vars(m.group('option'), location, -1)

                try:
                    # if it is in ENVGROUP then check ENV variables with a Resource object
                    # if it is in CLIGROUP then check CLI argument with a Resource object
                    # otherwise check in standard groups
                    if g == Conf._ENVGROUP:
                        r = Resource(CliArgument=None, EnvVariable=o)
                        dummy = r.getValue()
                    elif g == Conf._CLIGROUP:
                        r = Resource(CliArgument=o, EnvVariable=None)
                        dummy = r.getValue()
                    else:
                        dummy = self._sections[g][self.optionxform(o)]
                except KeyError, _:  #IGNORE:W0612
                    raise exceptions.SubstitutionError(
                        lineno, location,
                        "Property %s[%s] doesn't exist in this configuration file \n"
                        % (g, o))

            toparse = toparse.replace(var, dummy)

            return self._replace_vars(toparse, location, -1)