Files
OpenBB/openbb_terminal/core/session/session_controller.py
montezdesousa b6b42afd71 Hub | Add colors, sources sync (#4546)
* fix show version

* update login prompt and account

* update set preference

* fix account controller

* block login/logout if not called from account

* change conditions to start terminal

* fix required args account model

* add not to login command

* pylint

* accounte controller messages

* set default user when logout

* logout then show menu

* remove unused functions

* fix logging bug

* ruff

* fix account unittests

* fix session unittests

* refactor colors preference

* no theme, no party

* add colors sync

* dynamic theme

* add comment

* add colors to settings

* ruff

* linting

* update menu desc

* adjust keys sync flag

* write to dotenv sync flag

* english

* fix pull command

* fix edge cases

* handle some errors

* small bug

* change func name

* pylint

* no light

* colors preview

* ruff

* fix login logout preferences

* ruff

* delete folder

* pylint

* default, custom, hub

* fix clear

* docstring

* add preference exceptions

* set only rich style

* request theme only if hub

* linting

* change guest msg

* add option to update chart style on the fly

* apply style

* rollback to old terminal style

* fix console theme

* remove print

* remove hub rich style

* ruff

* mypy

* black

* Hub | Add sources sync (#4565)

* Add sources model

* adapt session model

* fetch sources dict

* rename local funcs

* set sources inside current user

* set sources inside current user

* generate sources dict from hub

* fix dict generator

* send request to hub

* fix conflict

* revert datasources to develop head

* fix docstring

* avoid invalid patch types

* add default option to sources

* fix small bug on sources

* ruff

* black

* remove (

* mypy failing only on git

* ignore mypy

* pylint

* fix settings controller

* Tests: fix keys model writing to .env

* fix keys controller test writing to .env

* ruff

* Test: fix account controller

* Test: fix account controller pull

* Tests: fix launch terminal

* Test: add sources to user object

* Tests: fix clear cmd tests

* ruff

* revert core/sdk changes

* TerminalStyle docstring

* upload config docstring

* remove comment

* remove unecessary pylint disable

* revert openbb_terminal/sdk changes

* Terminal style docstring

* docstring

* remove unecessary changes

* add TODO

* add other todo

* Tests: prevent sync from being written in .env

* change clear msg

* display sources source on menu

* update settings controller

* display keys source

* black

* sync not needed for pull

* remove sync from apply configs

* change func name for consistency

* Update test_keys_model.py

* save rich style in user folder

* search recursively inside styles

* set hub theme by default

* remove debug print

* allow any plot style

* comment plotstyle sync for now

* fix sources set terminal to hub

* ruff

* rewrite cassettes

* remove repeated settings

* rewrite expected
2023-03-27 16:59:36 +00:00

115 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.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 or press <ENTER> for guest mode:[/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:
return launch_terminal()
session = create_session(email, password, save)
if isinstance(session, dict) and session:
return login_and_launch(session=session)
def launch_terminal():
"""Launch terminal"""
from openbb_terminal import terminal_controller
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()