mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-10-30 14:02:06 +00:00
improvements on the argparse translator and wip to integrate the stocks.load
This commit is contained in:
@@ -42,9 +42,12 @@ class ArgparseTranslator:
|
||||
self.type_hints = get_type_hints(func)
|
||||
|
||||
self.parser = argparse.ArgumentParser(
|
||||
description=func.__doc__, formatter_class=argparse.RawTextHelpFormatter
|
||||
description=func.__doc__,
|
||||
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
|
||||
)
|
||||
|
||||
self._generate_argparse_arguments(self.signature.parameters)
|
||||
|
||||
@staticmethod
|
||||
def _param_is_default(param: inspect.Parameter) -> bool:
|
||||
return param.default != inspect.Parameter.empty
|
||||
@@ -176,6 +179,24 @@ class ArgparseTranslator:
|
||||
|
||||
return kwargs
|
||||
|
||||
def execute_func(
|
||||
self,
|
||||
args: Optional[argparse.Namespace] = None,
|
||||
) -> Any:
|
||||
kwargs = self._unflatten_args(vars(args))
|
||||
kwargs = self._update_with_custom_types(kwargs)
|
||||
return self.func(**kwargs)
|
||||
|
||||
def parse_args_and_execute(self) -> Any:
|
||||
"""
|
||||
Parses the arguments and executes the original function.
|
||||
|
||||
Returns:
|
||||
Any: The return value of the original function.
|
||||
"""
|
||||
args = self.parser.parse_args()
|
||||
return self.execute_func(args)
|
||||
|
||||
def translate(self) -> Callable:
|
||||
"""
|
||||
Wraps the original function with an argparse program.
|
||||
@@ -183,12 +204,20 @@ class ArgparseTranslator:
|
||||
Returns:
|
||||
Callable: The original function wrapped with an argparse program.
|
||||
"""
|
||||
self._generate_argparse_arguments(self.signature.parameters)
|
||||
|
||||
def wrapper_func():
|
||||
args = self.parser.parse_args()
|
||||
kwargs = self._unflatten_args(vars(args))
|
||||
kwargs = self._update_with_custom_types(kwargs)
|
||||
return self.func(**kwargs)
|
||||
return self.parse_args_and_execute()
|
||||
|
||||
return wrapper_func
|
||||
|
||||
|
||||
# def hello_world(name: str, age: int):
|
||||
# print(f"Hello {name}! You are {age} years old.")
|
||||
|
||||
|
||||
# translator = ArgparseTranslator(hello_world)
|
||||
# parser = translator.parser
|
||||
# parsed_args = parser.parse_args()
|
||||
# print(parsed_args)
|
||||
|
||||
# translator.execute_func(parsed_args)
|
||||
|
||||
@@ -31,6 +31,8 @@ from openbb_terminal.rich_config import MenuText, console
|
||||
from openbb_terminal.stocks import cboe_view, stocks_helper, stocks_view
|
||||
from openbb_terminal.terminal_helper import suppress_stdout
|
||||
|
||||
from argparse_translator.argparse_translator import ArgparseTranslator
|
||||
|
||||
# pylint: disable=R1710,import-outside-toplevel,R0913,R1702,no-member
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -150,6 +152,22 @@ class StocksController(StockBaseController):
|
||||
with suppress_stdout():
|
||||
self.call_load(other_args)
|
||||
|
||||
def call_load(self, other_args: List[str]):
|
||||
"""Process load command."""
|
||||
|
||||
def hello_world(name: str, age: int):
|
||||
print(f"Hello {name}! You are {age} years old.")
|
||||
|
||||
print(other_args)
|
||||
|
||||
translator = ArgparseTranslator(hello_world)
|
||||
parser = translator.parser
|
||||
|
||||
print("here")
|
||||
|
||||
if ns_parser := self.parse_known_args_and_warn(parser, other_args):
|
||||
print("not bad")
|
||||
|
||||
@log_start_end(log=logger)
|
||||
def call_search(self, other_args: List[str]):
|
||||
"""Process search command."""
|
||||
|
||||
Reference in New Issue
Block a user