reduce code duplication

This commit is contained in:
Diogo Sousa
2023-07-07 11:55:25 +01:00
parent ee1d77a68d
commit fc6e995a15
3 changed files with 40 additions and 29 deletions

View File

@@ -25,6 +25,7 @@ try:
from openbb_sdk_core.app.command_runner import CommandRunnerSession
from openbb_sdk_core.app.model.system_settings import SystemSettings
from openbb_sdk_core.app.model.user_settings import UserSettings
# from openbb_sdk_core.app.router import CommandMap
from openbb_sdk_core.app.static.account import Account
from openbb_sdk_core.app.static.package.MODULE_4ebd0208_8328_5d69_8c44_ec50939c0967 import (

View File

@@ -172,10 +172,8 @@ class ImportDefinition:
code += "\nfrom pydantic import validate_arguments"
code += "\nfrom inspect import Parameter"
code += "\nfrom typing import List, Dict, Union, Optional, Literal"
code += "\nimport warnings"
code += "\nimport builtins"
code += "\nfrom openbb_sdk_core.app.utils import df_to_basemodel"
code += "\nfrom openbb_sdk_core.app.model.abstract.warning import OpenBBWarning"
code += "\nfrom utils import parse_command_input, parse_command_output\n"
module_list = [hint_type.__module__ for hint_type in hint_type_list]
module_list = list(set(module_list))
@@ -373,22 +371,7 @@ class MethodDefinition:
parameter_map = dict(sig.parameters)
parameter_map.pop("cc", None)
code = ""
if "data" in parameter_map:
code += " if isinstance(data, pandas.DataFrame):\n"
code += " data = df_to_basemodel(data, data.index.name is not None)\n"
code += "\n"
# TODO: Uncomment when ready
# if "provider_choices" in parameter_map:
# code += " if provider is None:\n"
# code += " defaults = self._command_runner_session.user_settings.defaults.endpoints.get(\n"
# code += f' "{path}",\n'
# code += " None,\n"
# code += " )\n"
# code += ' provider = defaults.get("provider", None) if defaults else Parameter.empty\n'
# code += "\n"
code = " parse_command_input(locals())\n"
code += " o = self._command_runner_session.run(\n"
code += f""" "{path}",\n"""
@@ -406,16 +389,7 @@ class MethodDefinition:
code += f" {name}={name},\n"
code += " ).output\n"
code += "\n"
code += " if o.warnings:\n"
code += " for w in o.warnings:\n"
code += (
" category = getattr(builtins, w.category, OpenBBWarning)\n"
)
code += " warnings.warn(w.message, category)\n"
code += "\n"
code += " if o.error:\n"
code += " raise Exception(o.error.message)\n"
code += " return o\n"
code += " return parse_command_output(o)\n"
return code

View File

@@ -0,0 +1,36 @@
import builtins
import warnings
from openbb_sdk_core.app.model.abstract.warning import OpenBBWarning
from openbb_sdk_core.app.model.command_output import CommandOutput
def parse_command_input(arguments: dict) -> dict:
return arguments
# if "data" in parameter_map:
# code += " if isinstance(data, pandas.DataFrame):\n"
# code += " data = df_to_basemodel(data, data.index.name is not None)\n"
# code += "\n"
# TODO: Uncomment when ready
# if "provider_choices" in parameter_map:
# code += " if provider is None:\n"
# code += " defaults = self._command_runner_session.user_settings.defaults.endpoints.get(\n"
# code += f' "{path}",\n'
# code += " None,\n"
# code += " )\n"
# code += ' provider = defaults.get("provider", None) if defaults else Parameter.empty\n'
# code += "\n"
def parse_command_output(command_output: CommandOutput) -> CommandOutput:
if command_output.warnings:
for w in command_output.warnings:
category = getattr(builtins, w.category, OpenBBWarning)
warnings.warn(w.message, category)
if command_output.error:
raise Exception(command_output.error.message)
return command_output