Files
OpenBB/cli/openbb_cli/argparse_translator/obbject_registry.py
Henrique Joaquim 13a62486de [Feature] Misc improvements on the Platform CLI (#6370)
* section description

* remove comment

* show msg only if it's an OBBject

* ommit coverage from menus

* styling: no new lines after settings

* Bugfix/cli max rows (#6374)

* fix: cli max rows

* fix: settings menu

---------

Co-authored-by: Henrique Joaquim <henriquecjoaquim@gmail.com>

* add a new line only to separate menus and commands

* if there's no menu description on reference.json, use the commands of that menu to create a pseudo description

* use the PATH instead in the top of the menu help

* default name len to 23

* keep command on the obbject so it can be shown on the results

* left spacing regardless description

* display cached results on every platform menu's help

* display info instead of sections and display cached results

* prepend OBB to use on the --data

* config to set number of cached results to display

* correct hub link

* Save routines locally if not logged in.

* Change the exit message

* Point to new docs on first launch.

* proper checking of max_obbjects_exceeded

* fix global flag on local routines

* Remove language from settings as it is not supported.

* Remove rcontext flag

* export to account multiple formats

* Revert "Remove rcontext flag"

This reverts commit 8a1f64b71c.

* Remove

* leftover

* properly match provider being used with provider arguments so that kwargs are correctly filtered

---------

Co-authored-by: montezdesousa <79287829+montezdesousa@users.noreply.github.com>
Co-authored-by: Igor Radovanovic <74266147+IgorWounds@users.noreply.github.com>
Co-authored-by: James Maslek <jmaslek11@gmail.com>
2024-05-10 13:39:28 +00:00

91 lines
3.3 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),
"command": obbject.extra.get("command", ""),
}
return obbjects
@property
def obbjects(self) -> List[OBBject]:
"""Return all obbjects in the registry"""
return self._obbjects