From 31fa79146f0cec15bd1b96ef50850027abd9c9c9 Mon Sep 17 00:00:00 2001
From: Savely Krendelhoff <alicecandyom@proton.me>
Date: Wed, 15 Nov 2023 12:30:28 +0300
Subject: [PATCH] [Chore] Abstract coloured print and log pattern

Problem: Lots of times we need to print coloured and log the same
message.

Solution: Add colorcode argument to the `print_and_log` function.
---
 baking/src/tezos_baking/tezos_setup_wizard.py | 46 +++++++++----------
 .../src/tezos_baking/tezos_voting_wizard.py   |  9 ++--
 baking/src/tezos_baking/util.py               |  1 +
 baking/src/tezos_baking/wizard_structure.py   |  6 +--
 4 files changed, 31 insertions(+), 31 deletions(-)

diff --git a/baking/src/tezos_baking/tezos_setup_wizard.py b/baking/src/tezos_baking/tezos_setup_wizard.py
index ce925acd8..ac1ecaacd 100644
--- a/baking/src/tezos_baking/tezos_setup_wizard.py
+++ b/baking/src/tezos_baking/tezos_setup_wizard.py
@@ -600,37 +600,31 @@ def get_snapshot_metadata(self, name, json_url):
             snapshot_metadata = self.extract_relevant_snapshot(snapshot_array)
 
             if snapshot_metadata is None:
-                message = f"No suitable snapshot found from the {name} provider."
-                print(
-                    color(
-                        message,
-                        color_red,
-                    )
+                print_and_log(
+                    f"No suitable snapshot found from the {name} provider.",
+                    log=logging.warning,
+                    colorcode=color_yellow,
                 )
-                logging.warning(message)
             else:
                 self.config["snapshots"][name] = snapshot_metadata
 
         except urllib.error.URLError:
-            message = f"\nCouldn't collect snapshot metadata from {json_url} due to networking issues.\n"
-            print(
-                color(
-                    message,
-                    color_red,
-                )
+            print_and_log(
+                f"\nCouldn't collect snapshot metadata from {json_url} due to networking issues.\n",
+                log=logging.error,
+                colorcode=color_red,
             )
-            logging.error(message)
         except ValueError:
-            message = f"\nCouldn't collect snapshot metadata from {json_url} due to format mismatch.\n"
-            print(
-                color(
-                    message,
-                    color_red,
-                )
+            print_and_log(
+                f"\nCouldn't collect snapshot metadata from {json_url} due to format mismatch.\n",
+                log=logging.error,
+                colorcode=color_red,
             )
-            logging.error(message)
         except Exception as e:
-            print_and_log(f"\nUnexpected error handling snapshot metadata:\n{e}\n")
+            print_and_log(
+                f"\nUnexpected error handling snapshot metadata:\n{e}\n",
+                log=logging.error,
+            )
 
     def output_snapshot_metadata(self, name):
         from datetime import datetime
@@ -1063,9 +1057,11 @@ def main():
                 + ".service"
             )
 
-        message = "Error in the Tezos Setup Wizard, exiting."
-        print("\n" + color(message, color_red))
-        logging.error(message)
+        print_and_log(
+            "Error in the Tezos Setup Wizard, exiting.",
+            log=logging.error,
+            colorcode=color_red,
+        )
 
         log_exception(exception=e, logfile="tezos-setup.log")
 
diff --git a/baking/src/tezos_baking/tezos_voting_wizard.py b/baking/src/tezos_baking/tezos_voting_wizard.py
index 93886723f..86e969187 100644
--- a/baking/src/tezos_baking/tezos_voting_wizard.py
+++ b/baking/src/tezos_baking/tezos_voting_wizard.py
@@ -536,9 +536,12 @@ def main():
         logging.info("Exiting the Tezos Voting Wizard.")
         sys.exit(1)
     except Exception as e:
-        message = "Error in the Tezos Voting Wizard, exiting."
-        print("\n" + color(message, color_red))
-        logging.error(message)
+
+        print_and_log(
+            "Error in the Tezos Voting Wizard, exiting.",
+            log=logging.error,
+            colorcode=color_red,
+        )
 
         log_exception(exception=e, logfile="tezos-vote.log")
 
diff --git a/baking/src/tezos_baking/util.py b/baking/src/tezos_baking/util.py
index 4928cb548..b76620864 100644
--- a/baking/src/tezos_baking/util.py
+++ b/baking/src/tezos_baking/util.py
@@ -108,6 +108,7 @@ def color(input, colorcode):
 
 color_red = "\x1b[1;31m"
 color_green = "\x1b[1;32m"
+color_yellow = "\x1b[1;33m"
 
 
 def yes_or_no(prompt, default=None):
diff --git a/baking/src/tezos_baking/wizard_structure.py b/baking/src/tezos_baking/wizard_structure.py
index c2ea314ed..bdb953deb 100644
--- a/baking/src/tezos_baking/wizard_structure.py
+++ b/baking/src/tezos_baking/wizard_structure.py
@@ -107,9 +107,9 @@ def setup_logger(log_file):
     )
 
 
-def print_and_log(s, log=logging.info):
-    print(s)
-    log(s)
+def print_and_log(message, log=logging.info, colorcode=None):
+    print(color(message, colorcode) if colorcode else message)
+    log(message)
 
 
 def log_exception(exception, logfile):