File size: 2,098 Bytes
b93364a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
"""Platform controller factory to create a platform controller."""
from typing import Dict, List, Union
from openbb_cli.argparse_translator.argparse_class_processor import (
ArgparseClassProcessor,
)
from openbb_cli.controllers.base_platform_controller import PlatformController
class PlatformControllerFactory:
"""Factory to create a platform controller."""
def __init__(self, platform_router: type, **kwargs):
"""Create the controller name."""
self.platform_router = platform_router
self._translated_target = ArgparseClassProcessor(
target_class=self.platform_router, reference=kwargs.get("reference", {})
)
self.router_name = (
str(type(self.platform_router))
.rsplit(".", maxsplit=1)[-1]
.replace("'>", "")
.replace("ROUTER_", "")
.lower()
)
self.controller_name = f"{self.router_name.capitalize()}Controller"
def create(self) -> type:
"""Create the platform controller."""
ClassName = self.controller_name
Parents = (PlatformController,)
Attributes: Dict[str, Union[bool, List[str]]] = {"CHOICES_GENERATION": True}
# Menu and Command choices generation
choices_menus: List[str] = []
choices_commands: List[str] = []
translators = self._translated_target.translators
paths = self._translated_target.paths
# menus
for key, value in paths.items():
if value == "path":
continue
choices_menus.append(key)
# commands
for name, _ in translators.items():
if any(f"{self.router_name}_{path}" in name for path in paths):
continue
new_name = name.replace(f"{self.router_name}_", "")
choices_commands.append(new_name)
Attributes["CHOICES_MENUS"] = choices_menus
Attributes["CHOICES_COMMANDS"] = choices_commands
# Use type to create the class
DynamicClass = type(ClassName, Parents, Attributes)
return DynamicClass
|