Adds Verbosity to Error Message Warnings and Fixes my Issue when No Variables Present. (#3287)

* Began update

* Shows more verbose error messages
This commit is contained in:
Colin Delahunty
2022-11-03 17:24:13 +01:00
committed by GitHub
parent 09f753da1c
commit b4d39ef399
3 changed files with 79 additions and 35 deletions

View File

@@ -1,13 +1,44 @@
# This is for helpers that do NOT import any OpenBB Modules
from typing import Callable, Any
from typing import Callable, Any, Literal
import os
from rich.console import Console
console = Console()
menus = Literal["", "featflags", "settings"]
def load_env_vars(name: str, converter: Callable, default: Any) -> Any:
def handle_error(name: str, default: Any, menu: menus = ""):
"""Handles the error by returning the default value and printing an
informative error message.
Parameters
----------
name: str
The name of the environment variable
default: Any
The value to return if the converter fails
menu: menus
If provided, will tell the user where to fix the setting
Returns
----------
Any
The default value
"""
base = f"[red]Invalid variable provided for variable '{name}'."
if menu:
base += f" Please change the setting in the `{menu}` menu."
base += "[/red]\n"
console.print(base)
return default
def load_env_vars(
name: str, converter: Callable, default: Any, menu: menus = ""
) -> Any:
"""Loads an environment variable and attempts to convert it to the correct data type.
Will return the provided default if it fails
@@ -19,21 +50,20 @@ def load_env_vars(name: str, converter: Callable, default: Any) -> Any:
The function to convert the env variable to the desired format
default: Any
The value to return if the converter fails
menu: menus
If provided, will tell the user where to fix the setting
Returns
----------
Any
The value or the default
"""
raw_var = os.getenv(name)
raw_var = os.getenv(name, str(default))
try:
return converter(raw_var)
except ValueError:
console.print(f"[red]Invalid type provided for variable '{name}'.[/red]\n")
return default
return handle_error(name, default, menu)
except AttributeError:
console.print(f"[red]Invalid type provided for variable '{name}'.[/red]\n")
return default
return handle_error(name, default, menu)
except TypeError:
console.print(f"[red]Invalid type provided for variable '{name}'.[/red]\n")
return default
return handle_error(name, default, menu)

View File

@@ -8,7 +8,7 @@ from openbb_terminal import base_helpers
dotenv.load_dotenv(USER_ENV_FILE)
dotenv.load_dotenv(REPOSITORY_ENV_FILE, override=True)
PLOT_DPI = base_helpers.load_env_vars("OPENBB_PLOT_DPI", int, 100)
PLOT_DPI = base_helpers.load_env_vars("OPENBB_PLOT_DPI", int, 100, "settings")
# Backend to use for plotting
BACKEND = os.getenv("OPENBB_BACKEND", "None")
@@ -22,20 +22,20 @@ if BACKEND == "None":
# See more: https://matplotlib.org/stable/tutorials/introductory/usage.html#the-builtin-backends
# Used when USE_PLOT_AUTOSCALING is set to False
PLOT_HEIGHT = base_helpers.load_env_vars("OPENBB_PLOT_HEIGHT", int, 500)
PLOT_WIDTH = base_helpers.load_env_vars("OPENBB_PLOT_WIDTH", int, 800)
PLOT_HEIGHT = base_helpers.load_env_vars("OPENBB_PLOT_HEIGHT", int, 500, "settings")
PLOT_WIDTH = base_helpers.load_env_vars("OPENBB_PLOT_WIDTH", int, 800, "settings")
# Used when USE_PLOT_AUTOSCALING is set to True
PLOT_HEIGHT_PERCENTAGE = base_helpers.load_env_vars(
"OPENBB_PLOT_HEIGHT_PERCENTAGE", float, 50.0
"OPENBB_PLOT_HEIGHT_PERCENTAGE", float, 50.0, "settings"
)
PLOT_WIDTH_PERCENTAGE = base_helpers.load_env_vars(
"OPENBB_PLOT_WIDTH_PERCENTAGE", float, 70.0
"OPENBB_PLOT_WIDTH_PERCENTAGE", float, 70.0, "settings"
)
# When autoscaling is True, choose which monitor to scale to
# Primary monitor = 0, secondary monitor use 1
MONITOR = base_helpers.load_env_vars("OPENBB_MONITOR", int, 0)
MONITOR = base_helpers.load_env_vars("OPENBB_MONITOR", int, 0, "settings")
# Color for `view` command data. All pyplot colors listed at:
# https://matplotlib.org/stable/gallery/color/named_colors.html

View File

@@ -28,92 +28,106 @@ load_dotenv(REPOSITORY_ENV_FILE, override=True)
load_dotenv(PACKAGE_ENV_FILE, override=True)
# Retry unknown commands with `load`
RETRY_WITH_LOAD = base_helpers.load_env_vars("OPENBB_RETRY_WITH_LOAD", strtobool, False)
RETRY_WITH_LOAD = base_helpers.load_env_vars(
"OPENBB_RETRY_WITH_LOAD", strtobool, False, "featflags"
)
# Use tabulate to print dataframes
USE_TABULATE_DF = base_helpers.load_env_vars("OPENBB_USE_TABULATE_DF", strtobool, True)
USE_TABULATE_DF = base_helpers.load_env_vars(
"OPENBB_USE_TABULATE_DF", strtobool, True, "featflags"
)
# Use clear console after each command
USE_CLEAR_AFTER_CMD = base_helpers.load_env_vars(
"OPENBB_USE_CLEAR_AFTER_CMD", strtobool, False
"OPENBB_USE_CLEAR_AFTER_CMD", strtobool, False, "featflags"
)
# Use coloring features
USE_COLOR = base_helpers.load_env_vars("OPENBB_USE_COLOR", strtobool, True)
USE_COLOR = base_helpers.load_env_vars("OPENBB_USE_COLOR", strtobool, True, "featflags")
# Select console flair (choose from config_terminal.py list)
USE_FLAIR = str(os.getenv("OPENBB_USE_FLAIR", ":openbb"))
# Add date and time to command line
USE_DATETIME = base_helpers.load_env_vars("OPENBB_USE_DATETIME", strtobool, True)
USE_DATETIME = base_helpers.load_env_vars(
"OPENBB_USE_DATETIME", strtobool, True, "featflags"
)
# Enable interactive matplotlib mode
USE_ION = base_helpers.load_env_vars("OPENBB_USE_ION", strtobool, True)
USE_ION = base_helpers.load_env_vars("OPENBB_USE_ION", strtobool, True, "featflags")
# Enable watermark in the figures
USE_WATERMARK = base_helpers.load_env_vars("OPENBB_USE_WATERMARK", strtobool, True)
USE_WATERMARK = base_helpers.load_env_vars(
"OPENBB_USE_WATERMARK", strtobool, True, "featflags"
)
# Enable command and source in the figures
USE_CMD_LOCATION_FIGURE = base_helpers.load_env_vars(
"OPENBB_USE_CMD_LOCATION_FIGURE", strtobool, True
"OPENBB_USE_CMD_LOCATION_FIGURE", strtobool, True, "featflags"
)
# Enable Prompt Toolkit
USE_PROMPT_TOOLKIT = base_helpers.load_env_vars(
"OPENBB_USE_PROMPT_TOOLKIT", strtobool, True
"OPENBB_USE_PROMPT_TOOLKIT", strtobool, True, "featflags"
)
# Enable plot autoscaling
USE_PLOT_AUTOSCALING = base_helpers.load_env_vars(
"OPENBB_USE_PLOT_AUTOSCALING", strtobool, False
"OPENBB_USE_PLOT_AUTOSCALING", strtobool, False, "featflags"
)
# Enable thoughts of the day
ENABLE_THOUGHTS_DAY = base_helpers.load_env_vars(
"OPENBB_ENABLE_THOUGHTS_DAY", strtobool, False
"OPENBB_ENABLE_THOUGHTS_DAY", strtobool, False, "featflags"
)
# Quick exit for testing
ENABLE_QUICK_EXIT = base_helpers.load_env_vars(
"OPENBB_ENABLE_QUICK_EXIT", strtobool, False
"OPENBB_ENABLE_QUICK_EXIT", strtobool, False, "featflags"
)
# Open report as HTML, otherwise notebook
OPEN_REPORT_AS_HTML = base_helpers.load_env_vars(
"OPENBB_OPEN_REPORT_AS_HTML", strtobool, True
"OPENBB_OPEN_REPORT_AS_HTML", strtobool, True, "featflags"
)
# Enable auto print_help when exiting menus
ENABLE_EXIT_AUTO_HELP = base_helpers.load_env_vars(
"OPENBB_ENABLE_EXIT_AUTO_HELP", strtobool, True
"OPENBB_ENABLE_EXIT_AUTO_HELP", strtobool, True, "featflags"
)
# Remember contexts during session
REMEMBER_CONTEXTS = base_helpers.load_env_vars(
"OPENBB_REMEMBER_CONTEXTS", strtobool, True
"OPENBB_REMEMBER_CONTEXTS", strtobool, True, "featflags"
)
# Use the colorful rich terminal
ENABLE_RICH = base_helpers.load_env_vars("OPENBB_ENABLE_RICH", strtobool, True)
ENABLE_RICH = base_helpers.load_env_vars(
"OPENBB_ENABLE_RICH", strtobool, True, "featflags"
)
# Use the colorful rich terminal
ENABLE_RICH_PANEL = base_helpers.load_env_vars(
"OPENBB_ENABLE_RICH_PANEL", strtobool, True
"OPENBB_ENABLE_RICH_PANEL", strtobool, True, "featflags"
)
# Check API KEYS before running a command
ENABLE_CHECK_API = base_helpers.load_env_vars(
"OPENBB_ENABLE_CHECK_API", strtobool, True
"OPENBB_ENABLE_CHECK_API", strtobool, True, "featflags"
)
# Send logs to data lake
LOG_COLLECTION = base_helpers.load_env_vars("OPENBB_LOG_COLLECT", strtobool, True)
LOG_COLLECTION = base_helpers.load_env_vars(
"OPENBB_LOG_COLLECT", strtobool, True, "featflags"
)
# Provide export folder path. If empty that means default.
EXPORT_FOLDER_PATH = str(os.getenv("OPENBB_EXPORT_FOLDER_PATH", ""))
# Toolbar hint
TOOLBAR_HINT = base_helpers.load_env_vars("OPENBB_TOOLBAR_HINT", strtobool, True)
TOOLBAR_HINT = base_helpers.load_env_vars(
"OPENBB_TOOLBAR_HINT", strtobool, True, "featflags"
)
# Select language to be used
USE_LANGUAGE = str(os.getenv("OPENBB_USE_LANGUAGE", "en"))