mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-12-01 13:44:16 +00:00
paths feature
This commit is contained in:
@@ -27,10 +27,13 @@ class ArgparseClassProcessor:
|
||||
self._target_class: Type = target_class
|
||||
self._add_help: bool = add_help
|
||||
self._translators: Dict[str, ArgparseTranslator] = {}
|
||||
self._paths: Dict[str, str] = {}
|
||||
|
||||
self._translators = self._process_class(
|
||||
target=self._target_class, add_help=self._add_help
|
||||
)
|
||||
self._paths[self._get_class_name(self._target_class)] = "path"
|
||||
self._build_paths(target=self._target_class)
|
||||
|
||||
@property
|
||||
def translators(self) -> Dict[str, ArgparseTranslator]:
|
||||
@@ -44,6 +47,18 @@ class ArgparseClassProcessor:
|
||||
"""
|
||||
return self._translators
|
||||
|
||||
@property
|
||||
def paths(self) -> Dict[str, str]:
|
||||
"""
|
||||
Get the paths associated with the target class.
|
||||
|
||||
Returns
|
||||
-------
|
||||
Dict[str, str]
|
||||
The paths associated with the target class.
|
||||
"""
|
||||
return self._paths
|
||||
|
||||
@staticmethod
|
||||
def _process_class(
|
||||
target: type,
|
||||
@@ -55,12 +70,7 @@ class ArgparseClassProcessor:
|
||||
if name.startswith("__") or name.startswith("_"):
|
||||
continue
|
||||
if inspect.ismethod(member):
|
||||
class_name = (
|
||||
str(type(target))
|
||||
.rsplit(".", maxsplit=1)[-1]
|
||||
.replace("'>", "")
|
||||
.replace("ROUTER_", "")
|
||||
)
|
||||
class_name = ArgparseClassProcessor._get_class_name(target)
|
||||
methods[f"{class_name}_{name}"] = ArgparseTranslator(
|
||||
func=member, add_help=add_help
|
||||
)
|
||||
@@ -74,6 +84,15 @@ class ArgparseClassProcessor:
|
||||
|
||||
return methods
|
||||
|
||||
@staticmethod
|
||||
def _get_class_name(target: type) -> str:
|
||||
return (
|
||||
str(type(target))
|
||||
.rsplit(".", maxsplit=1)[-1]
|
||||
.replace("'>", "")
|
||||
.replace("ROUTER_", "")
|
||||
)
|
||||
|
||||
def get_translator(self, command: str) -> ArgparseTranslator:
|
||||
"""
|
||||
Retrieve the ArgparseTranslator object associated with a specific menu and command.
|
||||
@@ -89,3 +108,13 @@ class ArgparseClassProcessor:
|
||||
The ArgparseTranslator associated with the specified menu and command.
|
||||
"""
|
||||
return self._translators[command]
|
||||
|
||||
def _build_paths(self, target: type, depth: int = 1):
|
||||
for name, member in inspect.getmembers(target):
|
||||
if name.startswith("__") or name.startswith("_"):
|
||||
continue
|
||||
if inspect.ismethod(member):
|
||||
pass
|
||||
else:
|
||||
self._build_paths(target=getattr(target, name), depth=depth + 1)
|
||||
self._paths[f"{name}"] = "sub" * depth + "path"
|
||||
|
||||
@@ -69,7 +69,17 @@ class PocPlatformEquityController(BaseController):
|
||||
"""Print help."""
|
||||
mt = MenuText("poc_plat_equity/", 80)
|
||||
|
||||
for cmd in self.CHOICES_COMMANDS:
|
||||
mt.add_cmd(cmd)
|
||||
menus = equity.paths
|
||||
commands = self.CHOICES_COMMANDS.copy()
|
||||
|
||||
for path in menus:
|
||||
if equity.paths[path] == "path":
|
||||
continue
|
||||
mt.add_menu(path)
|
||||
new_commands = [command for command in commands if path in command]
|
||||
for command in new_commands:
|
||||
mt.add_cmd(command)
|
||||
|
||||
mt.add_raw("\n")
|
||||
|
||||
console.print(text=mt.menu_text, menu="POC Platform Equity")
|
||||
|
||||
Reference in New Issue
Block a user