diff options
author | hjoaquim <h.joaquim@campus.fct.unl.pt> | 2024-05-03 17:19:07 +0100 |
---|---|---|
committer | hjoaquim <h.joaquim@campus.fct.unl.pt> | 2024-05-03 17:19:07 +0100 |
commit | 73fd87fdf6cdd30b7802d15a04da040cac171c25 (patch) | |
tree | d2897b1d1878c45126d2452f57ed5375fadf6284 | |
parent | 18115e4e91adb6632dabee7bd8577f08798efa08 (diff) |
registry belongs to the session instead
-rw-r--r-- | cli/openbb_cli/argparse_translator/obbject_registry.py | 29 | ||||
-rw-r--r-- | cli/openbb_cli/controllers/base_platform_controller.py | 9 | ||||
-rw-r--r-- | cli/openbb_cli/controllers/cli_controller.py | 3 | ||||
-rw-r--r-- | cli/openbb_cli/session.py | 7 |
4 files changed, 29 insertions, 19 deletions
diff --git a/cli/openbb_cli/argparse_translator/obbject_registry.py b/cli/openbb_cli/argparse_translator/obbject_registry.py index cb1643747b3..f6a96bbee32 100644 --- a/cli/openbb_cli/argparse_translator/obbject_registry.py +++ b/cli/openbb_cli/argparse_translator/obbject_registry.py @@ -3,33 +3,33 @@ import json from typing import Dict, List -from openbb_core.app.model.abstract.singleton import SingletonMeta from openbb_core.app.model.obbject import OBBject -class Registry(metaclass=SingletonMeta): +class Registry: + """Registry for OBBjects.""" - obbjects: List[OBBject] = [] + 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) - @classmethod - def register(cls, obbject: OBBject): + def register(self, obbject: OBBject): """Designed to add an OBBject instance to the registry.""" - if isinstance(obbject, OBBject) and not cls._contains_obbject( - obbject.id, cls.obbjects + if isinstance(obbject, OBBject) and not self._contains_obbject( + obbject.id, self._obbjects ): - cls.obbjects.append(obbject) + self._obbjects.append(obbject) - @classmethod - def get(cls, idx: int) -> 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(cls.obbjects)) + reversed_list = list(reversed(self._obbjects)) return reversed_list[idx] @property @@ -65,7 +65,7 @@ class Registry(metaclass=SingletonMeta): return data_repr obbjects = {} - for i, obbject in enumerate(list(reversed(self.obbjects))): + for i, obbject in enumerate(list(reversed(self._obbjects))): obbjects[i] = { "route": obbject._route, # pylint: disable=protected-access "provider": obbject.provider, @@ -74,3 +74,8 @@ class Registry(metaclass=SingletonMeta): } return obbjects + + @property + def obbjects(self) -> List[OBBject]: + """Return all obbjects in the registry""" + return self._obbjects diff --git a/cli/openbb_cli/controllers/base_platform_controller.py b/cli/openbb_cli/controllers/base_platform_controller.py index 0fc55c0ff1c..3fd8bce3f75 100644 --- a/cli/openbb_cli/controllers/base_platform_controller.py +++ b/cli/openbb_cli/controllers/base_platform_controller.py @@ -11,7 +11,6 @@ from openbb_charting.core.openbb_figure import OpenBBFigure from openbb_cli.argparse_translator.argparse_class_processor import ( ArgparseClassProcessor, ) -from openbb_cli.argparse_translator.obbject_registry import Registry from openbb_cli.config.completer import NestedCompleter from openbb_cli.config.menu_text import MenuText from openbb_cli.controllers.base_controller import BaseController @@ -79,16 +78,16 @@ class PlatformController(BaseController): for _, trl in self.translators.items(): for action in trl._parser._actions: # pylint: disable=protected-access if action.dest == "data": - action.choices = range(len(Registry.obbjects)) + action.choices = range(len(session.obbject_registry.obbjects)) action.type = int action.nargs = None def _intersect_data_processing_commands(self, ns_parser): """Intersect data processing commands and change the obbject id into an actual obbject.""" if hasattr(ns_parser, "data") and ns_parser.data in range( - len(Registry.obbjects) + len(session.obbject_registry.obbjects) ): - obbject = Registry.get(ns_parser.data) + obbject = session.obbject_registry.get(ns_parser.data) setattr(ns_parser, "data", obbject.results) return ns_parser @@ -159,7 +158,7 @@ class PlatformController(BaseController): title = f"{self.PATH}{translator.func.__name__}" if obbject: - Registry.register(obbject) + session.obbject_registry.register(obbject) if hasattr(ns_parser, "chart") and ns_parser.chart: obbject.show() diff --git a/cli/openbb_cli/controllers/cli_controller.py b/cli/openbb_cli/controllers/cli_controller.py index e82a5dbdfc0..61e2af1b885 100644 --- a/cli/openbb_cli/controllers/cli_controller.py +++ b/cli/openbb_cli/controllers/cli_controller.py @@ -20,7 +20,6 @@ import certifi import pandas as pd import requests from openbb import obb -from openbb_cli.argparse_translator.obbject_registry import Registry from openbb_cli.config import constants from openbb_cli.config.completer import NestedCompleter from openbb_cli.config.constants import ( @@ -477,7 +476,7 @@ class CLIController(BaseController): def call_results(self, _): """Process results command.""" - results = Registry().all + results = session.obbject_registry.all if results: df = pd.DataFrame.from_dict(results, orient="index") print_rich_table( diff --git a/cli/openbb_cli/session.py b/cli/openbb_cli/session.py index ef63fd6e7f0..be2a7938aa3 100644 --- a/cli/openbb_cli/session.py +++ b/cli/openbb_cli/session.py @@ -9,6 +9,7 @@ from openbb_core.app.model.abstract.singleton import SingletonMeta from openbb_core.app.model.user_settings import UserSettings as User from prompt_toolkit import PromptSession +from openbb_cli.argparse_translator.obbject_registry import Registry from openbb_cli.config.completer import CustomFileHistory from openbb_cli.config.console import Console from openbb_cli.config.constants import HIST_FILE_PROMPT @@ -31,6 +32,7 @@ class Session(metaclass=SingletonMeta): settings=self._settings, style=self._style.console_style ) self._prompt_session = self._get_prompt_session() + self._obbject_registry = Registry() @property def user(self) -> User: @@ -53,6 +55,11 @@ class Session(metaclass=SingletonMeta): return self._console @property + def obbject_registry(self) -> Registry: + """Get obbject registry.""" + return self._obbject_registry + + @property def prompt_session(self) -> Optional[PromptSession]: """Get prompt session.""" return self._prompt_session |