diff --git a/i18n/en.yml b/i18n/en.yml index 631ad9bddacb..f2a3878a3515 100644 --- a/i18n/en.yml +++ b/i18n/en.yml @@ -11,6 +11,8 @@ en: sources: select your preferred data sources keys: set API keys and check their validity wiki: search for an expression in Wikipedia (https://www.wikipedia.org/) + record: record session to be converted into a .openbb routine + stop: stop session recording _configure_: Configure your own terminal _main_menu_: Main menu keys/_keys_: This menu allows you to set your own API keys. Type 'about' for more information diff --git a/openbb_terminal/parent_classes.py b/openbb_terminal/parent_classes.py index 3d7d5163444e..a3ce78c70494 100644 --- a/openbb_terminal/parent_classes.py +++ b/openbb_terminal/parent_classes.py @@ -1,7 +1,7 @@ """Parent Classes""" __docformat__ = "numpy" -# pylint: disable= C0301,C0302,R0902 +# pylint: disable=C0301,C0302,R0902,global-statement from abc import ABCMeta, abstractmethod @@ -22,7 +22,10 @@ import pandas as pd import numpy as np -from openbb_terminal.core.config.paths import CUSTOM_IMPORTS_DIRECTORY +from openbb_terminal.core.config.paths import ( + CUSTOM_IMPORTS_DIRECTORY, + ROUTINES_DIRECTORY, +) from openbb_terminal.decorators import log_start_end from openbb_terminal.menu import session @@ -65,6 +68,10 @@ SUPPORT_TYPE = ["bug", "suggestion", "question", "generic"] +RECORD_SESSION = False +SESSION_RECORDED = list() +SESSION_RECORDED_NAME = "" + class BaseController(metaclass=ABCMeta): CHOICES_COMMON = [ @@ -82,6 +89,8 @@ class BaseController(metaclass=ABCMeta): "reset", "support", "wiki", + "record", + "stop", ] CHOICES_COMMANDS: List[str] = [] @@ -286,6 +295,9 @@ def switch(self, an_input: str) -> List[str]: else: (known_args, other_args) = self.parser.parse_known_args(an_input.split()) + if RECORD_SESSION: + SESSION_RECORDED.append(an_input) + # Redirect commands to their correct functions if known_args.cmd: if known_args.cmd in ("..", "q"): @@ -469,14 +481,12 @@ def call_support(self, other_args: List[str]) -> None: @log_start_end(log=logger) def call_wiki(self, other_args: List[str]) -> None: """Process wiki command""" - parser = argparse.ArgumentParser( add_help=False, formatter_class=argparse.ArgumentDefaultsHelpFormatter, prog="wiki", description="Search Wikipedia", ) - parser.add_argument( "--expression", "-e", @@ -487,7 +497,6 @@ def call_wiki(self, other_args: List[str]) -> None: default="", help="Expression to search for", ) - if other_args and "-" not in other_args[0][0]: other_args.insert(0, "-e") @@ -498,6 +507,77 @@ def call_wiki(self, other_args: List[str]) -> None: expression = " ".join(ns_parser.expression) search_wikipedia(expression) + @log_start_end(log=logger) + def call_record(self, other_args) -> None: + """Process record command""" + parser = argparse.ArgumentParser( + add_help=False, + formatter_class=argparse.ArgumentDefaultsHelpFormatter, + prog="record", + description="Start recording session into .openbb routine file", + ) + parser.add_argument( + "-r", + "--routine", + action="store", + dest="routine_name", + type=str, + default=datetime.now().strftime("%Y%m%d_%H%M%S_routine.openbb"), + help="Routine file name to be saved.", + ) + if other_args and "-" not in other_args[0][0]: + other_args.insert(0, "-r") + ns_parser = parse_simple_args(parser, other_args) + + if ns_parser: + global SESSION_RECORDED_NAME + global RECORD_SESSION + if ".openbb" in ns_parser.routine_name: + SESSION_RECORDED_NAME = ns_parser.routine_name + else: + SESSION_RECORDED_NAME = ns_parser.routine_name + ".openbb" + + console.print( + "[green]The session is successfully being recorded. Remember to 'stop' before exiting terminal!\n[/green]" + ) + RECORD_SESSION = True + + @log_start_end(log=logger) + def call_stop(self, _) -> None: + """Process stop command""" + global RECORD_SESSION + global SESSION_RECORDED + + if not RECORD_SESSION: + console.print( + "[red]There is no session being recorded. Start one using 'record'[/red]\n" + ) + elif not SESSION_RECORDED: + console.print( + "[red]There is no session to be saved. Run at least 1 command after starting 'record'[/red]\n" + ) + else: + routine_file = os.path.join(ROUTINES_DIRECTORY, SESSION_RECORDED_NAME) + + if os.path.isfile(routine_file): + routine_file = os.path.join( + ROUTINES_DIRECTORY, + datetime.now().strftime("%Y%m%d_%H%M%S_") + SESSION_RECORDED_NAME, + ) + + # Writing to file + with open(routine_file, "w") as file1: + # Writing data to a file + file1.writelines([c + "\n\n" for c in SESSION_RECORDED[:-1]]) + + console.print( + f"[green]Your routine has been recorded and saved here: {routine_file}[/green]\n" + ) + + # Clear session to be recorded again + RECORD_SESSION = False + SESSION_RECORDED = list() + def parse_known_args_and_warn( self, parser: argparse.ArgumentParser, diff --git a/terminal.py b/terminal.py index 47f22b288c85..7dda583b240f 100644 --- a/terminal.py +++ b/terminal.py @@ -133,6 +133,8 @@ def print_help(self): mt.add_cmd("survey") mt.add_cmd("update") mt.add_cmd("wiki") + mt.add_cmd("record") + mt.add_cmd("stop") mt.add_raw("\n") mt.add_info("_configure_") mt.add_menu("keys")