Discovery menu refactor part 1

This commit is contained in:
Artem Veremey
2021-03-28 12:17:55 -07:00
parent 9fa3386bac
commit 6c6c2d9ce6
16 changed files with 192 additions and 151 deletions

View File

@@ -1 +1 @@
from . import disc_menu # noqa: F401
from . import disc_controller # noqa: F401

View File

@@ -0,0 +1,185 @@
import argparse
from typing import List
from gamestonk_terminal import feature_flags as gtff
from gamestonk_terminal.helper_funcs import get_flair
from gamestonk_terminal.menu import session
from gamestonk_terminal.discovery import (
alpha_vantage_view,
ark_view,
fidelity_view,
finviz_view,
seeking_alpha_view,
short_interest_view,
simply_wallst_view,
spachero_view,
unusual_whales_view,
yahoo_finance_view,
)
from prompt_toolkit.completion import NestedCompleter
class DiscoveryController:
""" Discovery Controller """
# Command choices
CHOICES = [
"help",
"q",
"quit",
"map",
"sectors",
"gainers",
"losers",
"orders",
"ark_orders",
"up_earnings",
"high_short",
"low_float",
"simply_wallst",
"spachero",
"uwhales",
]
def __init__(self):
"""Constructor"""
self.disc_parser = argparse.ArgumentParser(add_help=False, prog="disc")
self.disc_parser.add_argument(
"cmd",
choices=self.CHOICES,
)
@staticmethod
def print_help():
""" Print help """
print("\nDiscovery Mode:")
print(" help show this discovery menu again")
print(" q quit this menu, and shows back to main menu")
print(" quit quit to abandon program")
print("")
print(" map S&P500 index stocks map [Finviz]")
print(" sectors show sectors performance [Alpha Vantage]")
print(" gainers show latest top gainers [Yahoo Finance]")
print(" losers show latest top losers [Yahoo Finance]")
print(" orders orders by Fidelity Customers [Fidelity]")
print(" ark_orders orders by ARK Investment Management LLC")
print(" up_earnings upcoming earnings release dates [Seeking Alpha]")
print(
" high_short show top high short interest stocks of over 20% ratio [www.highshortinterest.com]"
)
print(
" low_float show low float stocks under 10M shares float [www.lowfloat.com]"
)
print(" simply_wallst Simply Wall St. research data [Simply Wall St.]")
print(" spachero great website for SPACs research [SpacHero]")
print(" uwhales good website for SPACs research [UnusualWhales]")
print("")
def switch(self, an_input: str):
"""Process and dispatch input
Returns
-------
True, False or None
False - quit the menu
True - quit the program
None - continue in the menu
"""
(known_args, other_args) = self.disc_parser.parse_known_args(an_input.split())
return getattr(
self, "call_" + known_args.cmd, lambda: "Command not recognized!"
)(other_args)
def call_help(self, _):
"""Process Help command"""
self.print_help()
def call_q(self, _):
"""Process Q command - quit the menu"""
return False
def call_quit(self, _):
"""Process Quit command - quit the program"""
return True
def call_map(self, other_args: List[str]):
"""Process map command"""
finviz_view.map_sp500(other_args)
def call_sectors(self, other_args: List[str]):
"""Process sectors command"""
alpha_vantage_view.sectors(other_args)
def call_gainers(self, other_args: List[str]):
"""Process gainers command"""
yahoo_finance_view.gainers(other_args)
def call_losers(self, other_args: List[str]):
"""Process losers command"""
yahoo_finance_view.losers(other_args)
def call_orders(self, other_args: List[str]):
"""Process orders command"""
fidelity_view.orders(other_args)
def call_ark_orders(self, other_args: List[str]):
"""Process ark_orders command"""
ark_view.ark_orders(other_args)
def call_up_earnings(self, other_args: List[str]):
"""Process up_earnings command"""
seeking_alpha_view.earnings_release_dates(other_args)
def call_high_short(self, other_args: List[str]):
"""Process high_short command"""
short_interest_view.high_short_interest(other_args)
def call_low_float(self, other_args: List[str]):
"""Process low_float command"""
short_interest_view.low_float(other_args)
def call_simply_wallst(self, other_args: List[str]):
"""Process simply_wallst command"""
simply_wallst_view.simply_wallst(other_args)
def call_spachero(self, other_args: List[str]):
"""Process spachero command"""
spachero_view.spachero(other_args)
def call_uwhales(self, other_args: List[str]):
"""Process uwhales command"""
unusual_whales_view.unusual_whales(other_args)
def menu():
"""Discovery Menu"""
disc_controller = DiscoveryController()
disc_controller.call_help(None)
# Loop forever and ever
while True:
# Get input command from user
if session and gtff.USE_PROMPT_TOOLKIT:
completer = NestedCompleter.from_nested_dict(
{c: None for c in disc_controller.CHOICES}
)
an_input = session.prompt(
f"{get_flair()} (disc)> ",
completer=completer,
)
else:
an_input = input(f"{get_flair()} (disc)> ")
try:
process_input = disc_controller.switch(an_input)
if process_input is not None:
return process_input
except SystemExit:
print("The command selected doesn't exist\n")
continue

View File

