def __init__(self, name: str, formattedName: str, splitIndex: int) -> None: if not Util.IsValidCStyleName(name): raise FormatStringInvalidVariableNameException(name) self.Name = name self.FormattedName = formattedName # if this is negative then its a virtual entry self.SplitIndex = splitIndex
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))