Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds BaseModel and SystemModel #4491

Merged
merged 30 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
529d479
adding BaseModel
hjoaquim Mar 15, 2023
e2f53ca
Merge branch 'develop' into feature/base-class
hjoaquim Mar 15, 2023
e076468
Merge remote-tracking branch 'OpenBB-finance/develop' into feature/ba…
hjoaquim Mar 15, 2023
6356256
initial version of the system model
hjoaquim Mar 15, 2023
3314a01
Merge branch 'feature/base-class' of github.com:OpenBB-finance/OpenBB…
hjoaquim Mar 16, 2023
0f29b3f
creating setup functions and removing constants from config_terminal
hjoaquim Mar 16, 2023
ab02e8b
removing log_supress const
hjoaquim Mar 16, 2023
14d47da
fix test
hjoaquim Mar 16, 2023
ff0ed36
merge
hjoaquim Mar 16, 2023
4ca720e
Merge remote-tracking branch 'OpenBB-finance/develop' into feature/ba…
hjoaquim Mar 16, 2023
7584385
system handler
hjoaquim Mar 20, 2023
c9a04fd
Merge branch 'develop' into feature/base-class
hjoaquim Mar 20, 2023
f1de13f
fix import
hjoaquim Mar 20, 2023
3c6d069
centralized configs setup
hjoaquim Mar 20, 2023
2d6e33a
repr to use the BaseModel
hjoaquim Mar 20, 2023
6f5159a
Merge branch 'develop' into feature/base-class
hjoaquim Mar 20, 2023
c263922
updating i18n dict location
hjoaquim Mar 21, 2023
9b712f1
ignoring warning on wrong type-var
hjoaquim Mar 21, 2023
56a7f3b
Merge branch 'develop' into feature/base-class
hjoaquim Mar 21, 2023
7773d52
ruff
hjoaquim Mar 21, 2023
ff5c929
Update openbb_terminal/core/models/system_model.py
hjoaquim Mar 21, 2023
6cf9cbc
Update openbb_terminal/core/models/system_model.py
hjoaquim Mar 21, 2023
f6b0933
import literal
hjoaquim Mar 21, 2023
a53b839
loading env files before the plot backend is called
hjoaquim Mar 21, 2023
a1a31a8
fix tests/openbb_terminal/test_terminal_helper.py
hjoaquim Mar 22, 2023
73114c1
tests need to have i18n initialized
hjoaquim Mar 22, 2023
112b45d
Merge branch 'develop' into feature/base-class
hjoaquim Mar 22, 2023
674829b
fix seeking_alpha tests
hjoaquim Mar 22, 2023
39b16d7
Merge branch 'feature/base-class' of github.com:OpenBB-finance/OpenBB…
hjoaquim Mar 22, 2023
fd4439a
Merge branch 'develop' into feature/base-class
hjoaquim Mar 22, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions generate_sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,17 @@ def __init__(self) -> None:
self.__check_initialize_logging()

def __check_initialize_logging(self):
if not cfg.LOGGING_SUPPRESS:
if not get_current_system().LOGGING_SUPPRESS:
self.__initialize_logging()

@staticmethod
def __initialize_logging() -> None:
# pylint: disable=C0415
from openbb_terminal.config_terminal import setup_logging_sub_app
from openbb_terminal.core.log.generation.settings_logger import log_all_settings
from openbb_terminal.loggers import setup_logging

cfg.LOGGING_SUB_APP = "sdk"
setup_logging_sub_app(sub_app="sdk")
setup_logging()
log_all_settings()

