Files
OpenBB/cli/openbb_cli/session.py
Henrique Joaquim 13a62486de [Feature] Misc improvements on the Platform CLI (#6370)
* section description

* remove comment

* show msg only if it's an OBBject

* ommit coverage from menus

* styling: no new lines after settings

* Bugfix/cli max rows (#6374)

* fix: cli max rows

* fix: settings menu

---------

Co-authored-by: Henrique Joaquim <henriquecjoaquim@gmail.com>

* add a new line only to separate menus and commands

* if there's no menu description on reference.json, use the commands of that menu to create a pseudo description

* use the PATH instead in the top of the menu help

* default name len to 23

* keep command on the obbject so it can be shown on the results

* left spacing regardless description

* display cached results on every platform menu's help

* display info instead of sections and display cached results

* prepend OBB to use on the --data

* config to set number of cached results to display

* correct hub link

* Save routines locally if not logged in.

* Change the exit message

* Point to new docs on first launch.

* proper checking of max_obbjects_exceeded

* fix global flag on local routines

* Remove language from settings as it is not supported.

* Remove rcontext flag

* export to account multiple formats

* Revert "Remove rcontext flag"

This reverts commit 8a1f64b71c.

* Remove

* leftover

* properly match provider being used with provider arguments so that kwargs are correctly filtered

---------

Co-authored-by: montezdesousa <79287829+montezdesousa@users.noreply.github.com>
Co-authored-by: Igor Radovanovic <74266147+IgorWounds@users.noreply.github.com>
Co-authored-by: James Maslek <jmaslek11@gmail.com>
2024-05-10 13:39:28 +00:00

93 lines
2.7 KiB
Python

"""Settings module."""
import sys
from pathlib import Path
from typing import Optional
from openbb import obb
from openbb_core.app.model.abstract.singleton import SingletonMeta
from openbb_core.app.model.user_settings import UserSettings as User
from prompt_toolkit import PromptSession
from openbb_cli.argparse_translator.obbject_registry import Registry
from openbb_cli.config.completer import CustomFileHistory
from openbb_cli.config.console import Console
from openbb_cli.config.constants import HIST_FILE_PROMPT
from openbb_cli.config.style import Style
from openbb_cli.models.settings import Settings
class Session(metaclass=SingletonMeta):
"""Session class."""
def __init__(self):
"""Initialize session."""
self._obb = obb
self._settings = Settings()
self._style = Style(
style=self._settings.RICH_STYLE,
directory=Path(self._obb.user.preferences.user_styles_directory), # type: ignore[union-attr]
)
self._console = Console(
settings=self._settings, style=self._style.console_style
)
self._prompt_session = self._get_prompt_session()
self._obbject_registry = Registry()
@property
def user(self) -> User:
"""Get platform user."""
return self._obb.user # type: ignore[union-attr]
@property
def settings(self) -> Settings:
"""Get CLI settings."""
return self._settings
@property
def style(self) -> Style:
"""Get CLI style."""
return self._style
@property
def console(self) -> Console:
"""Get console."""
return self._console
@property
def obbject_registry(self) -> Registry:
"""Get obbject registry."""
return self._obbject_registry
@property
def prompt_session(self) -> Optional[PromptSession]:
"""Get prompt session."""
return self._prompt_session
def _get_prompt_session(self) -> Optional[PromptSession]:
"""Initialize prompt session."""
try:
if sys.stdin.isatty():
prompt_session: Optional[PromptSession] = PromptSession(
history=CustomFileHistory(str(HIST_FILE_PROMPT))
)
else:
prompt_session = None
except Exception:
prompt_session = None
return prompt_session
def is_local(self) -> bool:
"""Check if user is local."""
return not bool(self.user.profile.hub_session)
def reset(self) -> None:
pass
def max_obbjects_exceeded(self) -> bool:
"""Check if max obbjects exceeded."""
return (
len(self.obbject_registry.all) >= self.settings.N_TO_KEEP_OBBJECT_REGISTRY
)