mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-10-28 04:52:07 +00:00
Allow special characters in autocompletion and improve sources controller (#4698)
* allow empty spaces between sources commands * allow special characters in autocomplete * update parse input * repeated character * improve sources controller * pylint? * help was not working on set * display file only if exists and has dat * lowercase * skip ultima test * update default sources instead of replacing * change var names * revert, need to think better about this * Update test_ultima_newsmonitor_model.py --------- Co-authored-by: Henrique Joaquim <h.joaquim@campus.fct.unl.pt>
This commit is contained in:
@@ -47,7 +47,7 @@ class WordCompleter(Completer):
|
||||
ignore_case: bool = False,
|
||||
display_dict: Optional[Mapping[str, AnyFormattedText]] = None,
|
||||
meta_dict: Optional[Mapping[str, AnyFormattedText]] = None,
|
||||
WORD: bool = False,
|
||||
WORD: bool = True,
|
||||
sentence: bool = False,
|
||||
match_middle: bool = False,
|
||||
pattern: Optional[Pattern[str]] = None,
|
||||
|
||||
@@ -4,6 +4,7 @@ __docformat__ = "numpy"
|
||||
# IMPORTATION STANDARD
|
||||
import argparse
|
||||
import logging
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import List, Optional
|
||||
|
||||
@@ -41,34 +42,39 @@ class SourcesController(BaseController):
|
||||
def __init__(self, queue: Optional[List[str]] = None):
|
||||
"""Constructor"""
|
||||
super().__init__(queue)
|
||||
|
||||
self.source = "default"
|
||||
if session and get_current_user().preferences.USE_PROMPT_TOOLKIT:
|
||||
self.choices: dict = self.choices_default
|
||||
self.completer = NestedCompleter.from_nested_dict(self.choices)
|
||||
|
||||
def update_print_help(self):
|
||||
"""Update print help"""
|
||||
sources_file = get_current_user().preferences.USER_DATA_SOURCES_FILE
|
||||
if is_local():
|
||||
if Path(sources_file).exists() and os.stat(sources_file).st_size > 0:
|
||||
self.source = sources_file
|
||||
else:
|
||||
self.source = SOURCES_URL
|
||||
|
||||
def parse_input(self, an_input: str) -> List:
|
||||
"""Parse controller input
|
||||
|
||||
Overrides the parent class function to handle github org/repo path convention.
|
||||
See `BaseController.parse_input()` for details.
|
||||
"""
|
||||
cmd_filter = r"((set\ --cmd |set\ -c |get\ --cmd |get\ -c ).*?("
|
||||
cmd_filter = r"((set\s+--cmd\s+|set\s+-c\s+|set\s+|get\s+--cmd\s+|get\s+-c\s+|get\s+).*?("
|
||||
for cmd in get_current_user().sources.sources_dict:
|
||||
cmd = cmd.replace("/", r"\/")
|
||||
cmd_filter += f"{cmd}|"
|
||||
cmd_filter += ")*)"
|
||||
|
||||
commands = parse_and_split_input(an_input=an_input, custom_filters=[cmd_filter])
|
||||
return commands
|
||||
|
||||
def print_help(self):
|
||||
"""Print help"""
|
||||
mt = MenuText("sources/")
|
||||
mt.add_param(
|
||||
"_source",
|
||||
get_current_user().preferences.USER_DATA_SOURCES_FILE
|
||||
if is_local()
|
||||
else SOURCES_URL,
|
||||
)
|
||||
mt.add_param("_source", self.source)
|
||||
mt.add_raw("\n")
|
||||
mt.add_info("_info_")
|
||||
mt.add_cmd("get")
|
||||
@@ -90,7 +96,7 @@ class SourcesController(BaseController):
|
||||
action="store",
|
||||
dest="cmd",
|
||||
choices=list(get_current_user().sources.sources_dict.keys()),
|
||||
required="-h" not in other_args,
|
||||
required="-h" not in other_args and "--help" not in other_args,
|
||||
help="Command that we want to check the available data sources and the default one.",
|
||||
metavar="COMMAND",
|
||||
)
|
||||
@@ -109,6 +115,7 @@ class SourcesController(BaseController):
|
||||
f"[param]Default :[/param] {cmd_defaults[0]}\n"
|
||||
f"[param]Available :[/param] {', '.join(cmd_defaults)}"
|
||||
)
|
||||
self.update_print_help()
|
||||
|
||||
@log_start_end(log=logger)
|
||||
def call_set(self, other_args):
|
||||
@@ -140,6 +147,15 @@ class SourcesController(BaseController):
|
||||
)
|
||||
if other_args and "-" not in other_args[0][0]:
|
||||
other_args.insert(0, "-c")
|
||||
|
||||
if (
|
||||
other_args
|
||||
and len(other_args) >= 2
|
||||
and "-s" not in other_args
|
||||
and "--source" not in other_args
|
||||
):
|
||||
other_args.insert(2, "-s")
|
||||
|
||||
ns_parser = self.parse_simple_args(parser, other_args)
|
||||
if ns_parser:
|
||||
sources_dict = get_current_user().sources.sources_dict
|
||||
@@ -172,3 +188,4 @@ class SourcesController(BaseController):
|
||||
f"'{ns_parser.cmd}' command.[/red]\n"
|
||||
f"[param]\nAvailable :[/param] {', '.join(sources_dict[ns_parser.cmd])}\n"
|
||||
)
|
||||
self.update_print_help()
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
"""Main helper."""
|
||||
__docformat__ = "numpy"
|
||||
|
||||
# pylint: disable=unsupported-assignment-operation,C0302
|
||||
# pylint: disable=no-member,too-many-branches,too-many-arguments
|
||||
# pylint: disable=too-many-lines, unsupported-assignment-operation
|
||||
# pylint: disable=no-member, too-many-branches, too-many-arguments
|
||||
# pylint: disable=inconsistent-return-statements
|
||||
# pylint: disable=consider-using-dict-items
|
||||
|
||||
|
||||
Reference in New Issue
Block a user