Skip to content

Commit

Permalink
Merge pull request #3 from EVOLVED-5G/publish_verdict
Browse files Browse the repository at this point in the history
Publish verdict
  • Loading branch information
NaniteBased authored Feb 16, 2022
2 parents afa38ee + 7abf6bd commit 5871f92
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 17 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
**16/02/2022** [Version 3.2.2]

- Enhanced PublishFromFile and PublishFromPreviousTaskLog:
- Make 'Keys' parameter optional
- Allow setting a Verdict

**14/02/2022** [Version 3.2.1]

- Fix redirection of command line output (Cli/TapExecute)
Expand Down
11 changes: 9 additions & 2 deletions Executor/Tasks/Run/publish_from_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ def __init__(self, name, parent, params, logMethod):
super().__init__(name, parent, params, logMethod, None)
self.paramRules = {
'Pattern': (None, True),
'Keys': (None, True),
'Path': (None, False) # Mandatory only for PublishFromFile, handled below
'Keys': ([], False),
'Path': (None, False), # Mandatory only for PublishFromFile, handled below
'VerdictOnMatch': ("NotSet", False),
'VerdictOnNoMatch': ("NotSet", False),
}

def Run(self):
Expand All @@ -19,6 +21,8 @@ def Run(self):
filePath = self.params["Path"]
pattern = self.params["Pattern"]
keys = self.params["Keys"]
onMatch = self.GetVerdictFromName(self.params["VerdictOnMatch"])
onNoMatch = self.GetVerdictFromName(self.params["VerdictOnNoMatch"])

self.Log(Level.DEBUG, f"Looking for pattern: '{pattern}'; Assigning groups as:")

Expand All @@ -31,12 +35,15 @@ def Run(self):

regex = re.compile(pattern)

matchFound = False
for line in self.generator({"Path": filePath}):
match = regex.match(line)
if match:
self.Log(Level.INFO, f"Match found: {match.string}")
matchFound = True
for index, key in keys:
self.Publish(key, match.group(index))
self.Verdict = onMatch if matchFound else onNoMatch

def generator(self, params: Dict):
raise NotImplementedError()
Expand Down
16 changes: 3 additions & 13 deletions Executor/Tasks/Run/upgrade_verdict.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,9 @@ def __init__(self, logMethod, parent, params):
}

def Run(self):
from Executor import Verdict

def _assignVerdict(name: str) -> Verdict | None:
try:
return Verdict[name]
except KeyError:
self.SetVerdictOnError()
self.Log(Level.ERROR, f"Unrecognized Verdict '{name}'")
return None

onMiss = _assignVerdict(self.params["VerdictOnMissing"])
onMatch = _assignVerdict(self.params["VerdictOnMatch"])
onNoMatch = _assignVerdict(self.params["VerdictOnNoMatch"])
onMiss = self.GetVerdictFromName(self.params["VerdictOnMissing"])
onMatch = self.GetVerdictFromName(self.params["VerdictOnMatch"])
onNoMatch = self.GetVerdictFromName(self.params["VerdictOnNoMatch"])
if None in [onMiss, onMatch, onNoMatch]: return

key = self.params["Key"]
Expand Down
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -510,13 +510,20 @@ Will produce this message in the log:

### Run.PublishFromFile / Run.PublishFromPreviousTaskLog
Reads the contents of a file / the log of the previous task and looks for lines that match the specified regular
expression pattern, publishing the groups found. Configuration values:
expression pattern, publishing the groups found. If multiple matches are found only the last one is saved.
Configuration values:
- `Pattern`: Regular expression to try to match, following
[Python's syntax](https://docs.python.org/3/library/re.html#regular-expression-syntax).
- `Keys`: List of (index, key) pairs, where index refers to the regex group, and key is the identifier to use when publishing.
> Extra escaping may be needed inside the regular expression, for example `\d` needs to be written as `\\d`, otherwise
> an exception will occur when parsing the YAML file ("unknown escape character 'd'").
- `Keys`: List of (index, key) pairs, where index refers to the regex group, and key is the identifier to use
when publishing. If not included, nothing will be published (an empty list will be used). This can be useful
if only setting a Verdict is needed.
> - Groups are defined within regular expressions using '(' ... ')'.
> - Group 0 always refers to the complete matched line, manually specified groups start at index 1.
> - While writing the `Keys` in the task configuration note that YAML does not have a syntax for tuples, use lists of two elements instead.
- `VerdictOnMatch`: Verdict to set if a line matches the regular expression. Defaults to `NotSet`.
- `VerdictOnNoMatch`: Verdict to set if no line matches the regular expression. Defaults to `NotSet`.
- `Path` (only for Run.PublishFromFile): Path of the file to read

### Run.RestApi
Expand Down
9 changes: 9 additions & 0 deletions Task/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,12 @@ def SetVerdictOnError(self):
self.Verdict = Verdict[verdict]
except KeyError as e:
raise RuntimeError(f"Unrecognized Verdict '{verdict}'") from e

def GetVerdictFromName(self, name: str):
from Executor import Verdict
try:
return Verdict[name]
except KeyError:
self.SetVerdictOnError()
self.Log(Level.ERROR, f"Unrecognized Verdict '{name}'")
return None

0 comments on commit 5871f92

Please sign in to comment.