Files
OpenBB/openbb_terminal/core/sources/utils.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

91 lines
2.1 KiB
Python

from collections.abc import MutableMapping
from functools import reduce
from typing import Any, Dict, List
def nested_dict(test_str, sep, value) -> Dict:
if sep not in test_str:
return {test_str: value}
key, val = test_str.split(sep, 1)
return {key: nested_dict(val, sep, value)}
def get_list_of_dicts(d: Dict[str, str]) -> List[Dict[str, Any]]:
"""Get list of dictionaries from a dictionary.
Parameters
----------
d : dict
The dictionary to convert.
Returns
-------
List[Dict[str, Any]]
The list of dictionaries.
"""
list_of_dicts = []
for k, v in d.items():
list_of_dicts.append(nested_dict(k, "/", v.replace(" ", "").split(",")))
return list_of_dicts
def recursive_merge(d1: Dict, d2: Dict) -> Dict:
"""Recursively merge dict d2 into dict d1.
Source: https://stackoverflow.com/questions/7204805/how-to-merge-dictionaries-of-dictionaries
Parameters
----------
d1 : Dict
The dictionary to be merged into.
d2 : Dict
The dictionary to merge into d1.
Returns
-------
Dict
The merged dictionary.
"""
for k, v in d1.items():
if k in d2 and all(isinstance(e, MutableMapping) for e in (v, d2[k])):
d2[k] = recursive_merge(v, d2[k])
d3 = d1.copy()
d3.update(d2)
return d3
def merge_dicts(list_of_dicts: List[Dict[str, Any]]) -> Dict[str, Any]:
"""Merge a list of dictionaries.
Parameters
----------
list_of_dicts : List[Dict[str, Any]]
The list of dictionaries to merge.
Returns
-------
Dict[str, Any]
The merged dictionary.
"""
result: Dict[str, Any] = {}
for d in list_of_dicts:
result = reduce(recursive_merge, (result, d))
return result
def generate_sources_dict(sources: Dict[str, str]) -> Dict[str, Any]:
"""Generate a dictionary of sources.
Parameters
----------
sources : Dict[str, str]
The sources dictionary.
Returns
-------
Dict[str, Any]
The sources dictionary.
"""
return merge_dicts(get_list_of_dicts(sources))