Liste récursive des interactions

This commit is contained in:
Benjamin Bohard 2021-09-02 13:47:47 +02:00
parent 9887189b38
commit 879a042487
2 changed files with 9 additions and 7 deletions

View File

@ -36,10 +36,10 @@ final = Expectation("""1 -> Ne rien faire
Entrez le numéro de votre choix :""", response='2', name='final') Entrez le numéro de votre choix :""", response='2', name='final')
expectations.add_expectation(already_registered) expectations.add_expectation(already_registered)
expectations.add_expectation(network_configuration) expectations.add_expectation(network_configuration)
expectations.add_expectation(new_server) expectations.add_expectation(new_server)
expectations.add_expectation(zephir_address)
expectations.add_expectation(hardware) expectations.add_expectation(hardware)
expectations.add_expectation(key_available) expectations.add_expectation(key_available)
expectations.add_expectation(final) expectations.add_expectation(final)
@ -48,6 +48,7 @@ network_configuration.set_next_expectation(interface_name, triggers=['O', 'Oui',
interface_name.set_next_expectation(network_address) interface_name.set_next_expectation(network_address)
network_address.set_next_expectation(network_netmask) network_address.set_next_expectation(network_netmask)
network_netmask.set_next_expectation(gateway) network_netmask.set_next_expectation(gateway)
network_configuration.set_next_expectation(zephir_address, triggers=['N', 'Non', 'NON'])
new_server.set_next_expectation(rne, triggers=['N', 'Non', 'NON']) new_server.set_next_expectation(rne, triggers=['N', 'Non', 'NON'])
rne.set_next_expectation(server_id, triggers='') rne.set_next_expectation(server_id, triggers='')

View File

@ -37,8 +37,7 @@ class ExpectationCollection:
expectations = [exp for exps in self.expectations_lookup.values() for exp in exps] expectations = [exp for exps in self.expectations_lookup.values() for exp in exps]
if recursive: if recursive:
expectations.extend([exp for exps in expectations for exp in exps.get_following_expectations()]) expectations.extend([exp for exps in expectations for exp in exps.get_following_expectations()])
pass return list(set(expectations))
return expectations
def count_expectations(self): def count_expectations(self):
return len(self.get_expectations(recursive=True)) return len(self.get_expectations(recursive=True))
@ -70,7 +69,7 @@ class Expectation:
self.response = response self.response = response
def expect(self, spawned, previous_answer=None): def expect(self, spawned, previous_answer=None):
print(f'-> expecting next "{self.pattern.format(variable=previous_answer}"') print(f'-> expecting next "{self.pattern.format(variable=previous_answer)}"')
p = spawned.expect([pexpect.EOF, pexpect.TIMEOUT, self.get_pattern(previous_answer=previous_answer)]) p = spawned.expect([pexpect.EOF, pexpect.TIMEOUT, self.get_pattern(previous_answer=previous_answer)])
if p not in [0, 1]: if p not in [0, 1]:
self.answer(spawned) self.answer(spawned)
@ -99,6 +98,8 @@ class Expectation:
return True return True
def get_following_expectations(self): def get_following_expectations(self):
for next_expectation in self.next.values(): following = []
return [next_expectation] + next_expectation.get_following_expectations() for next_expectation in set(self.next.values()):
return [] following.append(next_expectation)
following.extend(next_expectation.get_following_expectations())
return following