mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-11-24 18:22:43 +00:00
Add record session into routine capabiltiies (#2702)
* 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>
This commit is contained in:
2
i18n/en.yml
vendored
2
i18n/en.yml
vendored
@@ -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
|
||||
|
||||
@@ -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 @@ from rich.markdown import Markdown
|
||||
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 @@ CRYPTO_SOURCES = {
|
||||
|
||||
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 @@ class BaseController(metaclass=ABCMeta):
|
||||
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 @@ class BaseController(metaclass=ABCMeta):
|
||||
@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 @@ class BaseController(metaclass=ABCMeta):
|
||||
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 @@ class BaseController(metaclass=ABCMeta):
|
||||
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,
|
||||
|
||||
@@ -133,6 +133,8 @@ class TerminalController(BaseController):
|
||||
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")
|
||||
|
||||
Reference in New Issue
Block a user