1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

minor optimizations and clean up in shell.py

* cleans up method signatures
* drops checking for Command properties that no longer exist
This commit is contained in:
Peter Sprygada 2016-09-04 08:33:26 -04:00
parent a6c0f07fbb
commit a40515524f

View file

@ -135,11 +135,7 @@ class Shell(object):
window = self.strip(recv.read()) window = self.strip(recv.read())
if hasattr(cmd, 'prompt') and not handled: if hasattr(cmd, 'prompt') and not handled:
if self.handle_prompt(window, prompt=cmd.prompt, response=cmd.response): handled = self.handle_prompt(window, cmd)
handled = True
time.sleep(cmd.delay)
if cmd.is_reboot:
return
try: try:
if self.find_prompt(window): if self.find_prompt(window):
@ -167,18 +163,15 @@ class Shell(object):
def close(self): def close(self):
self.shell.close() self.shell.close()
def handle_prompt(self, resp, prompt, response): def handle_prompt(self, resp, cmd):
if not prompt or not response: prompt = to_list(cmd.prompt)
return response = to_list(cmd.response)
prompt = to_list(prompt)
response = to_list(response)
for pr, ans in zip(prompt, response): for pr, ans in zip(prompt, response):
match = pr.search(resp) match = pr.search(resp)
if match: if match:
cmd = '%s\r' % ans answer = '%s\r' % ans
self.shell.sendall(cmd) self.shell.sendall(answer)
return True return True
def sanitize(self, cmd, resp): def sanitize(self, cmd, resp):
@ -215,7 +208,7 @@ class CliBase(object):
self._connected = False self._connected = False
self.default_output = 'text' self.default_output = 'text'
def connect(self, params, kickstart=True, **kwargs): def connect(self, params, kickstart=True):
host = params['host'] host = params['host']
port = params.get('port') or 22 port = params.get('port') or 22
@ -242,7 +235,7 @@ class CliBase(object):
self._connected = True self._connected = True
def disconnect(self, **kwargs): def disconnect(self):
self.shell.close() self.shell.close()
self._connected = False self._connected = False
@ -251,22 +244,25 @@ class CliBase(object):
### Command methods ### ### Command methods ###
def execute(self, commands, **kwargs): def execute(self, commands):
try: try:
return self.shell.send(commands) return self.shell.send(commands)
except ShellError: except ShellError:
exc = get_exception() exc = get_exception()
raise NetworkError(exc.message, commands=commands) raise NetworkError(exc.message, commands=commands)
def run_commands(self, commands, **kwargs): def run_commands(self, commands):
return self.execute(to_list(commands)) return self.execute(to_list(commands))
### Config methods ### ### Config methods ###
def load_config(self, commands, **kwargs): def configure(self, commands):
raise NotImplementedError raise NotImplementedError
def replace_config(self, commands, **kwargs): def get_config(self, commands):
raise NotImplementedError
def load_config(self, commands):
raise NotImplementedError raise NotImplementedError
def save_config(self): def save_config(self):