mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-10-31 22:41:19 +00:00
first iteration with a more generic class that only translates all methods once
This commit is contained in:
54
argparse_translator/argparse_class_processor.py
Normal file
54
argparse_translator/argparse_class_processor.py
Normal file
@@ -0,0 +1,54 @@
|
||||
import inspect
|
||||
from typing import Type
|
||||
|
||||
from argparse_translator.argparse_translator import ArgparseTranslator
|
||||
|
||||
|
||||
class ArgparseClassProcessor:
|
||||
"""
|
||||
Process a target class to create ArgparseTranslators for its methods.
|
||||
"""
|
||||
|
||||
def __init__(self, target_class: Type, menu_designation: str):
|
||||
"""
|
||||
Initialize the ArgparseClassProcessor.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
target_class : Type
|
||||
The target class whose methods will be processed.
|
||||
menu_designation : str
|
||||
The designation for the menu level associated with the target class.
|
||||
"""
|
||||
self._target_class = target_class
|
||||
self._menu_designation = menu_designation
|
||||
self._translators = {self._menu_designation: {}}
|
||||
|
||||
self._process_methods()
|
||||
|
||||
def _process_methods(self):
|
||||
"""
|
||||
Process the methods of the target class to create ArgparseTranslators.
|
||||
"""
|
||||
for name, method in inspect.getmembers(self._target_class, inspect.ismethod):
|
||||
if name.startswith("__"):
|
||||
continue
|
||||
self._translators[self._menu_designation][name] = ArgparseTranslator(method)
|
||||
|
||||
def get_translator(self, menu: str, command: str) -> ArgparseTranslator:
|
||||
"""
|
||||
Retrieve the ArgparseTranslator object associated with a specific menu and command.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
menu : str
|
||||
The menu designation.
|
||||
command : str
|
||||
The command associated with the ArgparseTranslator.
|
||||
|
||||
Returns
|
||||
-------
|
||||
ArgparseTranslator
|
||||
The ArgparseTranslator associated with the specified menu and command.
|
||||
"""
|
||||
return self._translators[menu][command]
|
||||
@@ -190,7 +190,7 @@ class ArgparseTranslator:
|
||||
kwargs["choices"] = choices
|
||||
|
||||
if param_type == bool:
|
||||
# store_true action does not accept the bellow kwargs
|
||||
# store_true action does not accept the below kwargs
|
||||
kwargs.pop("type")
|
||||
kwargs.pop("nargs")
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ from typing import List, Optional
|
||||
|
||||
import financedatabase as fd
|
||||
|
||||
from argparse_translator.argparse_translator import ArgparseTranslator
|
||||
from argparse_translator.argparse_class_processor import ArgparseClassProcessor
|
||||
|
||||
# pylint: disable=R1710,import-outside-toplevel,R0913,R1702,no-member
|
||||
from openbb_sdk.openbb import obb
|
||||
@@ -37,6 +37,8 @@ from openbb_terminal.terminal_helper import suppress_stdout
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
stocks_translations = ArgparseClassProcessor(obb.stocks, "stocks")
|
||||
|
||||
|
||||
class StocksController(StockBaseController):
|
||||
"""Stocks Controller class."""
|
||||
@@ -155,7 +157,7 @@ class StocksController(StockBaseController):
|
||||
def call_load(self, other_args: List[str]):
|
||||
"""Process load command."""
|
||||
|
||||
translator = ArgparseTranslator(func=obb.stocks.load, add_help=False)
|
||||
translator = stocks_translations.get_translator(menu="stocks", command="load")
|
||||
parser = translator.parser
|
||||
|
||||
if ns_parser := self.parse_known_args_and_warn(parser, other_args):
|
||||
@@ -167,14 +169,14 @@ class StocksController(StockBaseController):
|
||||
self.ticker = ns_parser.symbol
|
||||
self.stock = c_out.to_dataframe()
|
||||
|
||||
# TODO : temporary workarround
|
||||
# TODO : temporary workaround
|
||||
# core gives all columns lower case and terminal expects 1st letter upper case
|
||||
# upper case first letter of all columns
|
||||
self.stock.columns = [
|
||||
x[0].upper() + x[1:] for x in self.stock.columns.tolist()
|
||||
]
|
||||
|
||||
c_out.show()
|
||||
if ns_parser.chart:
|
||||
c_out.show()
|
||||
|
||||
@log_start_end(log=logger)
|
||||
def call_search(self, other_args: List[str]):
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
import sys
|
||||
from multiprocessing import freeze_support
|
||||
|
||||
# TODO : importing the sdk-core here trying to get the obb object to be available
|
||||
# so it doesn't take a lot when accessing a menu for the first time
|
||||
# is it good or is it bad?
|
||||
from openbb_sdk.openbb import obb
|
||||
import openbb_terminal.config_terminal as cfg
|
||||
|
||||
# pylint:disable=unused-import,import-outside-toplevel
|
||||
|
||||
Reference in New Issue
Block a user