/// <summary> /// Return the argument associated with the key. /// If the key is not found, then return the default, /// If the default is null, then the argument is required, so /// if not found then null is returned and explanation is set. /// </summary> /// <param name="argList"></param> /// <param name="key"></param> /// <param name="defaultValue"></param> /// <returns></returns> private static string GetArgumentAsString(List <RequestArgument> argList, string key, string defaultValue, out string explanation) { explanation = ""; RequestArgument arg = argList.SingleOrDefault(rr => rr.Key == key); if (arg != null) { return(arg.Value); } else if (defaultValue == null) { explanation = $"Key={key}. Null default and key not found."; return(null); } else { return(defaultValue); } }
/// <summary> /// A raw request file has been place in the requests area. A SimEngine request file is built. /// The format of the raw file is: /// * Lines beginning with # are comments /// * lines that are empty after trimming are ignored /// * lines beginning with "action:" are followed by action name and argument /// * actions are a comma list, with first token the action name (e.g. Experiment) /// and other tokens in key=value pairs. E.g. Experiment,Project=MyProject,Experiment=MyExperiment /// or "Status,ID=MyID" /// </summary> /// <param name="fileDropFolderPath"></param> /// <param name="action"></param> /// <param name="projectFilename"></param> /// <param name="argsList"></param> /// <param name="explanation"></param> /// <returns></returns> private static bool ParseRawRequestFile(string fileDropFolderPath, List <SimEngineRequest> requestsList, out string explanation) { explanation = ""; if (requestsList == null) { explanation = $"RequestsList cannot be null"; return(false); } requestsList.Clear(); StringBuilder sbComments = new StringBuilder(); try { string[] lines = File.ReadAllLines(fileDropFolderPath); foreach (string line in lines) { string trimmedLine = line.Trim(); if (string.IsNullOrEmpty(trimmedLine)) { goto GetNextLine; } if (trimmedLine.StartsWith("#")) { sbComments.AppendLine(trimmedLine); goto GetNextLine; } string[] argTokens = line.Trim().Split(','); int lineNbr = 0; string action = ""; string project = ""; List <RequestArgument> argsList = new List <RequestArgument>(); foreach (string argToken in argTokens.ToList()) { lineNbr++; string[] pairTokens = argToken.Split('='); if (pairTokens.Length != 2) { explanation = $"Line#={lineNbr} Pair={argToken} is missing '='"; return(false); } string key = pairTokens[0].ToLower(); switch (key) { case "action": { action = pairTokens[1]; } break; case "project": { project = pairTokens[1]; } break; default: { RequestArgument arg = new RequestArgument(pairTokens[0], pairTokens[1]); argsList.Add(arg); } break; } } // foreach argPair if (!string.IsNullOrEmpty(action) && !string.IsNullOrEmpty(project)) { SimEngineRequest request = new SimEngineRequest(action, project, argsList); requestsList.Add(request); } else { explanation = $"Invalid Request at Line={lineNbr}. No Action or Project specified."; return(false); } GetNextLine :; } return(true); } catch (Exception ex) { explanation = $"File={fileDropFolderPath} Err={ex.Message}"; return(false); } }