Skip to content

Commit

Permalink
[WIP] Migrate user, feature flags and configurations to dataclass (Op…
Browse files Browse the repository at this point in the history
…enBB-finance#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 <[email protected]>
Co-authored-by: [email protected] <[email protected]>
Co-authored-by: James Maslek <[email protected]>
  • Loading branch information
4 people authored Mar 4, 2023
1 parent cdea224 commit 475ebb5
Show file tree
Hide file tree
Showing 372 changed files with 7,311 additions and 3,338 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/unit-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ env:
OPENBB_USE_PROMPT_TOOLKIT: false
OPENBB_FILE_OVERWRITE: true
OPENBB_ENABLE_CHECK_API: false
OPENBB_PREVIOUS_USER: true
OPENBB_PREVIOUS_USE: true
PIP_DEFAULT_TIMEOUT: 100

on:
Expand Down
95 changes: 51 additions & 44 deletions openbb_terminal/account/account_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,22 @@
from prompt_toolkit.completion import NestedCompleter

from openbb_terminal import (
feature_flags as obbff,
keys_model,
)
from openbb_terminal.account.account_model import get_diff, get_routines_info
from openbb_terminal.account.account_view import display_routines_list
from openbb_terminal.core.config.paths import USER_ROUTINES_DIRECTORY
from openbb_terminal.core.session import (
hub_model as Hub,
local_model as Local,
)
from openbb_terminal.core.session.current_user import get_current_user, is_local
from openbb_terminal.core.session.preferences_handler import set_preference
from openbb_terminal.core.session.session_model import logout
from openbb_terminal.decorators import log_start_end
from openbb_terminal.featflags_controller import FeatureFlagsController
from openbb_terminal.helper_funcs import check_positive
from openbb_terminal.menu import session
from openbb_terminal.parent_classes import BaseController
from openbb_terminal.rich_config import MenuText, console
from openbb_terminal.session import (
hub_model as Hub,
local_model as Local,
)
from openbb_terminal.session.session_model import logout
from openbb_terminal.session.user import User

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -58,7 +56,7 @@ def __init__(self, queue: Optional[List[str]] = None):
def update_runtime_choices(self):
"""Update runtime choices"""
self.ROUTINE_FILES = self.get_routines()
if session and obbff.USE_PROMPT_TOOLKIT:
if session and get_current_user().preferences.USE_PROMPT_TOOLKIT:
choices: dict = {c: {} for c in self.controller_choices} # type: ignore
choices["sync"] = {"--on": {}, "--off": {}}
choices["upload"]["--file"] = {c: {} for c in self.ROUTINE_FILES}
Expand All @@ -71,11 +69,17 @@ def update_runtime_choices(self):

def get_routines(self):
"""Get routines"""
current_user = get_current_user()
routines = {
filepath.name: filepath
for filepath in USER_ROUTINES_DIRECTORY.glob("*.openbb")
for filepath in current_user.preferences.USER_ROUTINES_DIRECTORY.glob(
"*.openbb"
)
}
user_folder = USER_ROUTINES_DIRECTORY / User.get_uuid()
user_folder = (
current_user.preferences.USER_ROUTINES_DIRECTORY
/ get_current_user().profile.get_uuid()
)
if os.path.exists(user_folder):
routines.update(
{filepath.name: filepath for filepath in user_folder.rglob("*.openbb")}
Expand Down Expand Up @@ -117,10 +121,11 @@ def call_logout(self, other_args: List[str]) -> None:
)
ns_parser = self.parse_known_args_and_warn(parser, other_args)
if ns_parser:
current_user = get_current_user()
logout(
auth_header=User.get_auth_header(),
token=User.get_token(),
guest=User.is_guest(),
auth_header=current_user.profile.get_auth_header(),
token=current_user.profile.get_token(),
guest=is_local(),
cls=True,
)

Expand All @@ -135,36 +140,30 @@ def call_sync(self, other_args: List[str]):
)
parser.add_argument(
"--on",
dest="on",
dest="sync",
help="Turn on sync",
action="store_true",
default=False,
)
parser.add_argument(
"--off",
dest="off",
dest="sync",
help="Turn on sync",
action="store_true",
default=False,
action="store_false",
)
parser.set_defaults(sync=None)

