mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-11-04 16:32:10 +00:00
* 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>
48 lines
1.0 KiB
Python
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")
|