utils.py
Go to the documentation of this file.
1 from __future__ import annotations
2 from enum import Enum
3 
4 
5 import sys
6 from typing import Any
7 import yaml
8 
9 from whichcraft import which
10 
11 
12 class CSIColor(str, Enum):
13  OKGREEN = "\033[92m"
14  WARNING = "\033[93m"
15  FAIL = "\033[91m"
16  ENDC = "\033[0m"
17 
18 
19 def is_tool(name: str) -> bool:
20  """!
21  Check whether an executable exists on PATH.
22  @param name Name of the executable
23  @return True if executable exists, False otherwise
24  """
25  return which(name) is not None
26 
27 
28 def write_flush(msg: str):
29  """!Write a message to standard output and flush the buffer"""
30  sys.stdout.write(msg)
31  sys.stdout.flush()
32 
33 
34 def query_yes_no(question: str, default: str = "yes") -> bool:
35  """!
36  Ask a yes/no question via input() and return their answer.
37  @param question The question that is presented to the user.
38  @param default The presumed answer if the user just hits <Enter>.
39  It must be "yes" (the default), "no" or None (meaning
40  an answer is required of the user).
41  @return True if the answer is "yes", False otherwise
42  """
43  valid = {"yes": True, "y": True, "ye": True, "no": False, "n": False}
44  if default is None:
45  prompt = " [y/n] "
46  elif default == "yes":
47  prompt = " [Y/n] "
48  elif default == "no":
49  prompt = " [y/N] "
50  else:
51  raise ValueError(f"invalid default answer: '{default}'")
52 
53  while True:
54  write_flush(question + prompt)
55  choice = input().lower()
56  if default is not None and choice == "":
57  return valid[default]
58  if choice in valid:
59  return valid[choice]
60  print("Please respond with 'yes' or 'no' " "(or 'y' or 'n').")
61 
62 
63 def prompt_options(options: list[tuple[str, Any]], default: int = 1) -> str:
64  for i, (name, _) in enumerate(options):
65  print(f"{i+1}) {name}")
66 
67  while True:
68  input_raw = input(f"Your selection [{default}]: ")
69  if input_raw == "":
70  selected_nr = default - 1
71  else:
72  selected_nr = int(input_raw) - 1
73  if 0 <= selected_nr < len(options):
74  _, selected = options[selected_nr]
75  return selected
76  print(f"Please select a valid option")
77 
78 
79 def parse_yaml(file_path: str):
80  with open(file_path, "r") as stream:
81  try:
82  return yaml.safe_load(stream)
83  except yaml.YAMLError as exc:
84  print(exc)
85 
86  return {}
leo_fw.utils.query_yes_no
bool query_yes_no(str question, str default="yes")
Ask a yes/no question via input() and return their answer.
Definition: utils.py:34
leo_fw.utils.is_tool
bool is_tool(str name)
Check whether an executable exists on PATH.
Definition: utils.py:19
leo_fw.utils.CSIColor
Definition: utils.py:12
leo_fw.utils.prompt_options
str prompt_options(list[tuple[str, Any]] options, int default=1)
Definition: utils.py:63
leo_fw.utils.write_flush
def write_flush(str msg)
Write a message to standard output and flush the buffer.
Definition: utils.py:28
leo_fw.utils.parse_yaml
def parse_yaml(str file_path)
Definition: utils.py:79


leo_fw
Author(s): Błażej Sowa , Aleksander Szymański
autogenerated on Sat Jul 6 2024 03:05:11