Skip to content

Commit

Permalink
[BugFix] Revamp the usage of list syntax on routines (#6450)
Browse files Browse the repository at this point in the history
* using list syntax on routines

* black

* ruff

* simplify

* black
  • Loading branch information
hjoaquim authored May 22, 2024
1 parent 8baf4d7 commit f91791d
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ def _process_class(
if name.startswith("__") or name.startswith("_"):
continue
if inspect.ismethod(member):

class_name = cls._get_class_name(target)
methods[f"{class_name}_{name}"] = ArgparseTranslator(
func=member,
Expand Down
3 changes: 0 additions & 3 deletions cli/openbb_cli/argparse_translator/argparse_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ class ArgparseActionType(Enum):


class CustomArgument(BaseModel):

name: str
type: Optional[Any]
dest: str
Expand Down Expand Up @@ -62,7 +61,6 @@ def remove_props_on_store_true(cls, values: "CustomArgument"):

# override
def model_dump(self, **kwargs):

res = super().model_dump(**kwargs)

# Check if choices is present and if it's an empty tuple remove it
Expand Down Expand Up @@ -152,7 +150,6 @@ def _get_choices(self, type_: str, custom_choices: Any) -> Tuple:
def build_custom_groups(self):
"""Build the custom groups from the reference."""
for route, v in self.reference.items():

for provider, args in v["parameters"].items():
if provider == "standard":
continue
Expand Down
21 changes: 16 additions & 5 deletions cli/openbb_cli/controllers/base_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -799,11 +799,22 @@ def parse_known_args_and_warn(
return None

try:
# If the user uses a comma separated list of arguments, split them
for index, arg in enumerate(other_args):
if "," in arg:
parts = arg.split(",")
other_args[index : index + 1] = parts
# Determine the index of the routine arguments
routine_args_index = next(
(
i + 1
for i, arg in enumerate(other_args)
if arg in ("-i", "--input")
and "routine_args" in [action.dest for action in parser._actions]
),
-1,
)
# Split comma-separated arguments, except for the argument at routine_args_index
other_args = [
part
for index, arg in enumerate(other_args)
for part in (arg.split(",") if index != routine_args_index else [arg])
]

(ns_parser, l_unknown_args) = parser.parse_known_args(other_args)

Expand Down
1 change: 0 additions & 1 deletion cli/openbb_cli/controllers/base_platform_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ def method(self, other_args: List[str], translator=translator):
title = f"{self.PATH}{translator.func.__name__}"

if obbject:

if isinstance(obbject, OBBject):
if session.max_obbjects_exceeded() and obbject.results:
session.obbject_registry.remove()
Expand Down
22 changes: 13 additions & 9 deletions cli/openbb_cli/controllers/cli_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ def call_exe(self, other_args: List[str]):
"--input",
help="Select multiple inputs to be replaced in the routine and separated by commas. E.g. GME,AMC,BTC-USD",
dest="routine_args",
type=lambda s: [str(item) for item in s.split(",")],
type=str,
)
parser.add_argument(
"-e",
Expand Down Expand Up @@ -431,17 +431,22 @@ def call_exe(self, other_args: List[str]):
with open(routine_path) as fp:
raw_lines = list(fp)

script_inputs = []
# Capture ARGV either as list if args separated by commas or as single value
if ns_parser.routine_args:
script_inputs = (
ns_parser.routine_args
if "," not in ns_parser.routine_args
else ns_parser.routine_args.split(",")
if routine_args := ns_parser.routine_args:
pattern = r"\[(.*?)\]"
matches = re.findall(pattern, routine_args)

for match in matches:
routine_args = routine_args.replace(f"[{match}]", "")
script_inputs.append(match)

script_inputs.extend(
[val for val in routine_args.split(",") if val]
)

err, parsed_script = parse_openbb_script(
raw_lines=raw_lines,
script_inputs=script_inputs if ns_parser.routine_args else None,
raw_lines=raw_lines, script_inputs=script_inputs
)

# If there err output is not an empty string then it means there was an
Expand Down Expand Up @@ -546,7 +551,6 @@ def run_cli(jobs_cmds: Optional[List[str]] = None, test_mode=False):
t_controller.print_help()

while ret_code:

# There is a command in the queue
if t_controller.queue and len(t_controller.queue) > 0:
# If the command is quitting the menu we want to return in here
Expand Down
1 change: 0 additions & 1 deletion cli/tests/test_controllers_settings_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
@pytest.fixture
def mock_session():
with patch("openbb_cli.controllers.settings_controller.session") as mock:

mock.settings.USE_INTERACTIVE_DF = False
mock.settings.ALLOWED_NUMBER_OF_ROWS = 20
mock.settings.TIMEZONE = "UTC"
Expand Down
2 changes: 1 addition & 1 deletion ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ force-wrap-aliases = true

[lint.pylint]
max-args = 8
max-branches = 25
max-branches = 26
max-returns = 9
max-statements = 30

Expand Down

0 comments on commit f91791d

Please sign in to comment.