Beispiel #1
0
    def Add(self,
            variableName: str,
            variableOptionList: List[str],
            linkedVariableName: Optional[str] = None) -> None:
        if len(variableOptionList) <= 0:
            raise VariableOptionListCanNotBeEmptyException(variableName)

        theDict = self._GetDict()
        if variableName in theDict:
            if self.__AllowAutoVariablesOverride and variableName == ToolAddedVariant.CONFIG:
                currentDef = theDict[variableName]
                if not self.__HasSameOptions(variableOptionList,
                                             currentDef.Options):
                    if self.__Log is not None:
                        self.__Log.LogPrintWarning(
                            "Overriding the auto variable '{0}' and the option list is different. New: {1}, old: {2}"
                            .format(variableName, variableOptionList,
                                    currentDef.Options))
                    theDict[variableName] = VariableReport(
                        variableName, variableOptionList,
                        currentDef.LinkTargetName)
                return
            raise VariableAlreadyDefinedException(
                "The variable '{0}' has already been added".format(
                    variableName))

        if linkedVariableName is not None:
            if linkedVariableName == variableName:
                raise LinkToSelfNotAllowedException(variableName)
            if linkedVariableName not in theDict:
                raise LinkedVariableNotFoundException(linkedVariableName)
            linkedOptionList = theDict[linkedVariableName].Options
            if len(linkedOptionList) != len(variableOptionList):
                raise VariableDoesNotContainTheSameAmountOfOptionsAsTheLinkedTargetException(
                    variableName, str(variableOptionList), linkedVariableName,
                    str(linkedOptionList))

        self.__Order.append(variableName)
        theDict[variableName] = VariableReport(variableName,
                                               variableOptionList,
                                               linkedVariableName)
Beispiel #2
0
    def __init__(self,
                 source: str,
                 variableDict: Optional[VariableDict],
                 environmentVariableResolver: Optional[
                     FormatStringEnvironmentVariableResolver] = None,
                 noVariableResolve: bool = False,
                 noEnvVariableResolve: bool = False) -> None:
        super().__init__()
        self.SplitList = []  # type: List[str]
        self.VarCommandList = []  # type: List[LookupVariableCommand]
        self.EnvCommandList = [
        ]  # type: List[LookupEnvironmentVariableCommand]

        if source == "":
            self.SplitList.append("")
            return

        state = ParseState.Scanning
        startSplitIndex = 0
        blockStartIndex = 0
        for index, ch in enumerate(source):
            if state == ParseState.Scanning:
                if ch == '$':
                    state = ParseState.CommandStart
            elif state == ParseState.CommandStart:
                if ch == '{':
                    state = ParseState.VariableBlock
                    if startSplitIndex < index - 1:
                        self.SplitList.append(source[startSplitIndex:index -
                                                     1])
                    startSplitIndex = index - 1
                    blockStartIndex = index + 1
                elif ch == '(':
                    state = ParseState.EnvBlock
                    if startSplitIndex < index - 1:
                        self.SplitList.append(source[startSplitIndex:index -
                                                     1])
                    startSplitIndex = index - 1
                    blockStartIndex = index + 1
                    #stringEndSplitIndex = blockStartIndex-2
            elif state == ParseState.VariableBlock:
                if ch == '}':
                    state = ParseState.Scanning
                    self.SplitList.append(source[startSplitIndex:index + 1])
                    startSplitIndex = index + 1
                    # Do the report lookup
                    variableName = (source[blockStartIndex:index])
                    if not Util.IsValidCStyleName(variableName):
                        raise FormatStringInvalidVariableNameException(
                            variableName)

                    if noVariableResolve:
                        variableValue = VariableReport(
                            "*NotDefined*", ["*NotDefined*"],
                            None)  # type: Optional[VariableReport]
                    else:
                        variableValue = variableDict.TryGetVariableReport(
                            variableName) if variableDict is not None else None
                    if variableValue is None:
                        raise FormatStringUndefinedVariableNameException(
                            variableName)

                    self.VarCommandList.append(
                        LookupVariableCommand(variableName, variableValue,
                                              len(self.SplitList) - 1))
                    #stringEndSplitIndex = blockStartIndex
            elif state == ParseState.EnvBlock:
                if ch == ')':
                    state = ParseState.Scanning
                    self.SplitList.append(source[startSplitIndex:index + 1])
                    startSplitIndex = index + 1

                    envName = source[blockStartIndex:index]
                    if not Util.IsValidCStyleName(envName):
                        raise FormatStringInvalidVariableNameException(envName)

                    if noEnvVariableResolve:
                        envValue = "*NotDefined*"  # type: Optional[str]
                    elif environmentVariableResolver is None:
                        envValue = IOUtil.TryGetEnvironmentVariable(envName)
                    else:
                        envValue = environmentVariableResolver(envName)

                    if envValue is None:
                        raise FormatStringUndefinedEnvironmentVariableNameException(
                            envName)

                    self.EnvCommandList.append(
                        LookupEnvironmentVariableCommand(
                            envName, envValue,
                            len(self.SplitList) - 1))

        if startSplitIndex < len(source):
            self.SplitList.append(source[startSplitIndex:])

        if state != ParseState.Scanning and state != ParseState.CommandStart:
            if state == ParseState.VariableBlock:
                raise FormatStringVariableMissingTerminationException(
                    "The string '{0}' is missing a terminating '}}' ".format(
                        source))
            elif state == ParseState.EnvBlock:
                raise FormatStringEnvironmentVariableMissingTerminationException(
                    "The string '{0}' is missing a terminating ')' ".format(
                        source))
            raise FormatStringInvalidException(
                "The string '{0}' contained invalid format codes".format(
                    source))
Beispiel #3
0
 def Add(self, variableName: str, variableValue: str) -> None:
     theDict = self._GetDict()
     theDict[variableName] = VariableReport(variableName, [variableValue],
                                            None)