@@ -1,144 +0,0 @@
import argparse
from gamestonk_terminal.discovery import (
alpha_vantage_api,
ark_api,
fidelity_api,
finviz_api,
seeking_alpha_api,
short_interest_api,
simply_wallst_api,
spachero_api,
unusual_whales_api,
yahoo_finance_api,
)
from gamestonk_terminal import feature_flags as gtff
from gamestonk_terminal.helper_funcs import get_flair
from gamestonk_terminal.menu import session
from prompt_toolkit.completion import NestedCompleter
def print_discovery():
""" Print help """
print("\nDiscovery Mode:")
print(" help show this discovery menu again")
print(" q quit this menu, and shows back to main menu")
print(" quit quit to abandon program")
print("")
print(" map S&P500 index stocks map [Finviz]")
print(" sectors show sectors performance [Alpha Vantage]")
print(" gainers show latest top gainers [Yahoo Finance]")
print(" losers show latest top losers [Yahoo Finance]")
print(" orders orders by Fidelity Customers [Fidelity]")
print(" ark_orders orders by ARK Investment Management LLC")
print(" up_earnings upcoming earnings release dates [Seeking Alpha]")
print(
" high_short show top high short interest stocks of over 20% ratio [www.highshortinterest.com]"
)
print(
" low_float show low float stocks under 10M shares float [www.lowfloat.com]"
)
print(" simply_wallst Simply Wall St. research data [Simply Wall St.]")
print(" spachero great website for SPACs research [SpacHero]")
print(" uwhales good website for SPACs research [UnusualWhales]")
print("")
return
def disc_menu():
# Add list of arguments that the discovery parser accepts
disc_parser = argparse.ArgumentParser(add_help=False, prog="discovery")
choices = [
"help",
"q",
"quit",
"map",
"sectors",
"gainers",
"losers",
"spacs",
"orders",
"ark_orders",
"spachero",
"high_short",
"low_float",
"up_earnings",
"simply_wallst",
"uwhales",
"mill",
]
disc_parser.add_argument("cmd", choices=choices)
completer = NestedCompleter.from_nested_dict({c: None for c in choices})
print_discovery()
# Loop forever and ever
while True:
# Get input command from user
if session and gtff.USE_PROMPT_TOOLKIT:
as_input = session.prompt(
f"{get_flair()} (disc)> ",
completer=completer,
)
else:
as_input = input(f"{get_flair()} (disc)> ")
# Parse fundamental analysis command of the list of possible commands
try:
(ns_known_args, l_args) = disc_parser.parse_known_args(as_input.split())
except SystemExit:
print("The command selected doesn't exist\n")
continue
if ns_known_args.cmd == "help":
print_discovery()
elif ns_known_args.cmd == "q":
# Just leave the DISC menu
return False
elif ns_known_args.cmd == "quit":
# Abandon the program
return True
elif ns_known_args.cmd == "map":
finviz_api.map_sp500(l_args)
elif ns_known_args.cmd == "sectors":
alpha_vantage_api.sectors(l_args)
elif ns_known_args.cmd == "gainers":
yahoo_finance_api.gainers(l_args)
elif ns_known_args.cmd == "losers":
yahoo_finance_api.losers(l_args)
elif ns_known_args.cmd == "spachero":
spachero_api.spachero(l_args)
elif ns_known_args.cmd == "uwhales":
unusual_whales_api.unusual_whales(l_args)
elif ns_known_args.cmd == "orders":
fidelity_api.orders(l_args)
elif ns_known_args.cmd == "ark_orders":
ark_api.ark_orders(l_args)
elif ns_known_args.cmd == "simply_wallst":
simply_wallst_api.simply_wallst(l_args)
elif ns_known_args.cmd == "up_earnings":
seeking_alpha_api.earnings_release_dates(l_args)
elif ns_known_args.cmd == "high_short":
short_interest_api.high_short_interest(l_args)
elif ns_known_args.cmd == "low_float":
short_interest_api.low_float(l_args)
else:
print("Command not recognized!")

View File

@@ -14,7 +14,7 @@ from gamestonk_terminal import config_terminal as cfg
from gamestonk_terminal import feature_flags as gtff
from gamestonk_terminal import thought_of_the_day as thought
from gamestonk_terminal import res_menu as rm
from gamestonk_terminal.discovery import disc_menu as dm
from gamestonk_terminal.discovery import disc_controller
from gamestonk_terminal.due_diligence import dd_menu as ddm
from gamestonk_terminal.fundamental_analysis import fa_menu as fam
from gamestonk_terminal.helper_funcs import b_is_stock_market_open, get_flair
@@ -184,7 +184,7 @@ def main():
main_cmd = True
elif ns_known_args.opt == "disc":
b_quit = dm.disc_menu()
b_quit = disc_controller.menu()
elif ns_known_args.opt == "mill":
b_quit = mill.papermill_menu()

View File

@@ -1,7 +1,7 @@
""" discovery/fidelity_api.py tests """
import unittest
from gamestonk_terminal.discovery.fidelity_api import (
from gamestonk_terminal.discovery.fidelity_view import (
buy_sell_ratio_color_red_green,
price_change_color_red_green,
orders,

View File

@@ -8,7 +8,7 @@ from gamestonk_terminal.test_helper import ( # noqa: F401
pytest_generate_tests,
)
from gamestonk_terminal.discovery.short_interest_api import (
from gamestonk_terminal.discovery.short_interest_view import (
high_short_interest,
low_float,
)
@@ -17,7 +17,7 @@ assertions = unittest.TestCase("__init__")
class TestDiscoveryShortInterestApi:
@mock.patch("gamestonk_terminal.discovery.short_interest_api.requests")
@mock.patch("gamestonk_terminal.discovery.short_interest_view.requests")
@parameterize_from_file(
"test_high_short_interest",
"../tests/data/discovery_short_interest_api.yaml",
@@ -29,7 +29,7 @@ class TestDiscoveryShortInterestApi:
mock_request_get.get().text = mock_high_short
high_short_interest(["-n", "10"])
@mock.patch("gamestonk_terminal.discovery.short_interest_api.requests")
@mock.patch("gamestonk_terminal.discovery.short_interest_view.requests")
@parameterize_from_file(
"test_low_float",
"../tests/data/discovery_short_interest_api.yaml",