Files
OpenBB/openbb_terminal/core/session/session_model.py
LOne2three 1f0c3ff4de Hotfix/routine default file overwrite (#4927)
* bump pywry

* fix windows pyinstaller subprocess fail if space in path

* remove API_IEX_TOKEN

* remove API_SENTIMENTINVESTOR_TOKEN

* added logic to download_routines and save_routine to put default and personal routines in difference folders and changed logic in call_dowload in account controller to account for the change in logic for save_routine

* Changed logic in read_routine function to account for the new directory structure and also changed the corresponding unit tests. Slightly amended test_save_routine to account for the new routine param

* Changed the routine parameter of mock_save_routine.assert_called_once_with to account for the change in the input parameter of the save_routine function

* changed routine string in test_routines_handler

* Made unit tests for download_routines in routines_handler.

* added comments

* Added new logic to call_exe so that a user could execute the file with --file default/filename.openbb, --file personal/filename.openbb or --file Users/Username/openBBUserData/Routines/hub/default/filename.openbb

* linting: black

* linting: ruff

* ruff broke black

* removed regex in call_exe and changed logic in call_download to give personal routines priority

* Changed formatting

* Changed formatting and removed regex from call_exe

* Created class variables and moved dictionaries out of constructor to solve linting issue.
Changed routine args type annotation
Changed data list

* Added type annotations for the dictionary class variables

* Changed data annotation type for routine dictionaries

* Moved dictionaries back into constructor

* removed import pathlib

* ruff

* mypy

* pylint

---------

Co-authored-by: James Maslek <jmaslek11@gmail.com>
Co-authored-by: teh_coderer <me@tehcoderer.com>
Co-authored-by: Diogo Sousa <montezdesousa@gmail.com>
Co-authored-by: montezdesousa <79287829+montezdesousa@users.noreply.github.com>
2023-05-05 13:12:45 +00:00

219 lines
6.2 KiB
Python

import json
import sys
from enum import Enum
from typing import Any, Dict, Optional
import matplotlib.pyplot as plt
import openbb_terminal.core.session.hub_model as Hub
import openbb_terminal.core.session.local_model as Local
from openbb_terminal.base_helpers import (
remove_log_handlers,
)
from openbb_terminal.core.config.paths import HIST_FILE_PATH, SESSION_FILE_PATH
from openbb_terminal.core.models.user_model import (
ProfileModel,
SourcesModel,
UserModel,
)
from openbb_terminal.core.session.current_user import (
get_current_user,
set_current_user,
set_default_user,
)
from openbb_terminal.core.session.routines_handler import (
download_routines,
save_routine,
)
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
# pylint: disable=consider-using-f-string
class LoginStatus(Enum):
SUCCESS = "success"
FAILED = "failed"
NO_RESPONSE = "no_response"
UNAUTHORIZED = "unauthorized"
def create_session(email: str, password: str, save: bool) -> Dict[Any, Any]:
"""Create a session.
Parameters
----------
email : str
The email.
password : str
The password.
save : bool
Save the session.
"""
session = Hub.get_session(email, password)
if session and save:
Local.save_session(session)
return session
def create_session_from_token(token: str, save: bool) -> Dict[Any, Any]:
"""Create a session from token.
Parameters
----------
token : str
The token.
save : bool
Save the session.
"""
session = Hub.get_session_from_token(token)
if session and save:
Local.save_session(session)
return session
def login(session: Dict, remember: bool = False) -> LoginStatus:
"""Login and load user info.
Parameters
----------
session : dict
The session info.
remember : bool, optional
Remember the session, by default False
"""
# Create a new user:
# credentials: stored in hub, but we fallback to local (.env)
# profile: stored in hub, so we set default here
# preferences: stored locally, so we use the current user preferences
# sources: stored in hub, so we set default here
hub_user = UserModel( # type: ignore
credentials=get_current_user().credentials,
profile=ProfileModel(),
preferences=get_current_user().preferences,
sources=SourcesModel(),
)
response = Hub.fetch_user_configs(session)
if response is not None:
if response.status_code == 200:
configs = json.loads(response.content)
email = configs.get("email", "")
hub_user.profile.load_user_info(session, email, remember)
set_current_user(hub_user)
auth_header = hub_user.profile.get_auth_header()
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)
return LoginStatus.SUCCESS
if response.status_code == 401:
return LoginStatus.UNAUTHORIZED
return LoginStatus.FAILED
return LoginStatus.NO_RESPONSE
def download_and_save_routines(auth_header: str):
"""Download and save routines.
Parameters
----------
auth_header : str
The authorization header, e.g. "Bearer <token>".
"""
routines = download_routines(auth_header=auth_header)
personal_routines_dict = routines[0]
default_routines_dict = routines[1]
try:
for name, content in personal_routines_dict.items():
save_routine(file_name=f"{name}.openbb", routine=content, force=True)
for name, content in default_routines_dict.items():
save_routine(file_name=f"{name}.openbb", routine=content, force=True)
except Exception:
console.print("[red]\nFailed to save routines.[/red]")
def update_backend_sources(auth_header, configs, silent: bool = True):
"""Update backend sources if new source or command path available.
Parameters
----------
auth_header : str
The authorization header, e.g. "Bearer <token>".
configs : Dict
Dictionary with configs
silent : bool
Whether to silence the console output, by default True
"""
console_print = console.print if not silent else lambda *args, **kwargs: None
try:
updated_sources = get_updated_hub_sources(configs)
if updated_sources:
Hub.upload_user_field(
key="features_sources",
value=updated_sources,
auth_header=auth_header,
silent=silent,
)
except Exception:
console_print("[red]Failed to update backend sources.[/red]")
def logout(
auth_header: Optional[str] = None,
token: Optional[str] = None,
cls: bool = False,
):
"""Logout and clear session.
Parameters
----------
auth_header : str, optional
The authorization header, e.g. "Bearer <token>".
token : str, optional
The token to delete.
In the terminal we want to delete the current session, so we use the user own token.
cls : bool
Clear the screen.
"""
if cls:
system_clear()
if not auth_header or not token:
return
success = True
r = Hub.delete_session(auth_header, token)
if not r or r.status_code != 200:
success = False
if not Local.remove(SESSION_FILE_PATH):
success = False
if not Local.remove(HIST_FILE_PATH):
success = False
if not Local.remove(get_current_user().preferences.USER_ROUTINES_DIRECTORY / "hub"):
success = False
remove_log_handlers()
set_default_user()
setup_logging()
plt.close("all")
if success:
console.print("[green]\nLogout successful.[/green]")