Skip to content

Commit

Permalink
Merge branch 'develop' into bugfix/pywry-plot-paperbg
Browse files Browse the repository at this point in the history
  • Loading branch information
deeleeramone authored May 10, 2024
2 parents 6700cce + 13a6248 commit 9d84973
Show file tree
Hide file tree
Showing 95 changed files with 9,550 additions and 24,591 deletions.
1 change: 0 additions & 1 deletion cli/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -1323,7 +1323,6 @@ This class contains both important variables and methods that are common across

- `cls`: clear screen
- `home`: go back to the main root
- `about`: allows to open our documentation directly on the menu or command
- `h`, `?` and `help`: display the help menu the user is in
- `q`, `quit` and `..`: go back to one menu above
- `exit`: exit the platform
Expand Down
16 changes: 13 additions & 3 deletions cli/openbb_cli/argparse_translator/argparse_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def __init__(
self.func = func
self.signature = inspect.signature(func)
self.type_hints = get_type_hints(func)
self.provider_parameters: List[str] = []
self.provider_parameters: Dict[str, List[str]] = {}

self._parser = argparse.ArgumentParser(
prog=func.__name__,
Expand All @@ -218,6 +218,7 @@ def __init__(

if custom_argument_groups:
for group in custom_argument_groups:
self.provider_parameters[group.name] = []
argparse_group = self._parser.add_argument_group(group.name)
for argument in group.arguments:
self._handle_argument_in_groups(argument, argparse_group)
Expand Down Expand Up @@ -278,7 +279,8 @@ def _update_providers(
if f"--{argument.name}" not in self._parser._option_string_actions:
kwargs = argument.model_dump(exclude={"name"}, exclude_none=True)
group.add_argument(f"--{argument.name}", **kwargs)
self.provider_parameters.append(argument.name)
if group.title in self.provider_parameters:
self.provider_parameters[group.title].append(argument.name)

else:
kwargs = argument.model_dump(exclude={"name"}, exclude_none=True)
Expand Down Expand Up @@ -582,11 +584,19 @@ def execute_func(
kwargs = self._unflatten_args(vars(parsed_args))
kwargs = self._update_with_custom_types(kwargs)

provider = kwargs.get("provider")
provider_args = []
if provider and provider in self.provider_parameters:
provider_args = self.provider_parameters[provider]
else:
for args in self.provider_parameters.values():
provider_args.extend(args)

# remove kwargs that doesn't match the signature or provider parameters
kwargs = {
key: value
for key, value in kwargs.items()
if key in self.signature.parameters or key in self.provider_parameters
if key in self.signature.parameters or key in provider_args
}

return self.func(**kwargs)
Expand Down
1 change: 1 addition & 0 deletions cli/openbb_cli/argparse_translator/obbject_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def _handle_data_repr(obbject: OBBject) -> str:
"provider": obbject.provider,
"standard params": _handle_standard_params(obbject),
"data": _handle_data_repr(obbject),
"command": obbject.extra.get("command", ""),
}

return obbjects
Expand Down
8 changes: 3 additions & 5 deletions cli/openbb_cli/assets/i18n/en.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
en:
intro: introduction on the OpenBB Platform CLI
about: discover the capabilities of the OpenBB Platform CLI (https://openbb.co/docs)
support: pre-populate a support ticket for our team to evaluate
survey: fill in our 2-minute survey so we better understand how we can improve the CLI
settings: enable and disable feature flags, preferences and settings
Expand All @@ -10,8 +9,8 @@ en:
exe: execute .openbb routine scripts (use exe --example for an example)
_configure_: Configure your own CLI
_main_menu_: Main menu
settings/_info_: Feature flags
settings/_settings_: Settings and preferences
settings/_feature_flags_: Feature flags
settings/_preferences_: Preferences
settings/retryload: retry misspelled commands with load first
settings/interactive: open dataframes in interactive window
settings/cls: clear console after each command
Expand All @@ -20,7 +19,6 @@ en:
settings/thoughts: thoughts of the day
settings/reporthtml: open report as HTML otherwise notebook
settings/exithelp: automatically print help when quitting menu
settings/rcontext: remember contexts loaded params during session
settings/rich: colorful rich CLI
settings/richpanel: colorful rich CLI panel
settings/watermark: watermark in figures
Expand All @@ -31,8 +29,8 @@ en:
settings/console_style: apply a custom rich style to the CLI
settings/flair: choose flair icon
settings/timezone: pick timezone
settings/language: translation language
settings/n_rows: number of rows to show on non interactive tables
settings/n_cols: number of columns to show on non interactive tables
settings/obbject_msg: show obbject registry message after a new result is added
settings/obbject_res: define the maximum number of obbjects allowed in the registry
settings/obbject_display: define the maximum number of cached results to display on the help menu
4 changes: 2 additions & 2 deletions cli/openbb_cli/config/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
":mercury": "(☿)",
":hidden": "",
":sun": "(☼)",
":moon": "()",
":moon": "(🌕)",
":nuke": "(☢)",
":hazard": "(☣)",
":tunder": "(☈)",
Expand All @@ -76,6 +76,6 @@
":scales": "(⚖)",
":ball": "(⚽)",
":golf": "(⛳)",
":piece": "(☮)",
":peace": "(☮)",
":yy": "(☯)",
}
25 changes: 21 additions & 4 deletions cli/openbb_cli/config/menu_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
class MenuText:
"""Create menu text with rich colors to be displayed by CLI."""

CMD_NAME_LENGTH = 18
CMD_NAME_LENGTH = 23
CMD_DESCRIPTION_LENGTH = 65
CMD_PROVIDERS_LENGTH = 23
SECTION_SPACING = 4
Expand Down Expand Up @@ -64,9 +64,7 @@ def _get_providers(command_path: str) -> List:
def _format_cmd_name(self, name: str) -> str:
"""Truncate command name length if it is too long."""
if len(name) > self.CMD_NAME_LENGTH:
new_name = name[
: self.CMD_NAME_LENGTH
] # Default to trimming to 18 characters
new_name = name[: self.CMD_NAME_LENGTH]

if "_" in name:
name_split = name.split("_")
Expand Down Expand Up @@ -108,6 +106,22 @@ def add_raw(self, text: str):
"""Append raw text (without translation)."""
self.menu_text += text

def add_section(
self, text: str, description: str = "", leading_new_line: bool = False
):
"""Append raw text (without translation)."""
spacing = (self.CMD_NAME_LENGTH - len(text) + self.SECTION_SPACING) * " "
left_spacing = self.SECTION_SPACING * " "
text = f"{left_spacing}{text}"
if description:
text = f"{text}{spacing}{description}\n"

if leading_new_line:
self.menu_text += "\n" + text

else:
self.menu_text += text

def add_custom(self, name: str):
"""Append custom text (after translation)."""
self.menu_text += f"{i18n.t(self.menu_path + name)}"
Expand Down Expand Up @@ -165,6 +179,9 @@ def add_menu(
if description == self.menu_path + name:
description = ""

if len(description) > self.CMD_DESCRIPTION_LENGTH:
description = description[: self.CMD_DESCRIPTION_LENGTH - 3] + "..."

menu = f"{name}{spacing}{description}"
tag = "unvl" if disable else "menu"
self.menu_text += f"[{tag}]> {menu}[/{tag}]\n"
Expand Down
3 changes: 0 additions & 3 deletions cli/openbb_cli/config/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import i18n

from openbb_cli.config.constants import ENV_FILE_SETTINGS, I18N_FILE, SETTINGS_DIRECTORY
from openbb_cli.session import Session

if TYPE_CHECKING:
from openbb_charting.core.openbb_figure import OpenBBFigure
Expand Down Expand Up @@ -81,5 +80,3 @@ def bootstrap():
"""Setup pre-launch configurations for the CLI."""
SETTINGS_DIRECTORY.mkdir(parents=True, exist_ok=True)
Path(ENV_FILE_SETTINGS).touch(exist_ok=True)

setup_i18n(lang=Session().settings.USE_LANGUAGE)
21 changes: 6 additions & 15 deletions cli/openbb_cli/controllers/base_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ class BaseController(metaclass=ABCMeta):
CHOICES_COMMON = [
"cls",
"home",
"about",
"h",
"?",
"help",
Expand Down Expand Up @@ -496,14 +495,7 @@ def call_record(self, other_args) -> None:
help="Whether the routine should be public or not",
default=False,
)
parser.add_argument(
"-l",
"--local",
dest="local",
action="store_true",
help="Only save the routine locally - this is necessary if you are running in guest mode.",
default=False,
)

if other_args and "-" not in other_args[0][0]:
other_args.insert(0, "-n")

Expand Down Expand Up @@ -549,17 +541,16 @@ def call_record(self, other_args) -> None:
)
return

if session.is_local() and not ns_parser.local:
if session.is_local():
session.console.print(
"[red]Recording session to the OpenBB Hub is not supported in guest mode.[/red]"
)
session.console.print(
"\n[yellow]Sign to OpenBB Hub to register: http://openbb.co[/yellow]"
"\n[yellow]Visit the OpenBB Hub to register: http://my.openbb.co[/yellow]"
)
session.console.print(
"\n[yellow]Otherwise set the flag '-l' to save the file locally.[/yellow]"
"\n[yellow]Your routine will be saved locally.[/yellow]\n"
)
return

# Check if title has a valid format
title = " ".join(ns_parser.name) if ns_parser.name else ""
Expand All @@ -577,8 +568,8 @@ def call_record(self, other_args) -> None:
global SESSION_RECORDED_TAGS # noqa: PLW0603
global SESSION_RECORDED_PUBLIC # noqa: PLW0603

RECORD_SESSION_LOCAL_ONLY = session.is_local()
RECORD_SESSION = True
RECORD_SESSION_LOCAL_ONLY = ns_parser.local
SESSION_RECORDED_NAME = title
SESSION_RECORDED_DESCRIPTION = (
" ".join(ns_parser.description)
Expand Down Expand Up @@ -871,6 +862,7 @@ def parse_known_args_and_warn(
type=check_file_type_saved(choices_export),
dest="export",
help=help_export,
nargs="+",
)

# If excel is an option, add the sheet name
Expand Down Expand Up @@ -1001,7 +993,6 @@ def menu(self, custom_path_menu_above: str = ""):
'<style bg="ansiblack" fg="ansiwhite">[e]</style> exit the program '
'<style bg="ansiblack" fg="ansiwhite">[cmd -h]</style> '
"see usage and available options "
f'<style bg="ansiblack" fg="ansiwhite">[about (cmd/menu)]</style> '
f"{self.path[-1].capitalize()} (cmd/menu) Documentation"
),
style=Style.from_dict(
Expand Down
63 changes: 45 additions & 18 deletions cli/openbb_cli/controllers/base_platform_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from openbb_cli.controllers.base_controller import BaseController
from openbb_cli.controllers.utils import export_data, print_rich_table
from openbb_cli.session import Session
from openbb_core.app.model.obbject import OBBject

session = Session()

Expand Down Expand Up @@ -74,17 +75,20 @@ def _link_obbject_to_data_processing_commands(self):
for _, trl in self.translators.items():
for action in trl._parser._actions: # pylint: disable=protected-access
if action.dest == "data":
action.choices = range(len(session.obbject_registry.obbjects))
action.type = int
action.choices = [
"OBB" + str(i)
for i in range(len(session.obbject_registry.obbjects))
]
action.type = str
action.nargs = None

def _intersect_data_processing_commands(self, ns_parser):
"""Intersect data processing commands and change the obbject id into an actual obbject."""
if hasattr(ns_parser, "data") and ns_parser.data in range(
len(session.obbject_registry.obbjects)
):
obbject = session.obbject_registry.get(ns_parser.data)
setattr(ns_parser, "data", obbject.results)
if hasattr(ns_parser, "data"):
ns_parser.data = int(ns_parser.data.replace("OBB", ""))
if ns_parser.data in range(len(session.obbject_registry.obbjects)):
obbject = session.obbject_registry.get(ns_parser.data)
setattr(ns_parser, "data", obbject.results)

return ns_parser

Expand Down Expand Up @@ -154,21 +158,22 @@ def method(self, other_args: List[str], translator=translator):
title = f"{self.PATH}{translator.func.__name__}"

if obbject:
max_obbjects_exceeded = (
len(session.obbject_registry.obbjects)
>= session.settings.N_TO_KEEP_OBBJECT_REGISTRY
)
if max_obbjects_exceeded:
if session.max_obbjects_exceeded():
session.obbject_registry.remove()

# use the obbject to store the command so we can display it later on results
obbject.extra["command"] = f"{title} {' '.join(other_args)}"

session.obbject_registry.register(obbject)
# we need to force to re-link so that the new obbject
# is immediately available for data processing commands
self._link_obbject_to_data_processing_commands()
# also update the completer
self.update_completer(self.choices_default)

if session.settings.SHOW_MSG_OBBJECT_REGISTRY:
if session.settings.SHOW_MSG_OBBJECT_REGISTRY and isinstance(
obbject, OBBject
):
session.console.print("Added OBBject to registry.")

if hasattr(ns_parser, "chart") and ns_parser.chart:
Expand Down Expand Up @@ -197,15 +202,15 @@ def method(self, other_args: List[str], translator=translator):
if hasattr(ns_parser, "export") and ns_parser.export:
sheet_name = getattr(ns_parser, "sheet_name", None)
export_data(
export_type=ns_parser.export,
export_type=",".join(ns_parser.export),
dir_path=os.path.dirname(os.path.abspath(__file__)),
func_name=translator.func.__name__,
df=df,
sheet_name=sheet_name,
figure=fig,
)

if max_obbjects_exceeded:
if session.max_obbjects_exceeded():
session.console.print(
"[yellow]\nMaximum number of OBBjects reached. The oldest entry was removed.[yellow]"
)
Expand Down Expand Up @@ -271,13 +276,26 @@ def _get_command_description(self, command: str) -> str:

def _get_menu_description(self, menu: str) -> str:
"""Get menu description."""

def _get_sub_menu_commands():
"""Get sub menu commands."""
sub_path = f"{self.PATH[1:].replace('/','_')}{menu}"
commands = []
for trl in self.translators:
if sub_path in trl:
commands.append(trl.replace(f"{sub_path}_", ""))
return commands

menu_description = (
obb.reference["routers"] # type: ignore
.get(f"{self.PATH}{menu}", {})
.get("description", "")
) or ""
if menu_description:
return menu_description.split(".")[0].lower()

return menu_description.split(".")[0].lower()
# If no description is found, return the sub menu commands
return ", ".join(_get_sub_menu_commands())

def print_help(self):
"""Print help."""
Expand All @@ -288,16 +306,25 @@ def print_help(self):
description = self._get_menu_description(menu)
mt.add_menu(name=menu, description=description)

if self.CHOICES_COMMANDS:
mt.add_raw("\n")

if self.CHOICES_COMMANDS:
mt.add_raw("\n")
for command in self.CHOICES_COMMANDS:
command_description = self._get_command_description(command)
mt.add_cmd(
name=command.replace(f"{self._name}_", ""),
description=command_description,
)

session.console.print(text=mt.menu_text, menu=self._name)
if session.obbject_registry.obbjects:
mt.add_section("Cached Results:\n", leading_new_line=True)
for key, value in list(session.obbject_registry.all.items())[
: session.settings.N_TO_DISPLAY_OBBJECT_REGISTRY
]:
mt.add_raw(f"\tOBB{key}: {value['command']}\n")

session.console.print(text=mt.menu_text, menu=self.PATH)

settings = session.settings
dev_mode = settings.DEBUG_MODE or settings.TEST_MODE
Expand Down
Loading

0 comments on commit 9d84973

Please sign in to comment.