From e62bfb118f10cca5ee0f6cbdeee2f36ffe061c23 Mon Sep 17 00:00:00 2001 From: Diogo Sousa Date: Wed, 1 Feb 2023 19:09:47 +0000 Subject: [PATCH] refactor timezone setting --- openbb_terminal/feature_flags.py | 9 ++++-- openbb_terminal/helper_funcs.py | 32 +--------------------- openbb_terminal/settings_controller.py | 38 ++++++++++++++++++++------ 3 files changed, 37 insertions(+), 42 deletions(-) diff --git a/openbb_terminal/feature_flags.py b/openbb_terminal/feature_flags.py index 22337629c84..4b78b32a78f 100644 --- a/openbb_terminal/feature_flags.py +++ b/openbb_terminal/feature_flags.py @@ -47,9 +47,6 @@ USE_CLEAR_AFTER_CMD = load_env_vars( # Use coloring features USE_COLOR = 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 = load_env_vars("OPENBB_USE_DATETIME", strtobool, True, "featflags") @@ -132,6 +129,12 @@ PREFERRED_DATA_SOURCE_FILE = str( ) ) +# Timezone +TIMEZONE = str(os.getenv("OPENBB_TIMEZONE", "America/New_York")) + +# Select console flair (choose from config_terminal.py list) +USE_FLAIR = str(os.getenv("OPENBB_USE_FLAIR", ":openbb")) + # Guess file GUESS_EASTER_EGG_FILE = str( os.getenv( diff --git a/openbb_terminal/helper_funcs.py b/openbb_terminal/helper_funcs.py index aac76938bb6..d21ae26e2dd 100644 --- a/openbb_terminal/helper_funcs.py +++ b/openbb_terminal/helper_funcs.py @@ -44,9 +44,7 @@ from openbb_terminal import config_terminal as cfg from openbb_terminal import config_plot as cfgPlot from openbb_terminal.core.config.paths import ( HOME_DIRECTORY, - USER_ENV_FILE, USER_EXPORTS_DIRECTORY, - load_dotenv_with_priority, ) from openbb_terminal.core.config import paths @@ -1104,7 +1102,6 @@ def get_flair() -> str: else str(obbff.USE_FLAIR) ) - set_default_timezone() if obbff.USE_DATETIME and get_user_timezone_or_invalid() != "INVALID": dtime = datetime.now(pytz.timezone(get_user_timezone())).strftime( "%Y %b %d, %H:%M" @@ -1119,14 +1116,6 @@ def get_flair() -> str: return flair -def set_default_timezone() -> None: - """Set a default (America/New_York) timezone if one doesn't exist.""" - load_dotenv_with_priority() - user_tz = os.getenv("OPENBB_TIMEZONE") - if not user_tz: - dotenv.set_key(USER_ENV_FILE, "OPENBB_TIMEZONE", "America/New_York") - - def is_timezone_valid(user_tz: str) -> bool: """Check whether user timezone is valid. @@ -1151,11 +1140,7 @@ def get_user_timezone() -> str: str user timezone based on .env file """ - load_dotenv_with_priority() - user_tz = os.getenv("OPENBB_TIMEZONE") - if user_tz: - return user_tz - return "" + return obbff.TIMEZONE def get_user_timezone_or_invalid() -> str: @@ -1172,21 +1157,6 @@ def get_user_timezone_or_invalid() -> str: return "INVALID" -def replace_user_timezone(user_tz: str) -> None: - """Replace user timezone. - - Parameters - ---------- - user_tz: str - User timezone to set - """ - if is_timezone_valid(user_tz): - dotenv.set_key(USER_ENV_FILE, "OPENBB_TIMEZONE", user_tz) - console.print("Timezone successfully updated", "\n") - else: - console.print("Timezone selected is not valid", "\n") - - def str_to_bool(value) -> bool: """Match a string to a boolean value.""" if isinstance(value, bool): diff --git a/openbb_terminal/settings_controller.py b/openbb_terminal/settings_controller.py index 580714df4bb..d8c1459aa83 100644 --- a/openbb_terminal/settings_controller.py +++ b/openbb_terminal/settings_controller.py @@ -27,7 +27,7 @@ from openbb_terminal.decorators import log_start_end from openbb_terminal.helper_funcs import ( get_flair, get_user_timezone_or_invalid, - replace_user_timezone, + parse_and_split_input, set_user_data_folder, ) from openbb_terminal.menu import session @@ -65,8 +65,6 @@ class SettingsController(BaseController): PATH = "/settings/" CHOICES_GENERATION = True - all_timezones = [tz.replace("/", "_") for tz in pytz.all_timezones] - languages_available = [ lang.strip(".yml") for lang in os.listdir(obbff.i18n_dict_location) @@ -80,11 +78,33 @@ class SettingsController(BaseController): if session and obbff.USE_PROMPT_TOOLKIT: choices: dict = self.choices_default - choices["tz"] = {c: None for c in self.all_timezones} + choices["tz"] = {c: None for c in pytz.all_timezones} choices["lang"] = {c: None for c in self.languages_available} self.choices = choices self.completer = NestedCompleter.from_nested_dict(choices) + self.sort_filter = r"((tz\ -t |tz ).*?(" + for tz in pytz.all_timezones: + tz = tz.replace("/", r"\/") + self.sort_filter += f"{tz}|" + self.sort_filter += ")*)" + + def parse_input(self, an_input: str) -> List: + """Parse controller input + + Overrides the parent class function to handle github org/repo path convention. + See `BaseController.parse_input()` for details. + """ + # Filtering out + sort_filter = self.sort_filter + + custom_filters = [sort_filter] + + commands = parse_and_split_input( + an_input=an_input, custom_filters=custom_filters + ) + return commands + def print_help(self): """Print help""" mt = MenuText("settings/") @@ -447,16 +467,18 @@ class SettingsController(BaseController): dest="timezone", help="Choose timezone", required="-h" not in other_args, - choices=self.all_timezones, + metavar="TIMEZONE", + choices=pytz.all_timezones, ) if other_args and "-t" not in other_args[0]: other_args.insert(0, "-t") ns_parser = self.parse_simple_args(parser, other_args) - if ns_parser: - if ns_parser.timezone: - replace_user_timezone(ns_parser.timezone.replace("_", "/", 1)) + if ns_parser and ns_parser.timezone: + obbff_ctrl.FeatureFlagsController.set_feature_flag( + "OPENBB_TIMEZONE", ns_parser.timezone + ) @log_start_end(log=logger) def call_flair(self, other_args: List[str]):