Files
OpenBB/openbb_terminal/core/session/session_controller.py
montezdesousa 475ebb5acd [WIP] Migrate user, feature flags and configurations to dataclass (#4309)
* implement user and change is guest

* replace User methods

* move remaining functions to user

* fix bug

* portfolio test

* fix tests

* fix tests

* uncomment skip

* ruff

* start credentials model implementation

* change deault structure

* refacto quandl

* change default struct and file names

* bug

* start using global user

* add comments

* move update flair

* change cfg references in keys model

* reddit & fmp

* refactore additional keys

* remaing config keys

* remove unsude imports and fix hub keys

* ruff

* fix line too long

* unblock unittests

* fix keys model tests

* remove os patch

* fix account controller tests

* fix account controller tests

* fix account model

* account fixed

* fix user tests

* User Model

* User Model

* User Model

* Move session folder

* User Model : grouping the models

* User Model : fix bugs

* Config Plot + bug fix

* User Model : Refactoring plots

* Feature flags

* Remove dup

* clean comments a fix some vars

* fix feature flags controller

* refactor obbff from settings controller

* refactor a bunch of obbff variables

* fix bug blocking terminal

* fix integration tests

* refactor controllers with obbff

* refactor remaining ffs

* remove comments

* log not started

* mispelled variable

* set current user on local model

* User Model : moving files + updating

* User Preferences

* User : Refactor is_guest function + bug fix

* User : refactor is_guest function

* Fix conftest.py

* remove lost obbffs

* typo on fileoverwrite

* Plot config: move variables and start plot dpi

* User : tests

* refactor plot dpi references

* remove plot dpi imports

* refactor some config terminal leftover

* /account/sync : fix bug

* remove configs reload

* start path refactor

* installation paths vs user paths

* remove cfg_plot

* Tests : User Refactoring

* user prefix only for preferences

* user prefix only for preferences

* move user paths

* remove comment

* update apply remote configs

* fix tz not updating

* save some copys

* small fix

* create interface for credentials and preferences

* wrong docstrings fixed

* ruff core

* fix some pref setting

* login is better

* ruff

* ruff

* remove Literal

* add small comment

* Tests : User Refactoring

* Credentials Model : add DataBento

* Fix helper_funcs

* Fixing code

* Tests : fixing

* User Model : freezing the models

* Fixed Income + Databento

* Tests : fixing keys

* black

* ruff

* Tests : user model

* Tests : user model

* Fix linting

* Fix linting

* Linting

* Linting

* Fixing code and tests

* Fix tests

* mypy

* SDK

* Clean ruff cache

* Fix syntax issue

* Update fred_view

* Black

* Linting

* Update code

* Update tests

* Linting

* Linting

* Tests

* Black

* Linting

* Linting

* Mock os.listdir to avoid looking for file that doesnt exist

* Mock options paths

* lint

* Tests : fix

* Code : update

* Tests + Linting

* Tests

* Ruff

* Code : update

* Tests

* Code : update

* Fix workflow

* Code : update

---------

Co-authored-by: Chavithra PARANA <chavithra@gmail.com>
Co-authored-by: luqazino@gmail.com <luqazino@gmail.com>
Co-authored-by: James Maslek <jmaslek11@gmail.com>
2023-03-05 00:37:19 +01:00

113 lines
2.9 KiB
Python

from typing import Tuple
from prompt_toolkit import PromptSession
import openbb_terminal.core.session.local_model as Local
from openbb_terminal import terminal_controller
from openbb_terminal.core.config.paths import PACKAGE_DIRECTORY
from openbb_terminal.core.session.constants import REGISTER_URL
from openbb_terminal.core.session.session_model import (
LoginStatus,
create_session,
login,
)
from openbb_terminal.rich_config import console
def display_welcome_message():
"""Display welcome message"""
with open(PACKAGE_DIRECTORY / "core" / "session" / "banner.txt") as f:
console.print(f"[menu]{f.read()}[/menu]\n")
console.print(f"Register : [cmds]{REGISTER_URL}[/cmds]")
console.print("Ask support : [cmds]https://openbb.co/support[/cmds]")
console.print(
"[yellow]\nWARNING: This is a pre-release version published for testing.[/yellow]"
"[yellow]\nBeware that your account will be deleted without notice.[/yellow]"
)
def get_user_input() -> Tuple[str, str, bool]:
"""Get user input
Returns
-------
Tuple[str, str, bool]
The user email, password and save login option.
"""
console.print("[info]\nPlease enter your credentials:[/info]")
s: PromptSession = PromptSession()
email = s.prompt(
message="> Email: ",
)
if not email:
return "", "", False
password = s.prompt(
message="> Password: ",
is_password=True,
)
save_str = s.prompt(message="> Remember me? (y/n): ", is_password=False).lower()
save = False
if save_str == "y":
save = True
return email, password, save
def prompt(welcome=True):
"""Prompt and launch terminal if login is successful.
Parameters
----------
welcome : bool, optional
Display welcome message, by default True
"""
if welcome:
display_welcome_message()
while True:
email, password, save = get_user_input()
if not email or not password:
continue
session = create_session(email, password, save)
if isinstance(session, dict) and session:
return login_and_launch(session=session)
def launch_terminal():
"""Launch terminal"""
terminal_controller.parse_args_and_run()
def login_and_launch(session: dict):
"""Login and launch terminal.
Parameters
----------
session : dict
The session info.
"""
status = login(session)
if status in [LoginStatus.SUCCESS, LoginStatus.NO_RESPONSE]:
launch_terminal()
elif status == LoginStatus.FAILED:
prompt(welcome=False)
else:
prompt(welcome=True)
def main():
"""Main function"""
local_session = Local.get_session()
if not local_session:
prompt()
else:
login_and_launch(session=local_session)
if __name__ == "__main__":
main()