Skip to content

Commit

Permalink
Add record session into routine capabiltiies (#2702)
Browse files Browse the repository at this point in the history
* add record session into routine capabiltiies

* remove unnecessary imports

* small fix

* ignore global statement

Co-authored-by: Colin Delahunty <72827203+colin99d@users.noreply.github.com>
  • Loading branch information
DidierRLopes and colin99d authored Oct 4, 2022
1 parent 2c454f8 commit c268d85
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 5 deletions.
2 changes: 2 additions & 0 deletions i18n/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
90 changes: 85 additions & 5 deletions openbb_terminal/parent_classes.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 = [
Expand All @@ -82,6 +89,8 @@ class BaseController(metaclass=ABCMeta):
"reset",
"support",
"wiki",
"record",
"stop",
]

CHOICES_COMMANDS: List[str] = []
Expand Down Expand Up @@ -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"):
Expand Down Expand Up @@ -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",
Expand All @@ -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")

Expand All @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit c268d85

Please sign in to comment.