mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-10-29 05:21:48 +00:00
Merge branch 'feature/login' of github.com:OpenBB-finance/OpenBBTerminal into feature/login
This commit is contained in:
@@ -6,9 +6,9 @@ from typing import Any, Callable, Dict, List, Optional
|
||||
from inspect import signature
|
||||
|
||||
import openbb_terminal.config_terminal as cfg
|
||||
|
||||
from openbb_terminal.core.library.metadata import Metadata
|
||||
from openbb_terminal.core.library.trail_map import TrailMap
|
||||
from openbb_terminal.session.sdk_session import login
|
||||
|
||||
# pylint: disable=import-outside-toplevel
|
||||
|
||||
@@ -232,7 +232,7 @@ class OperationLogger:
|
||||
|
||||
if func_module == "openbb_terminal.keys_model":
|
||||
|
||||
from openbb_terminal.core.log.generation.settings_logger import ( # pylint: disable=C0415
|
||||
from openbb_terminal.core.log.generation.settings_logger import ( # pylint: disable=import-outside-toplevel
|
||||
log_keys,
|
||||
)
|
||||
|
||||
@@ -252,6 +252,9 @@ class OperationLogger:
|
||||
method_result=method_result,
|
||||
method_chosen=self.__method_chosen,
|
||||
)
|
||||
self.__log_if_login(
|
||||
method_chosen=self.__method_chosen,
|
||||
)
|
||||
self.__log_end(
|
||||
logger=logger,
|
||||
method_chosen=self.__method_chosen,
|
||||
@@ -275,6 +278,17 @@ class OperationLogger:
|
||||
extra={"func_name_override": method_chosen.__name__},
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def __log_if_login(
|
||||
method_chosen: Callable,
|
||||
):
|
||||
if method_chosen.__name__ == login.__name__:
|
||||
from openbb_terminal.core.log.generation.user_logger import ( # pylint: disable=import-outside-toplevel
|
||||
log_user,
|
||||
)
|
||||
|
||||
log_user(with_rollover=False)
|
||||
|
||||
@staticmethod
|
||||
def __log_end(logger: Logger, method_chosen: Callable):
|
||||
logger.info(
|
||||
|
||||
@@ -5,13 +5,9 @@ import re
|
||||
# IMPORTATION THIRDPARTY
|
||||
|
||||
# IMPORTATION INTERNAL
|
||||
from openbb_terminal.core.log.generation.user_logger import __name__ as user_logger_name
|
||||
from openbb_terminal.core.config.paths import HOME_DIRECTORY
|
||||
from openbb_terminal.core.log.generation.settings import AppSettings
|
||||
|
||||
# considering starting at 0 and after the logging prefix i.e., just the text to be filtered
|
||||
MODULE_POSITION = 1
|
||||
|
||||
|
||||
class FormatterWithExceptions(logging.Formatter):
|
||||
"""Logging Formatter that includes formatting of Exceptions"""
|
||||
@@ -58,15 +54,19 @@ class FormatterWithExceptions(logging.Formatter):
|
||||
|
||||
@staticmethod
|
||||
def mock_email(text: str) -> str:
|
||||
if text.split("|")[MODULE_POSITION] == user_logger_name:
|
||||
return text
|
||||
|
||||
pattern = r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b"
|
||||
replacement = " FILTERED_EMAIL "
|
||||
text_mocked = re.sub(pattern, replacement, text)
|
||||
|
||||
return text_mocked
|
||||
|
||||
@staticmethod
|
||||
def mock_password(text: str) -> str:
|
||||
pattern = r'("password": ")[^"]+'
|
||||
replacement = r"\1 FILTERED_PASSWORD "
|
||||
text_mocked = re.sub(pattern, replacement, text)
|
||||
return text_mocked
|
||||
|
||||
@staticmethod
|
||||
def mock_home_directory(text: str) -> str:
|
||||
user_home_directory = str(HOME_DIRECTORY)
|
||||
@@ -85,6 +85,7 @@ class FormatterWithExceptions(logging.Formatter):
|
||||
def filter_piis(cls, text: str) -> str:
|
||||
text_filtered = cls.mock_ipv4(text=text)
|
||||
text_filtered = cls.mock_email(text=text_filtered)
|
||||
text_filtered = cls.mock_password(text=text_filtered)
|
||||
text_filtered = cls.mock_home_directory(text=text_filtered)
|
||||
|
||||
return text_filtered
|
||||
|
||||
@@ -15,13 +15,13 @@ logger = logging.getLogger(__name__)
|
||||
def log_user(with_rollover: bool = True):
|
||||
"""Log user"""
|
||||
if not User.is_guest():
|
||||
log_user_info()
|
||||
_log_user_info()
|
||||
|
||||
if with_rollover:
|
||||
do_rollover()
|
||||
|
||||
|
||||
def log_user_info():
|
||||
def _log_user_info():
|
||||
"""Log user info"""
|
||||
user_info = {"user_uuid": User.get_uuid(), "user_email": User.get_email()}
|
||||
user_info = {"user_uuid": User.get_uuid()}
|
||||
logger.info("USER: %s ", json.dumps(user_info))
|
||||
|
||||
@@ -1,14 +1,10 @@
|
||||
import json
|
||||
|
||||
from openbb_terminal.session import local_model as Local
|
||||
from openbb_terminal.session import hub_model as Hub
|
||||
from openbb_terminal.session.session_model import create_session
|
||||
from openbb_terminal.session.user import User
|
||||
from openbb_terminal.session import session_model
|
||||
from openbb_terminal.rich_config import console
|
||||
|
||||
|
||||
def get_session(email: str, password: str, save: bool):
|
||||
session = create_session(email, password, save)
|
||||
session = session_model.create_session(email, password, save)
|
||||
if not (isinstance(session, dict) and session):
|
||||
raise Exception("Failed to create session")
|
||||
return session
|
||||
@@ -20,11 +16,10 @@ def login(email: str = "", password: str = "", save: bool = False):
|
||||
if not session:
|
||||
session = get_session(email, password, save)
|
||||
|
||||
User.load_user_info(session)
|
||||
response = Hub.fetch_user_configs(session)
|
||||
if session_model.login(session) in [
|
||||
session_model.LoginStatus.FAILED,
|
||||
session_model.LoginStatus.NO_RESPONSE,
|
||||
]:
|
||||
raise Exception("Login failed")
|
||||
|
||||
if not (response and response.status_code == 200):
|
||||
raise Exception("Failed to fetch user configs")
|
||||
|
||||
Local.apply_configs(configs=json.loads(response.content))
|
||||
console.print("[green]Login successful[/green]")
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import importlib
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
import json
|
||||
from enum import Enum
|
||||
import matplotlib.pyplot as plt
|
||||
from enum import Enum
|
||||
import openbb_terminal.session.local_model as Local
|
||||
@@ -12,9 +14,9 @@ from openbb_terminal.rich_config import console
|
||||
|
||||
|
||||
class LoginStatus(Enum):
|
||||
SUCCESS = 1
|
||||
FAILED = 2
|
||||
NO_RESPONSE = 3
|
||||
SUCCESS = "success"
|
||||
FAILED = "failed"
|
||||
NO_RESPONSE = "no_response"
|
||||
|
||||
|
||||
def create_session(email: str, password: str, save: bool) -> dict:
|
||||
@@ -71,6 +73,10 @@ def logout(cls: bool = False):
|
||||
if v.startswith("OPENBB"):
|
||||
os.environ.pop(v)
|
||||
|
||||
# Remove the log handlers - needs to be done before reloading modules
|
||||
for handler in logging.root.handlers[:]:
|
||||
logging.root.removeHandler(handler)
|
||||
|
||||
# Reload all openbb modules to clear memorized variables
|
||||
modules = sys.modules.copy()
|
||||
for module in modules:
|
||||
|
||||
Reference in New Issue
Block a user