Skip to content

Commit

Permalink
Core: move parse_simple_args (#3705)
Browse files Browse the repository at this point in the history
* Moving Docker folder into Build

* Workflows : update Docker

* Docker : update compose image version

* Docker : fix volumes path

* Update core

* Update helper_funcs

* Update dashboards

* Update reports

* Update keys_controller

* Update settings_controller

* Update sources_controller

* Tests : fix __mock_parse_simple_args

* Reduce lines

Co-authored-by: James Maslek <jmaslek11@gmail.com>
  • Loading branch information
Chavithra and jmaslek authored Dec 6, 2022
1 parent acb1577 commit cfde16e
Show file tree
Hide file tree
Showing 9 changed files with 220 additions and 220 deletions.
7 changes: 4 additions & 3 deletions openbb_terminal/core/completer/choices.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def __mock_parse_known_args_and_warn(

def __mock_parse_simple_args(parser: ArgumentParser, other_args: List[str]) -> None:
"""Add the arguments that would have normally added by:
- openbb_terminal.helper_funcs.parse_simple_args
- openbb_terminal.parent_classes.BaseController.parse_simple_args
Parameters
----------
Expand Down Expand Up @@ -199,8 +199,9 @@ def __patch_controller_functions(controller):
)

patcher_list = [
patch(
target="openbb_terminal.parent_classes.parse_simple_args",
patch.object(
target=controller,
attribute="parse_simple_args",
side_effect=__mock_parse_simple_args,
return_value=None,
),
Expand Down
225 changes: 115 additions & 110 deletions openbb_terminal/dashboards/dashboards_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from openbb_terminal.menu import session
from openbb_terminal.parent_classes import BaseController
from openbb_terminal.rich_config import console, MenuText
from openbb_terminal.helper_funcs import parse_simple_args

# pylint: disable=consider-using-with

Expand Down Expand Up @@ -69,146 +68,152 @@ def print_help(self):
@log_start_end(log=logger)
def call_stocks(self, other_args: List[str]):
"""Process stocks command"""
create_call_voila(other_args, "stocks", "stocks")
self.create_call_voila(other_args, "stocks", "stocks")

@log_start_end(log=logger)
def call_correlation(self, other_args: List[str]):
"""Process correlation command"""
create_call_voila(other_args, "correlation", "correlation")
self.create_call_voila(other_args, "correlation", "correlation")

@log_start_end(log=logger)
def call_vsurf(self, other_args: List[str]):
"""Process vsurf command"""
create_call_voila(other_args, "vsurf", "")
self.create_call_voila(other_args, "vsurf", "")

@log_start_end(log=logger)
def call_chains(self, other_args: List[str]):
"""Process chains command"""
create_call_voila(other_args, "chains", "")
self.create_call_voila(other_args, "chains", "")

@log_start_end(log=logger)
def call_shortdata(self, other_args: List[str]):
"""Process shortdata command"""
create_call_voila(other_args, "shortdata", "")
self.create_call_voila(other_args, "shortdata", "")

@log_start_end(log=logger)
def call_crypto(self, other_args: List[str]):
"""Process crypto command"""
create_call_voila(other_args, "crypto", "")
self.create_call_voila(other_args, "crypto", "")

@log_start_end(log=logger)
def call_futures(self, other_args: List[str]):
"""Process futures command"""
create_call_voila(other_args, "futures", "")
self.create_call_voila(other_args, "futures", "")

@log_start_end(log=logger)
def call_forecast(self, other_args: List[str]):
"""Process forecast command"""
create_call_voila(other_args, "forecast", "")
self.create_call_voila(other_args, "forecast", "")

@log_start_end(log=logger)
def call_forecasting(self, other_args: List[str]):
"""Process forecasting command"""
create_call_streamlit(other_args, "forecast")


def create_call_voila(other_args: List[str], name: str, filename: str = None) -> None:
filename = filename if filename else name

parser = argparse.ArgumentParser(
add_help=False,
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
prog=name,
description=f"""Shows {name} dashboard""",
)
parser.add_argument(
"-j",
"--jupyter",
action="store_true",
default=False,
dest="jupyter",
help="Shows dashboard in jupyter-lab.",
)
parser.add_argument(
"-n",
"--no-input",
action="store_true",
default=False,
dest="input",
help="Skips confirmation to run server.",
)
parser.add_argument(
"-d",
"--dark",
action="store_true",
default=False,
dest="dark",
help="Whether to show voila in dark mode",
)
ns_parser = parse_simple_args(parser, other_args)

if ns_parser:
cmd = "jupyter-lab" if ns_parser.jupyter else "voila"
base_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), "voila")
file = os.path.join(base_path, f"{filename}.ipynb")
if not ns_parser.input:
console.print(
f"Warning: opens a port on your computer to run a {cmd} server."
self.create_call_streamlit(other_args, "forecast")

@classmethod
def create_call_voila(
cls, other_args: List[str], name: str, filename: str = None
) -> None:
filename = filename if filename else name

parser = argparse.ArgumentParser(
add_help=False,
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
prog=name,
description=f"""Shows {name} dashboard""",
)
parser.add_argument(
"-j",
"--jupyter",
action="store_true",
default=False,
dest="jupyter",
help="Shows dashboard in jupyter-lab.",
)
parser.add_argument(
"-n",
"--no-input",
action="store_true",
default=False,
dest="input",
help="Skips confirmation to run server.",
)
parser.add_argument(
"-d",
"--dark",
action="store_true",
default=False,
dest="dark",
help="Whether to show voila in dark mode",
)
ns_parser = cls.parse_simple_args(parser, other_args)

if ns_parser:
cmd = "jupyter-lab" if ns_parser.jupyter else "voila"
base_path = os.path.join(
os.path.abspath(os.path.dirname(__file__)), "voila"
)
response = input("Would you like us to run the server for you [yn]? ")
args = ""
if ns_parser.dark and not ns_parser.jupyter:
args += "--theme=dark"
if ns_parser.input or response.lower() == "y":
cfg.LOGGING_SUPPRESS = True
subprocess.Popen(
f"{cmd} {file} {args}",
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
shell=True, # nosec
file = os.path.join(base_path, f"{filename}.ipynb")
if not ns_parser.input:
console.print(
f"Warning: opens a port on your computer to run a {cmd} server."
)
response = input("Would you like us to run the server for you [yn]? ")
args = ""
if ns_parser.dark and not ns_parser.jupyter:
args += "--theme=dark"
if ns_parser.input or response.lower() == "y":
cfg.LOGGING_SUPPRESS = True
subprocess.Popen(
f"{cmd} {file} {args}",
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
shell=True, # nosec
)
cfg.LOGGING_SUPPRESS = False
else:
console.print(f"Type: {cmd} voila/{file}\ninto a terminal to run.")

@classmethod
def create_call_streamlit(
cls, other_args: List[str], name: str, filename: str = None
) -> None:
filename = filename if filename else name

parser = argparse.ArgumentParser(
add_help=False,
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
prog=name,
description=f"""Shows streamlit {name} dashboard""",
)
parser.add_argument(
"-n",
"--no-input",
action="store_true",
default=False,
dest="input",
help="Skips confirmation to run server.",
)
ns_parser = cls.parse_simple_args(parser, other_args)

if ns_parser:
cmd = "streamlit run"
base_path = os.path.join(
os.path.abspath(os.path.dirname(__file__)), "stream"
)
cfg.LOGGING_SUPPRESS = False
else:
console.print(f"Type: {cmd} voila/{file}\ninto a terminal to run.")


def create_call_streamlit(
other_args: List[str], name: str, filename: str = None
) -> None:
filename = filename if filename else name

parser = argparse.ArgumentParser(
add_help=False,
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
prog=name,
description=f"""Shows streamlit {name} dashboard""",
)
parser.add_argument(
"-n",
"--no-input",
action="store_true",
default=False,
dest="input",
help="Skips confirmation to run server.",
)
ns_parser = parse_simple_args(parser, other_args)

if ns_parser:
cmd = "streamlit run"
base_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), "stream")
file = os.path.join(base_path, f"{filename}.py")
if not ns_parser.input:
console.print(
f"Warning: opens a port on your computer to run a {cmd} server."
)
response = input("Would you like us to run the server for you [yn]? ")
args = ""
if ns_parser.input or response.lower() == "y":
subprocess.Popen(
f"{cmd} {file} {args}",
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
shell=True,
)
else:
console.print(f"Type: {cmd} stream/{file}\ninto a terminal to run.")
file = os.path.join(base_path, f"{filename}.py")
if not ns_parser.input:
console.print(
f"Warning: opens a port on your computer to run a {cmd} server."
)
response = input("Would you like us to run the server for you [yn]? ")
args = ""
if ns_parser.input or response.lower() == "y":
subprocess.Popen(
f"{cmd} {file} {args}",
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
shell=True,
)
else:
console.print(f"Type: {cmd} stream/{file}\ninto a terminal to run.")
41 changes: 0 additions & 41 deletions openbb_terminal/helper_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -966,47 +966,6 @@ def patch_pandas_text_adjustment():
pandas.io.formats.format.TextAdjustment.adjoin = text_adjustment_adjoin


def parse_simple_args(parser: argparse.ArgumentParser, other_args: List[str]):
"""Parse list of arguments into the supplied parser.
Parameters
----------
parser: argparse.ArgumentParser
Parser with predefined arguments
other_args: List[str]
List of arguments to parse
Returns
-------
ns_parser:
Namespace with parsed arguments
"""
parser.add_argument(
"-h", "--help", action="store_true", help="show this help message"
)

if obbff.USE_CLEAR_AFTER_CMD:
system_clear()

try:
(ns_parser, l_unknown_args) = parser.parse_known_args(other_args)
except SystemExit:
# In case the command has required argument that isn't specified
console.print("\n")
return None

if ns_parser.help:
txt_help = parser.format_help()
console.print(f"[help]{txt_help}[/help]")
return None

if l_unknown_args:
console.print(f"The following args couldn't be interpreted: {l_unknown_args}")
console.print("\n")

return ns_parser


def lambda_financials_colored_values(val: str) -> str:
"""Add a color to a value."""
if val == "N/A" or str(val) == "nan":
Expand Down
Loading

0 comments on commit cfde16e

Please sign in to comment.