From 309b2b8399054101c42449ea4e8710cbf06032a7 Mon Sep 17 00:00:00 2001 From: colin99d Date: Fri, 14 Oct 2022 19:19:53 +0100 Subject: [PATCH 1/3] Loads env file after creating a new one --- openbb_terminal/settings_controller.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/openbb_terminal/settings_controller.py b/openbb_terminal/settings_controller.py index ee59906c5ad0..7bf7e70b8c4c 100644 --- a/openbb_terminal/settings_controller.py +++ b/openbb_terminal/settings_controller.py @@ -11,7 +11,7 @@ import pytz # IMPORTATION THIRDPARTY -from dotenv import set_key +from dotenv import set_key, load_dotenv # IMPORTATION INTERNAL from openbb_terminal import config_plot as cfg_plot @@ -157,6 +157,7 @@ def call_dt(self, _): obbff.USE_DATETIME = not obbff.USE_DATETIME set_key(obbff.USER_ENV_FILE, "OPENBB_USE_DATETIME", str(obbff.USE_DATETIME)) console.print("") + load_dotenv(USER_ENV_FILE) @log_start_end(log=logger) def call_source(self, other_args: List[str]): @@ -197,6 +198,7 @@ def call_source(self, other_args: List[str]): str(ns_parser.value), ) console.print("") + load_dotenv(USER_ENV_FILE) @log_start_end(log=logger) def call_autoscaling(self, _): @@ -208,6 +210,7 @@ def call_autoscaling(self, _): str(obbff.USE_PLOT_AUTOSCALING), ) console.print("") + load_dotenv(USER_ENV_FILE) @log_start_end(log=logger) def call_dpi(self, other_args: List[str]): @@ -233,6 +236,7 @@ def call_dpi(self, other_args: List[str]): set_key(obbff.USER_ENV_FILE, "OPENBB_PLOT_DPI", str(ns_parser.value)) cfg_plot.PLOT_DPI = ns_parser.value console.print("") + load_dotenv(USER_ENV_FILE) @log_start_end(log=logger) def call_height(self, other_args: List[str]): @@ -258,6 +262,7 @@ def call_height(self, other_args: List[str]): set_key(obbff.USER_ENV_FILE, "OPENBB_PLOT_HEIGHT", str(ns_parser.value)) cfg_plot.PLOT_HEIGHT = ns_parser.value console.print("") + load_dotenv(USER_ENV_FILE) @log_start_end(log=logger) def call_width(self, other_args: List[str]): @@ -283,6 +288,7 @@ def call_width(self, other_args: List[str]): set_key(obbff.USER_ENV_FILE, "OPENBB_PLOT_WIDTH", str(ns_parser.value)) cfg_plot.PLOT_WIDTH = ns_parser.value console.print("") + load_dotenv(USER_ENV_FILE) @log_start_end(log=logger) def call_pheight(self, other_args: List[str]): @@ -311,6 +317,7 @@ def call_pheight(self, other_args: List[str]): ) cfg_plot.PLOT_HEIGHT_PERCENTAGE = ns_parser.value console.print("") + load_dotenv(USER_ENV_FILE) @log_start_end(log=logger) def call_pwidth(self, other_args: List[str]): @@ -339,6 +346,7 @@ def call_pwidth(self, other_args: List[str]): ) cfg_plot.PLOT_WIDTH_PERCENTAGE = ns_parser.value console.print("") + load_dotenv(USER_ENV_FILE) @log_start_end(log=logger) def call_monitor(self, other_args: List[str]): @@ -363,6 +371,7 @@ def call_monitor(self, other_args: List[str]): set_key(obbff.USER_ENV_FILE, "OPENBB_MONITOR", str(ns_parser.value)) cfg_plot.MONITOR = ns_parser.value console.print("") + load_dotenv(USER_ENV_FILE) @log_start_end(log=logger) def call_backend(self, other_args: List[str]): @@ -390,6 +399,7 @@ def call_backend(self, other_args: List[str]): else: cfg_plot.BACKEND = ns_parser.value console.print("") + load_dotenv(USER_ENV_FILE) @log_start_end(log=logger) def call_lang(self, other_args: List[str]): @@ -423,6 +433,7 @@ def call_lang(self, other_args: List[str]): f"Languages available: {', '.join(self.languages_available)}" ) console.print("") + load_dotenv(USER_ENV_FILE) @log_start_end(log=logger) def call_tz(self, other_args: List[str]): @@ -449,6 +460,7 @@ def call_tz(self, other_args: List[str]): if ns_parser: if ns_parser.timezone: replace_user_timezone(ns_parser.timezone.replace("_", "/", 1)) + load_dotenv(USER_ENV_FILE) @log_start_end(log=logger) def call_flair(self, other_args: List[str]): @@ -478,6 +490,7 @@ def call_flair(self, other_args: List[str]): set_key(obbff.USER_ENV_FILE, "OPENBB_USE_FLAIR", str(ns_parser.emoji)) obbff.USE_FLAIR = ns_parser.emoji console.print("") + load_dotenv(USER_ENV_FILE) @log_start_end(log=logger) def call_userdata(self, other_args: List[str]): @@ -567,3 +580,4 @@ def call_userdata(self, other_args: List[str]): success_userdata = True console.print() + load_dotenv(USER_ENV_FILE) From de7eeb190cbc7686c568bbcb488e72e2e0bee12b Mon Sep 17 00:00:00 2001 From: colin99d Date: Sun, 16 Oct 2022 08:38:57 +0200 Subject: [PATCH 2/3] Continued work --- openbb_terminal/config_plot.py | 33 ++++++++++++++++++++++---- openbb_terminal/helper_funcs.py | 1 + openbb_terminal/settings_controller.py | 16 +------------ 3 files changed, 31 insertions(+), 19 deletions(-) diff --git a/openbb_terminal/config_plot.py b/openbb_terminal/config_plot.py index 3fd5e3ef1f1b..9a96b2c389e4 100644 --- a/openbb_terminal/config_plot.py +++ b/openbb_terminal/config_plot.py @@ -3,6 +3,7 @@ import dotenv from openbb_terminal.core.config.paths import USER_ENV_FILE, REPOSITORY_ENV_FILE +from openbb_terminal.rich_config import console dotenv.load_dotenv(USER_ENV_FILE) dotenv.load_dotenv(REPOSITORY_ENV_FILE, override=True) @@ -21,12 +22,36 @@ # See more: https://matplotlib.org/stable/tutorials/introductory/usage.html#the-builtin-backends # Used when USE_PLOT_AUTOSCALING is set to False -PLOT_HEIGHT = int(os.getenv("OPENBB_PLOT_HEIGHT", "500")) -PLOT_WIDTH = int(os.getenv("OPENBB_PLOT_WIDTH", "800")) +try: + PLOT_HEIGHT = int(os.getenv("OPENBB_PLOT_HEIGHT", "500")) +except ValueError: + PLOT_HEIGHT = 500 + console.print( + "[red] Invalid value for OPENBB_PLOT_HEIGHT. Please use a number.[/red]\n" + ) +try: + PLOT_WIDTH = int(os.getenv("OPENBB_PLOT_WIDTH", "800")) +except ValueError: + PLOT_WIDTH = 800 + console.print( + "[red] Invalid value for OPENBB_PLOT_WIDTH. Please use a number.[/red]\n" + ) # Used when USE_PLOT_AUTOSCALING is set to True -PLOT_HEIGHT_PERCENTAGE = float(os.getenv("OPENBB_PLOT_HEIGHT_PERCENTAGE", "50.00")) -PLOT_WIDTH_PERCENTAGE = float(os.getenv("OPENBB_PLOT_WIDTH_PERCENTAGE", "70.00")) +try: + PLOT_HEIGHT_PERCENTAGE = float(os.getenv("OPENBB_PLOT_HEIGHT_PERCENTAGE", "50.00")) +except ValueError: + PLOT_HEIGHT_PERCENTAGE = 50.00 + console.print( + "[red] Invalid value for OPENBB_PLOT_HEIGHT_PERCENTAGE. Please use a number.[/red]\n" + ) +try: + PLOT_WIDTH_PERCENTAGE = float(os.getenv("OPENBB_PLOT_WIDTH_PERCENTAGE", "70.00")) +except ValueError: + PLOT_WIDTH_PERCENTAGE = 70.00 + console.print( + "[red] Invalid value for OPENBB_PLOT_WIDTH_PERCENTAGE. Please use a number.[/red]\n" + ) # When autoscaling is True, choose which monitor to scale to # Primary monitor = 0, secondary monitor use 1 diff --git a/openbb_terminal/helper_funcs.py b/openbb_terminal/helper_funcs.py index 2352116b5071..e88ec9df08a6 100644 --- a/openbb_terminal/helper_funcs.py +++ b/openbb_terminal/helper_funcs.py @@ -1149,6 +1149,7 @@ def replace_user_timezone(user_tz: str) -> None: """ if is_timezone_valid(user_tz): dotenv.set_key(USER_ENV_FILE, "OPENBB_TIMEZONE", user_tz) + os.environ["OPENBB_TIMEZONE"] = user_tz console.print("Timezone successfully updated", "\n") else: console.print("Timezone selected is not valid", "\n") diff --git a/openbb_terminal/settings_controller.py b/openbb_terminal/settings_controller.py index 7bf7e70b8c4c..ee59906c5ad0 100644 --- a/openbb_terminal/settings_controller.py +++ b/openbb_terminal/settings_controller.py @@ -11,7 +11,7 @@ import pytz # IMPORTATION THIRDPARTY -from dotenv import set_key, load_dotenv +from dotenv import set_key # IMPORTATION INTERNAL from openbb_terminal import config_plot as cfg_plot @@ -157,7 +157,6 @@ def call_dt(self, _): obbff.USE_DATETIME = not obbff.USE_DATETIME set_key(obbff.USER_ENV_FILE, "OPENBB_USE_DATETIME", str(obbff.USE_DATETIME)) console.print("") - load_dotenv(USER_ENV_FILE) @log_start_end(log=logger) def call_source(self, other_args: List[str]): @@ -198,7 +197,6 @@ def call_source(self, other_args: List[str]): str(ns_parser.value), ) console.print("") - load_dotenv(USER_ENV_FILE) @log_start_end(log=logger) def call_autoscaling(self, _): @@ -210,7 +208,6 @@ def call_autoscaling(self, _): str(obbff.USE_PLOT_AUTOSCALING), ) console.print("") - load_dotenv(USER_ENV_FILE) @log_start_end(log=logger) def call_dpi(self, other_args: List[str]): @@ -236,7 +233,6 @@ def call_dpi(self, other_args: List[str]): set_key(obbff.USER_ENV_FILE, "OPENBB_PLOT_DPI", str(ns_parser.value)) cfg_plot.PLOT_DPI = ns_parser.value console.print("") - load_dotenv(USER_ENV_FILE) @log_start_end(log=logger) def call_height(self, other_args: List[str]): @@ -262,7 +258,6 @@ def call_height(self, other_args: List[str]): set_key(obbff.USER_ENV_FILE, "OPENBB_PLOT_HEIGHT", str(ns_parser.value)) cfg_plot.PLOT_HEIGHT = ns_parser.value console.print("") - load_dotenv(USER_ENV_FILE) @log_start_end(log=logger) def call_width(self, other_args: List[str]): @@ -288,7 +283,6 @@ def call_width(self, other_args: List[str]): set_key(obbff.USER_ENV_FILE, "OPENBB_PLOT_WIDTH", str(ns_parser.value)) cfg_plot.PLOT_WIDTH = ns_parser.value console.print("") - load_dotenv(USER_ENV_FILE) @log_start_end(log=logger) def call_pheight(self, other_args: List[str]): @@ -317,7 +311,6 @@ def call_pheight(self, other_args: List[str]): ) cfg_plot.PLOT_HEIGHT_PERCENTAGE = ns_parser.value console.print("") - load_dotenv(USER_ENV_FILE) @log_start_end(log=logger) def call_pwidth(self, other_args: List[str]): @@ -346,7 +339,6 @@ def call_pwidth(self, other_args: List[str]): ) cfg_plot.PLOT_WIDTH_PERCENTAGE = ns_parser.value console.print("") - load_dotenv(USER_ENV_FILE) @log_start_end(log=logger) def call_monitor(self, other_args: List[str]): @@ -371,7 +363,6 @@ def call_monitor(self, other_args: List[str]): set_key(obbff.USER_ENV_FILE, "OPENBB_MONITOR", str(ns_parser.value)) cfg_plot.MONITOR = ns_parser.value console.print("") - load_dotenv(USER_ENV_FILE) @log_start_end(log=logger) def call_backend(self, other_args: List[str]): @@ -399,7 +390,6 @@ def call_backend(self, other_args: List[str]): else: cfg_plot.BACKEND = ns_parser.value console.print("") - load_dotenv(USER_ENV_FILE) @log_start_end(log=logger) def call_lang(self, other_args: List[str]): @@ -433,7 +423,6 @@ def call_lang(self, other_args: List[str]): f"Languages available: {', '.join(self.languages_available)}" ) console.print("") - load_dotenv(USER_ENV_FILE) @log_start_end(log=logger) def call_tz(self, other_args: List[str]): @@ -460,7 +449,6 @@ def call_tz(self, other_args: List[str]): if ns_parser: if ns_parser.timezone: replace_user_timezone(ns_parser.timezone.replace("_", "/", 1)) - load_dotenv(USER_ENV_FILE) @log_start_end(log=logger) def call_flair(self, other_args: List[str]): @@ -490,7 +478,6 @@ def call_flair(self, other_args: List[str]): set_key(obbff.USER_ENV_FILE, "OPENBB_USE_FLAIR", str(ns_parser.emoji)) obbff.USE_FLAIR = ns_parser.emoji console.print("") - load_dotenv(USER_ENV_FILE) @log_start_end(log=logger) def call_userdata(self, other_args: List[str]): @@ -580,4 +567,3 @@ def call_userdata(self, other_args: List[str]): success_userdata = True console.print() - load_dotenv(USER_ENV_FILE) From eed02cb38d513e0caf826cf8630657744978f4fb Mon Sep 17 00:00:00 2001 From: colin99d Date: Mon, 17 Oct 2022 07:38:44 +0200 Subject: [PATCH 3/3] improved error handling --- openbb_terminal/config_plot.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/openbb_terminal/config_plot.py b/openbb_terminal/config_plot.py index 9a96b2c389e4..3edac01f930c 100644 --- a/openbb_terminal/config_plot.py +++ b/openbb_terminal/config_plot.py @@ -8,7 +8,13 @@ dotenv.load_dotenv(USER_ENV_FILE) dotenv.load_dotenv(REPOSITORY_ENV_FILE, override=True) -PLOT_DPI = int(os.getenv("OPENBB_PLOT_DPI", "100")) +try: + PLOT_DPI = int(os.getenv("OPENBB_PLOT_DPI", "100")) +except ValueError: + PLOT_DPI = 100 + console.print( + f"[red]OPENBB_PLOT_DPI is not an integer, using default value of {PLOT_DPI}[/red]" + ) # Backend to use for plotting BACKEND = os.getenv("OPENBB_BACKEND", "None")