Skip to content

Commit

Permalink
Interactive mode fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
davidBMSTU committed Mar 6, 2024
1 parent f6bd884 commit bb44893
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 23 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = cotea
version = 1.3.15
version = 1.3.16
author = David Badalyan
author_email = [email protected]
description = Tool that provides Python API to run Ansible programmatically.
Expand Down
43 changes: 23 additions & 20 deletions src/cotea/debug_utils.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from ansible.playbook.task import Task
from cotea.runner import runner
from cotea.utils import get_string_from_input


def print_help_msg():
help_msg = "Informative commands:\n"
help_msg += "'ft' - print info about the Failed Task\n"
help_msg += "'msg' - print all ansible error MSGs (including the ignored ones)\n"
help_msg += "'p' - print Progress bar\n"
#help_msg += "'p' - print Progress bar\n"
help_msg += "'h'/'help' - print this msg\n"
help_msg += "\nAction commands:\n"
help_msg += "'re' - RErun of the failed task\n"
Expand Down Expand Up @@ -49,8 +50,9 @@ def interactive_discotech(failed_task: Task, r: runner):
print("\nINTERACTIVE MODE")

while True:
print("Enter command: ", end="")
command = input()
command = get_string_from_input("Enter command: ")
command = command.strip(" ")

if command == "ft":
pretty_print_task(failed_task)

Expand All @@ -68,31 +70,32 @@ def interactive_discotech(failed_task: Task, r: runner):
msg_number = 1

for msg in err_msgs:
print("MSG number {}:\n{}\n".format(msg_number, msg))
print(f"MSG number {msg_number}:\n{msg}\n")
msg_number += 1

elif command == "v":
print("var will be added as extra var")
print("Enter var name: ", end="")
var_name = input()
print("Enter var value: ", end="")
var_value = input()

var_name = get_string_from_input("Enter var name: ")
var_name = var_name.strip(" ")

var_value = get_string_from_input("Enter var value: ")
var_value = var_value.strip(" ")

r.add_var_as_extra_var(var_name, var_value)
print("var added successfully!\n")

elif command == "c":
break

elif command == "p":
print()
play_name = r.get_cur_play_name()
next_task = r.get_next_task_name()
r.progress_bar.print_bar(play_name, next_task)
# elif command == "p":
# print()
# play_name = r.get_cur_play_name()
# next_task = r.get_next_task_name()
# r.progress_bar.print_bar(play_name, next_task)

elif command == "nt":
print("Enter new task like a string entering all \\n and spaces needed:")
new_task_str = input()
new_task_str = get_string_from_input("Enter new task like a string entering all \\n and spaces needed:\n")

# TODO: not sure that this is a good solution
# however, this is interactive mode and
Expand All @@ -101,18 +104,18 @@ def interactive_discotech(failed_task: Task, r: runner):
add_ok, err_msg = r.add_new_task(new_task_str)

if not add_ok:
print("\nThe adding process was failed with the error:\n", err_msg)
print(f"\nThe adding process was failed with the error:\n{err_msg}")
else:
print("\nNew task was added! It will run on every host of current inventory.")
print("Press 'c' after this, not 're'. This will run new task and the failed one after it.\n")
r.progress_bar.add_to_total_task_count(1)

elif command == "w":
print("Enter var name: ", end="")

var_name = input()
var_name = get_string_from_input("Enter var name: ")
var_name = var_name.strip(" ")

value = r.get_variable(var_name)
msg = "{} var value:\n{}".format(var_name, value)
msg = f"{var_name} var value:\n{value}"

print(msg)

Expand Down
10 changes: 9 additions & 1 deletion src/cotea/progress_bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ def __init__(self):
self.bar_len = 100
self.bar_sym = u"█"
self.bar_empty_sym = "."
self.total_task_count = -1


def set_total_task_count(self, tasks_count):
Expand All @@ -18,7 +19,14 @@ def add_to_total_task_count(self, num):


def print_bar(self, play_name, task_name):
percent = self.executed_count / self.total_task_count
if self.total_task_count <= 0:
return

try:
percent = self.executed_count / self.total_task_count
except ZeroDivisionError as e:
return

divisions = int(percent * self.bar_len)
if divisions > self.bar_len:
divisions = self.bar_len
Expand Down
15 changes: 14 additions & 1 deletion src/cotea/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,17 @@ def obj_has_attrs(obj, attrs_list):
error_msg = "Obj {} doesn't have attr {} but should"
return False, error_msg.format(obj, attr)

return True, ""
return True, ""

def get_string_from_input(hint_string):
s = ""

while True:
try:
s = input(hint_string)
break
except Exception as e:
msg = f"While entering a command an exeption raised:\n\n{str(e)}\nTry one more time\n"
print(msg)

return s

0 comments on commit bb44893

Please sign in to comment.