ns_parser = self.parse_known_args_and_warn(parser, other_args)
if ns_parser:
if ns_parser.on:
if not obbff.SYNC_ENABLED:
FeatureFlagsController.set_feature_flag(
"OPENBB_SYNC_ENABLED", True, force=True
)
elif ns_parser.off and obbff.SYNC_ENABLED:
FeatureFlagsController.set_feature_flag(
"OPENBB_SYNC_ENABLED", False, force=True
if ns_parser.sync is None:
sync = get_current_user().preferences.SYNC_ENABLED
console.print(f"sync is {sync}, use --on or --off to change.")
else:
set_preference(
name="OPENBB_SYNC_ENABLED",
value=ns_parser.sync,
)

sync = "ON" if obbff.SYNC_ENABLED else "OFF"

if ns_parser.on or ns_parser.off:
sync = get_current_user().preferences.SYNC_ENABLED
console.print(f"[info]sync:[/info] {sync}")
else:
console.print(f"sync is {sync}, use --on or --off to change.")

@log_start_end(log=logger)
def call_pull(self, other_args: List[str]):
Expand All @@ -177,7 +176,8 @@ def call_pull(self, other_args: List[str]):
)
ns_parser = self.parse_known_args_and_warn(parser, other_args)
if ns_parser:
response = Hub.fetch_user_configs(User.get_session())
current_user = get_current_user()
response = Hub.fetch_user_configs(current_user.profile.get_session())
if response:
configs_diff = get_diff(configs=json.loads(response.content))
if configs_diff:
Expand Down Expand Up @@ -210,7 +210,9 @@ def call_clear(self, other_args: List[str]):
)
console.print("")
if i.lower() in ["y", "yes"]:
Hub.clear_user_configs(auth_header=User.get_auth_header())
Hub.clear_user_configs(
auth_header=get_current_user().profile.get_auth_header()
)
else:
console.print("[info]Aborted.[/info]")

Expand Down Expand Up @@ -242,7 +244,7 @@ def call_list(self, other_args: List[str]):
ns_parser = self.parse_known_args_and_warn(parser, other_args)
if ns_parser:
response = Hub.list_routines(
auth_header=User.get_auth_header(),
auth_header=get_current_user().profile.get_auth_header(),
page=ns_parser.page,
size=ns_parser.size,
)
Expand Down Expand Up @@ -302,8 +304,10 @@ def call_upload(self, other_args: List[str]):
else " ".join(ns_parser.file).split(sep=".openbb", maxsplit=-1)[0]
)

current_user = get_current_user()