Expand Down Expand Up @@ -240,7 +241,7 @@ def write_class_attr_docs(
if module:
f.write(" def __init__(self):\r super().__init__()\r")
elif sdk_root:
f.write(" __version__ = cfg.VERSION\r\r")
f.write(" __version__ = get_current_system().VERSION\r\r")
f.write(" def __init__(self):\r SDKLogger()\r")

def write_class_attributes(
Expand Down
162 changes: 93 additions & 69 deletions openbb_terminal/config_terminal.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
# IMPORTATION STANDARD
import os
import dataclasses
from pathlib import Path

# IMPORTATION THIRDPARTY
from typing import Optional

import i18n

# IMPORTATION INTERNAL
from openbb_terminal.base_helpers import load_env_files, load_env_vars, strtobool
from openbb_terminal.core.config.paths import MISCELLANEOUS_DIRECTORY
from openbb_terminal.core.config.paths import I18N_DICT_LOCATION
from openbb_terminal.core.config.paths_helper import init_userdata
from openbb_terminal.core.plots.backend import plots_backend
from openbb_terminal.core.session.current_system import (
get_current_system,
set_current_system,
)
from openbb_terminal.core.session.current_user import get_current_user

from .helper_classes import TerminalStyle as _TerminalStyle

# Terminal UX section
current_user = get_current_user()
theme = _TerminalStyle(
current_user.preferences.MPL_STYLE,
current_user.preferences.PMF_STYLE,
current_user.preferences.RICH_STYLE,
)


# Start the plots backend
def start_plot_backend():
"""Starts the plot backend"""
plots_backend().start(load_env_vars("DEBUG_MODE", strtobool, False))
Expand All @@ -34,63 +32,89 @@ def start_required_configurations():
init_userdata()


# Logging section
def change_logging_suppress(new_value: bool):
"""Change the logging suppress value"""
current_system = get_current_system()
updated_system = dataclasses.replace(current_system, LOGGING_SUPPRESS=new_value) # type: ignore
set_current_system(updated_system)

# USE IN LOG LINES + FOR FOLDER NAME INSIDE S3 BUCKET
LOGGING_APP_NAME = (
"gst_packaged_pypi"
if "site-packages" in __file__
else os.getenv("OPENBB_LOGGING_APP_NAME") or "gst"
)
# AWS KEYS
LOGGING_AWS_ACCESS_KEY_ID = (
os.getenv("OPENBB_LOGGING_AWS_ACCESS_KEY_ID") or "REPLACE_ME"
)
LOGGING_AWS_SECRET_ACCESS_KEY = (
os.getenv("OPENBB_LOGGING_AWS_SECRET_ACCESS_KEY") or "REPLACE_ME"

def setup_i18n(i18n_path: Path = I18N_DICT_LOCATION):
"""Select the terminal translation language."""

i18n.load_path.append(i18n_path)
i18n.set("locale", get_current_user().preferences.USE_LANGUAGE)
i18n.set("filename_format", "{locale}.{format}")


def setup_version():
"""Setup the version of the terminal"""

def try_get_version_from_dist() -> Optional[str]:
def check_using_git():
try:
__import__("git")
except ImportError:
return False
return True

if not check_using_git():
try:
import pkg_resources # pylint:disable=import-outside-toplevel

return pkg_resources.get_distribution("OpenBB").version

except Exception:
pass

return None

version = try_get_version_from_dist()

if version:
current_system = get_current_system()
updated_system = dataclasses.replace(current_system, VERSION=version) # type: ignore
set_current_system(updated_system)


def setup_logging_app_name():
"""
Setup the logging app name of the terminal

- if there's a LOGGING_APP_NAME in the environment => use it
- if "site-packages" in __file__ => use "gst_packaged_pypi" (will override environment variable)
- if none of the above => use "gst" (which is the default value on the SystemModel)
"""

if "site-packages" in __file__:
current_system = get_current_system()
updated_system = dataclasses.replace(
current_system, LOGGING_APP_NAME="gst_packaged_pypi"
) # type: ignore
set_current_system(updated_system)


def setup_logging_sub_app(sub_app: str):
"""Setup the logging sub app"""

current_system = get_current_system()
updated_system = dataclasses.replace(current_system, LOGGING_SUB_APP=sub_app) # type: ignore
set_current_system(updated_system)


def setup_config_terminal():
"""Setup the config terminal"""
start_plot_backend()
start_required_configurations()
hjoaquim marked this conversation as resolved.
Show resolved Hide resolved
setup_i18n()
setup_version()
setup_logging_app_name()


# Terminal UX section
current_user = get_current_user()
theme = _TerminalStyle(
current_user.preferences.MPL_STYLE,
current_user.preferences.PMF_STYLE,
current_user.preferences.RICH_STYLE,
)
LOGGING_COMMIT_HASH = str(os.getenv("OPENBB_LOGGING_COMMIT_HASH", "REPLACE_ME"))
# D | H | M | S
LOGGING_FREQUENCY = os.getenv("OPENBB_LOGGING_FREQUENCY") or "H"
# stdout,stderr,noop,file
LOGGING_HANDLERS = os.getenv("OPENBB_LOGGING_HANDLERS") or "file"
LOGGING_ROLLING_CLOCK = load_env_vars("OPENBB_LOGGING_ROLLING_CLOCK", strtobool, False)
# CRITICAL = 50
# FATAL = CRITICAL
# ERROR = 40
# WARNING = 30
# WARN = WARNING
# INFO = 20
# DEBUG = 10
# NOTSET = 0
LOGGING_VERBOSITY = load_env_vars("OPENBB_LOGGING_VERBOSITY", int, 20)
# LOGGING SUB APP
LOGGING_SUB_APP = os.getenv("OPENBB_LOGGING_SUB_APP") or "terminal"
LOGGING_SUPPRESS = False

# pylint: disable=no-member,c-extension-no-member

try:
__import__("git")
except ImportError:
WITH_GIT = False
else:
WITH_GIT = True


try:
if not WITH_GIT:
import pkg_resources

version = pkg_resources.get_distribution("OpenBB").version
else:
raise Exception("Using git")
except Exception:
version = "2.5.1"
VERSION = str(os.getenv("OPENBB_VERSION", version))

# # Select the terminal translation language
i18n_dict_location = MISCELLANEOUS_DIRECTORY / "i18n"
i18n.load_path.append(i18n_dict_location)
i18n.set("locale", get_current_user().preferences.USE_LANGUAGE)
i18n.set("filename_format", "{locale}.{format}")
3 changes: 3 additions & 0 deletions openbb_terminal/core/config/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@
SETTINGS_DIRECTORY = HOME_DIRECTORY / ".openbb_terminal"
SETTINGS_ENV_FILE = SETTINGS_DIRECTORY / ".env"
HIST_FILE_PATH = SETTINGS_DIRECTORY / ".openbb_terminal.his"

# i18n_dict_location
I18N_DICT_LOCATION = MISCELLANEOUS_DIRECTORY / "i18n"
4 changes: 4 additions & 0 deletions openbb_terminal/core/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
from openbb_terminal.core.models.base_model import BaseModel
from openbb_terminal.core.models.credentials_model import CredentialsModel
from openbb_terminal.core.models.preferences_model import PreferencesModel
from openbb_terminal.core.models.profile_model import ProfileModel
from openbb_terminal.core.models.system_model import SystemModel
from openbb_terminal.core.models.user_model import UserModel

__all__ = [
"BaseModel",
"CredentialsModel",
"PreferencesModel",
"ProfileModel",
"SystemModel",
"UserModel",
]
29 changes: 29 additions & 0 deletions openbb_terminal/core/models/base_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from typing import Any, Optional

from pydantic.dataclasses import dataclass

# pylint: disable=too-many-instance-attributes, disable=no-member


@dataclass
class BaseModel:
def __repr__(self) -> str:
"""Return string representation of model."""
dataclass_repr = ""
for key, value in self.__dict__.items():
if key.startswith("_"):
continue
dataclass_repr += f" {key}='{value}', \n"

return f"{self.__class__.__name__}(\n{dataclass_repr[:-2]}\n)"

@classmethod
def get_fields(cls) -> dict[str, Any]:
"""Get dict of fields."""
return cls.__dataclass_fields__ # type: ignore

def get_field_value(self, field: str) -> Optional[str]:
"""Get field value."""
if hasattr(self, field):
return getattr(self, field)
return None
31 changes: 9 additions & 22 deletions openbb_terminal/core/models/credentials_model.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from typing import Any, Optional
from typing import Optional

from pydantic.dataclasses import dataclass

from openbb_terminal.core.models import BaseModel

# pylint: disable=too-many-instance-attributes, disable=no-member


@dataclass(config=dict(validate_assignment=True))
class CredentialsModel:
@dataclass(config=dict(validate_assignment=True, frozen=True))
class CredentialsModel(BaseModel):
"""Model for credentials."""

# Data providers
Expand Down Expand Up @@ -64,23 +66,8 @@ class CredentialsModel:
API_COINBASE_SECRET: str = "REPLACE_ME"
API_COINBASE_PASS_PHRASE: str = "REPLACE_ME"

def __repr__(self) -> str:
"""Return string representation of model."""
dataclass_repr = ""
for key, value in self.__dict__.items():
if key.startswith("_"):
continue
dataclass_repr += f" {key}='{value}', \n"

return f"{self.__class__.__name__}(\n{dataclass_repr[:-2]}\n)"

@classmethod
def get_fields(cls) -> dict[str, Any]:
"""Get dict of fields."""
return cls.__dataclass_fields__ # type: ignore
# Others
OPENBB_PERSONAL_ACCESS_TOKEN: str = "REPLACE_ME"

def get_field_value(self, field: str) -> Optional[str]:
"""Get field value."""
if hasattr(self, field):
return getattr(self, field)
return None
def __repr__(self) -> str: # pylint: disable=useless-super-delegation
return super().__repr__()
27 changes: 5 additions & 22 deletions openbb_terminal/core/models/preferences_model.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
from typing import Any, Optional
from typing import Optional

from pydantic import NonNegativeInt, PositiveFloat, PositiveInt
from pydantic.dataclasses import dataclass
Expand All @@ -8,12 +8,13 @@
HOME_DIRECTORY,
USER_DATA_SOURCES_DEFAULT_FILE,
)
from openbb_terminal.core.models import BaseModel

# pylint: disable=too-many-instance-attributes, disable=no-member


@dataclass(config=dict(validate_assignment=True, frozen=True))
class PreferencesModel:
class PreferencesModel(BaseModel):
"""Data model for preferences."""

# PLOT
Expand Down Expand Up @@ -97,23 +98,5 @@ class PreferencesModel:
USER_FORECAST_MODELS_DIRECTORY = USER_DATA_DIRECTORY / "exports" / "forecast_models"
USER_FORECAST_WHISPER_DIRECTORY = USER_DATA_DIRECTORY / "exports" / "whisper"

def __repr__(self) -> str:
"""Return string representation of model."""
dataclass_repr = ""
for key, value in self.__dict__.items():
if key.startswith("_"):
continue
dataclass_repr += f" {key}='{value}', \n"

return f"{self.__class__.__name__}(\n{dataclass_repr[:-2]}\n)"

@classmethod
def get_fields(cls) -> dict[str, Any]:
"""Get dict of fields."""
return cls.__dataclass_fields__ # type: ignore

def get_field_value(self, field: str) -> Optional[str]:
"""Get field value."""
if hasattr(self, field):
return getattr(self, field)
return None
def __repr__(self) -> str: # pylint: disable=useless-super-delegation
return super().__repr__()
7 changes: 6 additions & 1 deletion openbb_terminal/core/models/profile_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

from pydantic.dataclasses import dataclass

from openbb_terminal.core.models import BaseModel


@dataclass(config=dict(validate_assignment=True, frozen=True))
class ProfileModel:
class ProfileModel(BaseModel):
"""Data model for profile."""

token_type: str = ""
Expand Down Expand Up @@ -72,3 +74,6 @@ def get_auth_header(self) -> str:
The auth header, e.g. "Bearer <token>".
"""
return f"{self.token_type.title()} {self.token}"

def __repr__(self) -> str: # pylint: disable=useless-super-delegation
return super().__repr__()
Loading