Files
OpenBB/openbb_terminal/core/session/sources_handler.py
montezdesousa b9ea4d5cd0 Refactor sources menu (#4668)
* add data sources defaults to model

* fix f string

* refactor sources controller

* write sources to file

* write to user file

* add user data sources file inside OpenBBUserData

* append to user file, don't rewrite

* fix when sources folder does not exist

* codespell

* change func name

* change path to str

* ruff

* Add method to upload entire user field

* change clear func args

* update get_ordered_list_sources function

* move flatten

* move flatten

* change extend func name

* remove comment

* clean sources utils funcs

* mypy

* Update utils.py

* spacing

* docs

---------

Co-authored-by: hjoaquim <h.joaquim@campus.fct.unl.pt>
2023-04-05 12:13:34 +00:00

48 lines
1.0 KiB
Python

import json
import os
from pathlib import Path
from typing import Dict
from openbb_terminal.core.sources.utils import extend, flatten
def read_sources(path: Path) -> Dict:
"""Read sources from file.
Parameters
----------
path : str
Path to file
Returns
-------
Dict
Dictionary with sources
"""
try:
if path.exists() and os.stat(path).st_size > 0:
with open(path) as file:
return flatten(json.load(file))
return {}
except Exception as e:
print(f"\nFailed to read data sources file: {path}\n{e}\n")
print("Using OpenBB defaults.")
return {}
def write_sources(sources: Dict, path: Path):
"""Write sources to file.
Parameters
----------
sources : Dict
Dictionary with sources
path : Path
Path to file
"""
try:
with open(path, "w") as f:
json.dump(extend(sources), f, indent=4)
except Exception as e:
print(f"\nFailed to write data sources file: {path}\n{e}\n")