mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-10-28 13:01:38 +00:00
* registry belongs to the session instead * help message on results * abstract the update_completer a bit and also force the re-link of newly created obbjects so they're immidiatly available * new settings to control the obbject registry * new method to remove a certain index from the stack * using the new flags to control messages on the registyr * fix: add spaces to results --help * Update cli/openbb_cli/controllers/feature_flags_controller.py Co-authored-by: montezdesousa <79287829+montezdesousa@users.noreply.github.com> * rename to settings_controller * typo wrong attr * better messages and actually removing the oldest obbject --------- Co-authored-by: Diogo Sousa <montezdesousa@gmail.com> Co-authored-by: montezdesousa <79287829+montezdesousa@users.noreply.github.com>
90 lines
3.2 KiB
Python
90 lines
3.2 KiB
Python
"""Registry for OBBjects."""
|
|
|
|
import json
|
|
from typing import Dict, List
|
|
|
|
from openbb_core.app.model.obbject import OBBject
|
|
|
|
|
|
class Registry:
|
|
"""Registry for OBBjects."""
|
|
|
|
def __init__(self):
|
|
"""Initialize the registry."""
|
|
self._obbjects: List[OBBject] = []
|
|
|
|
@staticmethod
|
|
def _contains_obbject(uuid: str, obbjects: List[OBBject]) -> bool:
|
|
"""Check if obbject with uuid is in the registry."""
|
|
return any(obbject.id == uuid for obbject in obbjects)
|
|
|
|
def register(self, obbject: OBBject):
|
|
"""Designed to add an OBBject instance to the registry."""
|
|
if isinstance(obbject, OBBject) and not self._contains_obbject(
|
|
obbject.id, self._obbjects
|
|
):
|
|
self._obbjects.append(obbject)
|
|
|
|
def get(self, idx: int) -> OBBject:
|
|
"""Return the obbject at index idx."""
|
|
# the list should work as a stack
|
|
# i.e., the last element needs to be accessed by idx=0 and so on
|
|
reversed_list = list(reversed(self._obbjects))
|
|
return reversed_list[idx]
|
|
|
|
def remove(self, idx: int = -1):
|
|
"""Remove the obbject at index idx, default is the last element."""
|
|
# the list should work as a stack
|
|
# i.e., the last element needs to be accessed by idx=0 and so on
|
|
reversed_list = list(reversed(self._obbjects))
|
|
del reversed_list[idx]
|
|
self._obbjects = list(reversed(reversed_list))
|
|
|
|
@property
|
|
def all(self) -> Dict[int, Dict]:
|
|
"""Return all obbjects in the registry"""
|
|
|
|
def _handle_standard_params(obbject: OBBject) -> str:
|
|
"""Handle standard params for obbjects"""
|
|
standard_params_json = ""
|
|
std_params = obbject._standard_params # pylint: disable=protected-access
|
|
if hasattr(std_params, "__dict__"):
|
|
standard_params = {
|
|
k: str(v)[:30] for k, v in std_params.__dict__.items() if v
|
|
}
|
|
standard_params_json = json.dumps(standard_params)
|
|
|
|
return standard_params_json
|
|
|
|
def _handle_data_repr(obbject: OBBject) -> str:
|
|
"""Handle data representation for obbjects"""
|
|
data_repr = ""
|
|
if hasattr(obbject, "results") and obbject.results:
|
|
data_schema = (
|
|
obbject.results[0].model_json_schema()
|
|
if obbject.results and isinstance(obbject.results, list)
|
|
else ""
|
|
)
|
|
if data_schema and "title" in data_schema:
|
|
data_repr = f"{data_schema['title']}"
|
|
if data_schema and "description" in data_schema:
|
|
data_repr += f" - {data_schema['description'].split('.')[0]}"
|
|
|
|
return data_repr
|
|
|
|
obbjects = {}
|
|
for i, obbject in enumerate(list(reversed(self._obbjects))):
|
|
obbjects[i] = {
|
|
"route": obbject._route, # pylint: disable=protected-access
|
|
"provider": obbject.provider,
|
|
"standard params": _handle_standard_params(obbject),
|
|
"data": _handle_data_repr(obbject),
|
|
}
|
|
|
|
return obbjects
|
|
|
|
@property
|
|
def obbjects(self) -> List[OBBject]:
|
|
"""Return all obbjects in the registry"""
|
|
return self._obbjects
|