mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-11-09 10:51:57 +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]
|
||||
Reference in New Issue
Block a user