diff --git a/gamestonk_terminal/discovery/__init__.py b/gamestonk_terminal/discovery/__init__.py index a1605dd4a10..0c8c0d157a7 100644 --- a/gamestonk_terminal/discovery/__init__.py +++ b/gamestonk_terminal/discovery/__init__.py @@ -1 +1 @@ -from . import disc_menu # noqa: F401 +from . import disc_controller # noqa: F401 diff --git a/gamestonk_terminal/discovery/alpha_vantage_api.py b/gamestonk_terminal/discovery/alpha_vantage_view.py similarity index 100% rename from gamestonk_terminal/discovery/alpha_vantage_api.py rename to gamestonk_terminal/discovery/alpha_vantage_view.py diff --git a/gamestonk_terminal/discovery/ark_api.py b/gamestonk_terminal/discovery/ark_view.py similarity index 100% rename from gamestonk_terminal/discovery/ark_api.py rename to gamestonk_terminal/discovery/ark_view.py diff --git a/gamestonk_terminal/discovery/disc_controller.py b/gamestonk_terminal/discovery/disc_controller.py new file mode 100644 index 00000000000..fb754300b51 --- /dev/null +++ b/gamestonk_terminal/discovery/disc_controller.py @@ -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 diff --git a/gamestonk_terminal/discovery/disc_menu.py b/gamestonk_terminal/discovery/disc_menu.py deleted file mode 100644 index 130a32b3bc2..00000000000 --- a/gamestonk_terminal/discovery/disc_menu.py +++ /dev/null @@ -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!") diff --git a/gamestonk_terminal/discovery/fidelity_api.py b/gamestonk_terminal/discovery/fidelity_view.py similarity index 100% rename from gamestonk_terminal/discovery/fidelity_api.py rename to gamestonk_terminal/discovery/fidelity_view.py diff --git a/gamestonk_terminal/discovery/finviz_api.py b/gamestonk_terminal/discovery/finviz_view.py similarity index 100% rename from gamestonk_terminal/discovery/finviz_api.py rename to gamestonk_terminal/discovery/finviz_view.py diff --git a/gamestonk_terminal/discovery/seeking_alpha_api.py b/gamestonk_terminal/discovery/seeking_alpha_view.py similarity index 100% rename from gamestonk_terminal/discovery/seeking_alpha_api.py rename to gamestonk_terminal/discovery/seeking_alpha_view.py diff --git a/gamestonk_terminal/discovery/short_interest_api.py b/gamestonk_terminal/discovery/short_interest_view.py similarity index 100% rename from gamestonk_terminal/discovery/short_interest_api.py rename to gamestonk_terminal/discovery/short_interest_view.py diff --git a/gamestonk_terminal/discovery/simply_wallst_api.py b/gamestonk_terminal/discovery/simply_wallst_view.py similarity index 100% rename from gamestonk_terminal/discovery/simply_wallst_api.py rename to gamestonk_terminal/discovery/simply_wallst_view.py diff --git a/gamestonk_terminal/discovery/spachero_api.py b/gamestonk_terminal/discovery/spachero_view.py similarity index 100% rename from gamestonk_terminal/discovery/spachero_api.py rename to gamestonk_terminal/discovery/spachero_view.py diff --git a/gamestonk_terminal/discovery/unusual_whales_api.py b/gamestonk_terminal/discovery/unusual_whales_view.py similarity index 100% rename from gamestonk_terminal/discovery/unusual_whales_api.py rename to gamestonk_terminal/discovery/unusual_whales_view.py diff --git a/gamestonk_terminal/discovery/yahoo_finance_api.py b/gamestonk_terminal/discovery/yahoo_finance_view.py similarity index 100% rename from gamestonk_terminal/discovery/yahoo_finance_api.py rename to gamestonk_terminal/discovery/yahoo_finance_view.py diff --git a/terminal.py b/terminal.py index 125be2ffc1a..040e1914398 100644 --- a/terminal.py +++ b/terminal.py @@ -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() diff --git a/tests/test_discovery_fidelity_api.py b/tests/test_discovery_fidelity_api.py index ec06fe607db..ac6850610a8 100644 --- a/tests/test_discovery_fidelity_api.py +++ b/tests/test_discovery_fidelity_api.py @@ -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, diff --git a/tests/test_discovery_short_interest_api.py b/tests/test_discovery_short_interest_api.py index ced87574ca0..20a80788a42 100644 --- a/tests/test_discovery_short_interest_api.py +++ b/tests/test_discovery_short_interest_api.py @@ -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",