response = Hub.upload_routine(
auth_header=User.get_auth_header(),
auth_header=current_user.profile.get_auth_header(),
name=name,
description=description,
routine=routine,
Expand All @@ -316,7 +320,7 @@ def call_upload(self, other_args: List[str]):
console.print("")
if i.lower() in ["y", "yes"]:
response = Hub.upload_routine(
auth_header=User.get_auth_header(),
auth_header=current_user.profile.get_auth_header(),
name=name,
description=description,
routine=routine,
Expand Down Expand Up @@ -352,7 +356,7 @@ def call_download(self, other_args: List[str]):
ns_parser = self.parse_known_args_and_warn(parser, other_args)
if ns_parser:
response = Hub.download_routine(
auth_header=User.get_auth_header(),
auth_header=get_current_user().profile.get_auth_header(),
name=" ".join(ns_parser.name),
)

Expand Down Expand Up @@ -424,7 +428,7 @@ def call_delete(self, other_args: List[str]):
console.print("")
if i.lower() in ["y", "yes"]:
response = Hub.delete_routine(
auth_header=User.get_auth_header(),
auth_header=get_current_user().profile.get_auth_header(),
name=name,
)
if (
Expand Down Expand Up @@ -474,7 +478,8 @@ def call_generate(self, other_args: List[str]) -> None:
return

response = Hub.generate_personal_access_token(
auth_header=User.get_auth_header(), days=ns_parser.days
auth_header=get_current_user().profile.get_auth_header(),
days=ns_parser.days,
)
if response and response.status_code == 200:
token = response.json().get("token", "")
Expand Down Expand Up @@ -503,7 +508,9 @@ def call_show(self, other_args: List[str]) -> None:
)
ns_parser = self.parse_known_args_and_warn(parser, other_args)
if ns_parser:
response = Hub.get_personal_access_token(auth_header=User.get_auth_header())
response = Hub.get_personal_access_token(
auth_header=get_current_user().profile.get_auth_header()
)
if response and response.status_code == 200:
token = response.json().get("token", "")
if token:
Expand All @@ -526,7 +533,7 @@ def call_revoke(self, other_args: List[str]) -> None:
)
if i.lower() in ["y", "yes"]:
response = Hub.revoke_personal_access_token(
auth_header=User.get_auth_header()
auth_header=get_current_user().profile.get_auth_header()
)
if response and response.status_code in [200, 202]:
console.print("[info]Token revoked.[/info]")
Expand Down
29 changes: 7 additions & 22 deletions openbb_terminal/account/account_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,8 @@
import numpy as np
import pandas as pd

from openbb_terminal import (
config_plot as cfg_plot,
config_terminal as cfg,
feature_flags as obbff,
)
from openbb_terminal.base_helpers import strtobool
from openbb_terminal.core.config import paths
from openbb_terminal.core.session.current_user import get_current_user
from openbb_terminal.rich_config import console


Expand Down Expand Up @@ -67,23 +62,12 @@ def get_diff_settings(settings: dict) -> dict:
dict
The diff.
"""
current_user = get_current_user()
diff = {}
if settings:
for k, v in sorted(settings.items()):
if hasattr(obbff, k):
old, new = get_var_diff(obbff, k, v)
if new is not None:
diff[k] = (old, new)
elif hasattr(cfg, k):
old, new = get_var_diff(cfg, k, v)
if new is not None:
diff[k] = (old, new)
elif hasattr(cfg_plot, k):
old, new = get_var_diff(cfg_plot, k, v)
if new is not None:
diff[k] = (old, new)
elif hasattr(paths, k):
old, new = get_var_diff(paths, k, v)
if hasattr(current_user.preferences, k):
old, new = get_var_diff(current_user.preferences, k, v)
if new is not None:
diff[k] = (old, new)

Expand All @@ -103,11 +87,12 @@ def get_diff_keys(keys: dict) -> dict:
dict
The diff.
"""
current_user = get_current_user()
diff = {}
if keys:
for k, v in sorted(keys.items()):
if hasattr(cfg, k):
old, new = get_var_diff(cfg, k, v)
if hasattr(current_user.credentials, k):
old, new = get_var_diff(current_user.credentials, k, v)
if new is not None:
diff[k] = (old, new)

Expand Down
4 changes: 2 additions & 2 deletions openbb_terminal/alternative/alt_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import logging
from typing import List, Optional

from openbb_terminal import feature_flags as obbff
from openbb_terminal.alternative import hackernews_view
from openbb_terminal.core.session.current_user import get_current_user
from openbb_terminal.custom_prompt_toolkit import NestedCompleter
from openbb_terminal.decorators import log_start_end
from openbb_terminal.helper_funcs import EXPORT_ONLY_RAW_DATA_ALLOWED
Expand All @@ -30,7 +30,7 @@ def __init__(self, queue: Optional[List[str]] = None):
"""Constructor"""
super().__init__(queue)

if session and obbff.USE_PROMPT_TOOLKIT:
if session and get_current_user().preferences.USE_PROMPT_TOOLKIT:
choices: dict = self.choices_default

self.completer = NestedCompleter.from_nested_dict(choices)
Expand Down
4 changes: 2 additions & 2 deletions openbb_terminal/alternative/covid/covid_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

import pandas as pd

from openbb_terminal import feature_flags as obbff
from openbb_terminal.alternative.covid import covid_view
from openbb_terminal.core.session.current_user import get_current_user
from openbb_terminal.custom_prompt_toolkit import NestedCompleter
from openbb_terminal.decorators import log_start_end
from openbb_terminal.helper_funcs import (
Expand Down Expand Up @@ -42,7 +42,7 @@ def __init__(self, queue: Optional[List[str]] = None):
countries_list = countries_df["Countries"].to_list()
self.COUNTRY_LIST = [x.lower().replace(" ", "_") for x in countries_list]

if session and obbff.USE_PROMPT_TOOLKIT:
if session and get_current_user().preferences.USE_PROMPT_TOOLKIT:
choices: dict = self.choices_default

self.completer = NestedCompleter.from_nested_dict(choices)
Expand Down
4 changes: 2 additions & 2 deletions openbb_terminal/alternative/oss/github_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import pandas as pd

from openbb_terminal import config_terminal as cfg
from openbb_terminal.core.session.current_user import get_current_user
from openbb_terminal.decorators import check_api_key, log_start_end
from openbb_terminal.helper_funcs import get_user_agent, request
from openbb_terminal.rich_config import console
Expand All @@ -36,7 +36,7 @@ def get_github_data(url: str, **kwargs) -> Optional[Dict[str, Any]]:
res = request(
url,
headers={
"Authorization": f"token {cfg.API_GITHUB_KEY}",
"Authorization": f"token {get_current_user().credentials.API_GITHUB_KEY}",
"User-Agent": get_user_agent(),
"Accept": "application/vnd.github.v3.star+json",
},
Expand Down
4 changes: 2 additions & 2 deletions openbb_terminal/alternative/oss/oss_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import logging
from typing import List, Optional

from openbb_terminal import feature_flags as obbff
from openbb_terminal.alternative.oss import github_view, runa_model, runa_view
from openbb_terminal.core.session.current_user import get_current_user
from openbb_terminal.custom_prompt_toolkit import NestedCompleter
from openbb_terminal.decorators import log_start_end
from openbb_terminal.helper_funcs import (
Expand All @@ -33,7 +33,7 @@ def __init__(self, queue: Optional[List[str]] = None):
"""Constructor"""
super().__init__(queue)

if session and obbff.USE_PROMPT_TOOLKIT:
if session and get_current_user().preferences.USE_PROMPT_TOOLKIT:
choices: dict = self.choices_default

self.completer = NestedCompleter.from_nested_dict(choices)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from datetime import datetime, timedelta
from typing import List, Optional

from openbb_terminal import feature_flags as obbff
from openbb_terminal.alternative.realestate import landRegistry_view
from openbb_terminal.core.session.current_user import get_current_user
from openbb_terminal.custom_prompt_toolkit import NestedCompleter
from openbb_terminal.decorators import log_start_end
from openbb_terminal.helper_funcs import EXPORT_ONLY_RAW_DATA_ALLOWED, check_positive
Expand Down Expand Up @@ -51,7 +51,7 @@ def __init__(self, queue: Optional[List[str]] = None):
self.region = None
self.limit = 25

if session and obbff.USE_PROMPT_TOOLKIT:
if session and get_current_user().preferences.USE_PROMPT_TOOLKIT:
choices: dict = self.choices_default
self.completer = NestedCompleter.from_nested_dict(choices)

Expand Down
Loading

0 comments on commit 475ebb5

Please sign in to comment.