diff --git a/cadoles/eole/plugins/modules/instance.py b/cadoles/eole/plugins/modules/instance.py index 721c7c2..dce6d9e 100644 --- a/cadoles/eole/plugins/modules/instance.py +++ b/cadoles/eole/plugins/modules/instance.py @@ -104,11 +104,10 @@ class ExpectationCollection: expectations = [exp for exps in self.expectations_lookup.values() for exp in exps] if recursive: expectations.extend([exp for exps in expectations for exp in exps.get_following_expectations()]) - pass - return expectations + return list(set(expectations)) def count_expectations(self): - return sum([exp.count_expectations() for exp in self.get_expectations()]) + return len(self.get_expectations(recursive=True)) def merge(self, collection): for expectation in collection.get_expectations(): @@ -121,21 +120,24 @@ class Expectation: self.context = [p.strip() for p in multiline_pattern.strip().split('\n') if p.strip()] self.pattern = self.context[-1] self.response = response - self.next = None + self.next = {} - def get_pattern(self): - return re.compile(f'(.*){re.escape(self.pattern)}(.*)') + def get_pattern(self, previous_answer=None): + return re.compile(f'(.*){re.escape(self.pattern.format(variable=previous_answer))}(.*)') - def set_next_expectation(self, expectation): - self.next = expectation + def set_next_expectation(self, expectation, triggers=None): + if not isinstance(triggers, list): + triggers = [triggers] + for trigger in triggers: + self.next[trigger] = expectation def set_response(self, response): print(f'Setting {response} for {self.pattern}') self.response = response - def expect(self, spawned): - print(f'-> expecting next "{self.pattern}"') - p = spawned.expect([pexpect.EOF, pexpect.TIMEOUT, self.get_pattern()]) + def expect(self, spawned, previous_answer=None): + print(f'-> expecting next "{self.pattern.format(variable=previous_answer)}"') + p = spawned.expect([pexpect.EOF, pexpect.TIMEOUT, self.get_pattern(previous_answer=previous_answer)]) if p not in [0, 1]: self.answer(spawned) else: @@ -145,8 +147,10 @@ class Expectation: print(f'-> answering "{self.response}" to "{spawned.after}"') if self.response is not None: spawned.sendline(self.response) - if self.next: - self.next.expect(spawned) + if self.response in self.next: + self.next[self.response].expect(spawned, previous_answer=self.response) + elif None in self.next: + self.next[None].expect(spawned, previous_answer=self.response) def is_the_one(self, context): #print(f'testing if it is the one {self.get_pattern()} for {context}') @@ -160,18 +164,12 @@ class Expectation: return False return True - def count_expectations(self): - count = 1 - if self.next: - count += self.next.count_expectations() - return count - def get_following_expectations(self): - if self.next: - return [self.next] + self.next.get_following_expectations() - return [] - - + following = [] + for next_expectation in set(self.next.values()): + following.append(next_expectation) + following.extend(next_expectation.get_following_expectations()) + return following expectations = ExpectationCollection()