mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-10-27 12:40:35 +00:00
Sync with hub on reset + routines fixes (#4842)
* keep session alive on reset * remove print * silence routines * ruff * skip login if is papermill * ruff * move is_papermill to avoid prompt_toolkit warning fd is not a terminal * move is_papermill back and check for atty instead * only download routines and update sources if terminal * only starts prompt toolkit if isatty * black * avoid duplicate files after reset/login * remove hub routines on reset * comment * fix unittest * forgot this debugging prints
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
# IMPORTATION STANDARD
|
||||
import logging
|
||||
|
||||
from openbb_terminal.base_helpers import remove_log_handlers
|
||||
from openbb_terminal.core.log.generation import settings_logger
|
||||
from openbb_terminal.core.log.generation.common import do_rollover
|
||||
|
||||
@@ -14,6 +15,9 @@ logger = logging.getLogger(__name__)
|
||||
def log_terminal(test_mode: bool):
|
||||
"""Logs for the terminal"""
|
||||
|
||||
do_rollover()
|
||||
remove_log_handlers()
|
||||
|
||||
if not test_mode:
|
||||
setup_logging()
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from typing import Tuple
|
||||
from typing import Dict, Optional, Tuple
|
||||
|
||||
from prompt_toolkit import PromptSession
|
||||
|
||||
@@ -106,9 +106,9 @@ def login_and_launch(session: dict, remember: bool = False):
|
||||
prompt(welcome=True)
|
||||
|
||||
|
||||
def main():
|
||||
def main(session: Optional[Dict] = None):
|
||||
"""Main function"""
|
||||
local_session = Local.get_session()
|
||||
local_session = Local.get_session() if session is None else session
|
||||
if not local_session:
|
||||
prompt()
|
||||
else:
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import json
|
||||
import sys
|
||||
from enum import Enum
|
||||
from typing import Any, Dict, Optional
|
||||
|
||||
@@ -28,7 +29,7 @@ from openbb_terminal.core.session.sources_handler import get_updated_hub_sources
|
||||
from openbb_terminal.core.session.utils import run_thread
|
||||
from openbb_terminal.helper_funcs import system_clear
|
||||
from openbb_terminal.loggers import setup_logging
|
||||
from openbb_terminal.rich_config import console, optional_rich_track
|
||||
from openbb_terminal.rich_config import console
|
||||
|
||||
# pylint: disable=consider-using-f-string
|
||||
|
||||
@@ -107,10 +108,12 @@ def login(session: Dict, remember: bool = False) -> LoginStatus:
|
||||
set_current_user(hub_user)
|
||||
|
||||
auth_header = hub_user.profile.get_auth_header()
|
||||
download_and_save_routines(auth_header)
|
||||
run_thread(
|
||||
update_backend_sources, {"auth_header": auth_header, "configs": configs}
|
||||
)
|
||||
if sys.stdin.isatty():
|
||||
download_and_save_routines(auth_header)
|
||||
run_thread(
|
||||
update_backend_sources,
|
||||
{"auth_header": auth_header, "configs": configs},
|
||||
)
|
||||
Local.apply_configs(configs)
|
||||
Local.update_flair(get_current_user().profile.username)
|
||||
|
||||
@@ -130,11 +133,8 @@ def download_and_save_routines(auth_header: str):
|
||||
The authorization header, e.g. "Bearer <token>".
|
||||
"""
|
||||
try:
|
||||
console.print("\nDownloading routines...")
|
||||
routines = download_routines(auth_header=auth_header)
|
||||
for name, content in optional_rich_track(
|
||||
routines.items(), desc="Saving routines"
|
||||
):
|
||||
for name, content in routines.items():
|
||||
save_routine(file_name=f"{name}.openbb", routine=content, force=True)
|
||||
except Exception:
|
||||
console.print("[red]Failed to download and save routines.[/red]")
|
||||
|
||||
@@ -49,7 +49,7 @@ def inputhook(inputhook_context):
|
||||
|
||||
|
||||
try:
|
||||
if not is_jupyter() and not is_papermill():
|
||||
if sys.stdin.isatty():
|
||||
session: Optional[PromptSession] = PromptSession(
|
||||
history=FileHistory(str(HIST_FILE_PATH))
|
||||
)
|
||||
|
||||
@@ -21,9 +21,8 @@ from prompt_toolkit.formatted_text import HTML
|
||||
from prompt_toolkit.styles import Style
|
||||
from rich.markdown import Markdown
|
||||
|
||||
import openbb_terminal.core.session.local_model as Local
|
||||
|
||||
# IMPORTS INTERNAL
|
||||
import openbb_terminal.core.session.local_model as Local
|
||||
from openbb_terminal.core.completer.choices import build_controller_choice_map
|
||||
from openbb_terminal.core.config.paths import HIST_FILE_PATH
|
||||
from openbb_terminal.core.session.current_user import get_current_user, is_local
|
||||
|
||||
@@ -10,18 +10,18 @@ import webbrowser
|
||||
|
||||
# IMPORTATION STANDARD
|
||||
from contextlib import contextmanager
|
||||
from typing import List, Optional
|
||||
from typing import Dict, List, Optional
|
||||
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
# IMPORTATION THIRDPARTY
|
||||
from packaging import version
|
||||
|
||||
# IMPORTATION INTERNAL
|
||||
import openbb_terminal.core.session.local_model as Local
|
||||
from openbb_terminal import thought_of_the_day as thought
|
||||
from openbb_terminal.base_helpers import load_env_files
|
||||
|
||||
# IMPORTATION INTERNAL
|
||||
from openbb_terminal.core.config.paths import SETTINGS_ENV_FILE
|
||||
from openbb_terminal.core.config.paths import HIST_FILE_PATH, SETTINGS_ENV_FILE
|
||||
from openbb_terminal.core.plots.backend import plots_backend
|
||||
from openbb_terminal.core.session.constants import REGISTER_URL
|
||||
from openbb_terminal.core.session.current_system import get_current_system
|
||||
@@ -370,20 +370,37 @@ def reset(queue: Optional[List[str]] = None):
|
||||
debug = get_current_system().DEBUG_MODE
|
||||
load_env_files()
|
||||
|
||||
# we clear all openbb_terminal modules from sys.modules
|
||||
try:
|
||||
# save the current user
|
||||
user_profile = get_current_user().profile
|
||||
session: Dict[str, str] = {
|
||||
"access_token": user_profile.token,
|
||||
"token_type": user_profile.token_type,
|
||||
"uuid": user_profile.uuid,
|
||||
}
|
||||
|
||||
# remove the hub routines
|
||||
if not is_local():
|
||||
Local.remove(get_current_user().preferences.USER_ROUTINES_DIRECTORY / "hub")
|
||||
if not get_current_user().profile.remember:
|
||||
Local.remove(HIST_FILE_PATH)
|
||||
|
||||
# we clear all openbb_terminal modules from sys.modules
|
||||
for module in list(sys.modules.keys()):
|
||||
parts = module.split(".")
|
||||
if parts[0] == "openbb_terminal":
|
||||
if len(parts) > 2 and parts[1] == "core" and parts[2] == "session":
|
||||
continue
|
||||
del sys.modules[module]
|
||||
|
||||
# pylint: disable=import-outside-toplevel
|
||||
# we run the terminal again
|
||||
from openbb_terminal.terminal_controller import main
|
||||
if is_local():
|
||||
from openbb_terminal.terminal_controller import main
|
||||
|
||||
main(debug, ["/".join(queue) if len(queue) > 0 else ""], module="") # type: ignore
|
||||
main(debug, ["/".join(queue) if len(queue) > 0 else ""], module="") # type: ignore
|
||||
else:
|
||||
from openbb_terminal.core.session import session_controller
|
||||
|
||||
session_controller.main(session)
|
||||
|
||||
except Exception as e:
|
||||
logger.exception("Exception: %s", str(e))
|
||||
|
||||
@@ -5,7 +5,6 @@ import openbb_terminal.config_terminal as cfg
|
||||
|
||||
# pylint:disable=unused-import,import-outside-toplevel
|
||||
import openbb_terminal.core.session.current_system as syst # noqa: F401
|
||||
import openbb_terminal.core.session.current_user as user # noqa: F401
|
||||
from openbb_terminal.terminal_helper import (
|
||||
hide_splashscreen,
|
||||
is_auth_enabled,
|
||||
@@ -30,7 +29,11 @@ def main():
|
||||
else:
|
||||
from openbb_terminal.core.session import session_controller
|
||||
|
||||
if is_auth_enabled() and ("--login" in sys.argv[1:] or is_installer()):
|
||||
if (
|
||||
is_auth_enabled()
|
||||
and ("--login" in sys.argv[1:] or is_installer())
|
||||
and sys.stdin.isatty()
|
||||
):
|
||||
session_controller.main()
|
||||
else:
|
||||
session_controller.launch_terminal()
|
||||
|
||||
@@ -107,9 +107,8 @@ def test_login_success_response(mocker):
|
||||
mock_fetch_user_configs = mocker.patch(path + "Hub.fetch_user_configs")
|
||||
mock_apply_configs = mocker.patch(path + "Local.apply_configs")
|
||||
mock_update_flair = mocker.patch(path + "Local.update_flair")
|
||||
mock_get_updated_hub_sources = mocker.patch(
|
||||
path + "get_updated_hub_sources", return_value={}
|
||||
)
|
||||
mocker.patch(path + "download_and_save_routines")
|
||||
mocker.patch(path + "run_thread")
|
||||
|
||||
response = Response()
|
||||
response.status_code = 200
|
||||
@@ -123,7 +122,6 @@ def test_login_success_response(mocker):
|
||||
mock_fetch_user_configs.assert_called_once_with(TEST_SESSION)
|
||||
mock_apply_configs.assert_called_once()
|
||||
mock_update_flair.assert_called_once()
|
||||
mock_get_updated_hub_sources.assert_called_once()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
|
||||
Reference in New Issue
Block a user