Skip to content

Commit

Permalink
Moving scripts and routines into openbb_terminal folder. (Open…
Browse files Browse the repository at this point in the history
  • Loading branch information
Chavithra authored Oct 17, 2022
1 parent 081ccd2 commit 77106c2
Show file tree
Hide file tree
Showing 86 changed files with 109 additions and 78 deletions.
1 change: 0 additions & 1 deletion build/pyinstaller/terminal.spec
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ set_key(default_env_file, "OPENBB_LOGGING_COMMIT_HASH", str(commit_hash))
# Files that are explicitly pulled into the bundle
added_files = [
(os.path.join(os.getcwd(), "openbb_terminal"), "openbb_terminal"),
(os.path.join(os.getcwd(), "routines"), "routines"),
(os.path.join(pathex, "property_cached"), "property_cached"),
(os.path.join(pathex, "user_agent"), "user_agent"),
(os.path.join(pathex, "vaderSentiment"), "vaderSentiment"),
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
This folder introduces the concept of routines, which are meant for the user to develop and run at once. The notebook reports is a better approach to this idea, but currently the development of this is not trivial. On the other hand, the development of this is trivial as long as the user is familiar with our terminal interface.

Try out with:
```

```bash
python terminal.py routines/example.openbb
```

Or within the Terminal, use:
```

```bash
exe routines/example.openbb
```
```
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
9 changes: 9 additions & 0 deletions openbb_terminal/miscellaneous/scripts/test_stocks.openbb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
stocks
search apple
load aapl -s 2022-10-01
tob
tob -e EDGX
candle
codes
news
exit
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
169 changes: 95 additions & 74 deletions openbb_terminal/terminal_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import difflib
import logging
import os
from pathlib import Path
import platform
import sys
import webbrowser
Expand All @@ -21,10 +22,11 @@
from openbb_terminal import feature_flags as obbff

from openbb_terminal.core.config.paths import (
REPOSITORY_DIRECTORY,
USER_ENV_FILE,
REPOSITORY_ENV_FILE,
HOME_DIRECTORY,
MISCELLANEOUS_DIRECTORY,
REPOSITORY_ENV_FILE,
USER_DATA_DIRECTORY,
USER_ENV_FILE,
USER_ROUTINES_DIRECTORY,
)
from openbb_terminal.core.log.generation.path_tracking_file_handler import (
Expand Down Expand Up @@ -116,7 +118,7 @@ def update_runtime_choices(self):
"""Update runtime choices"""
self.ROUTINE_FILES = {
filepath.name: filepath
for filepath in (REPOSITORY_DIRECTORY / "routines").rglob("*.openbb")
for filepath in (MISCELLANEOUS_DIRECTORY / "routines").rglob("*.openbb")
}
self.ROUTINE_FILES.update(
{
Expand Down Expand Up @@ -594,13 +596,14 @@ def call_exe(self, other_args: List[str]):


# pylint: disable=global-statement
def terminal(jobs_cmds: List[str] = None, appName: str = "gst"):
def terminal(jobs_cmds: List[str] = None, appName: str = "gst", test_mode=False):
"""Terminal Menu"""
# TODO: HELP WANTED! Refactor the appName setting if a more elegant solution comes up
if obbff.PACKAGED_APPLICATION:
appName = "gst_packaged"

setup_logging(appName)
if not test_mode:
setup_logging(appName)
logger.info("START")
log_settings()

Expand Down Expand Up @@ -793,7 +796,7 @@ def log_settings() -> None:


def run_scripts(
path: str,
path: Path,
test_mode: bool = False,
verbose: bool = False,
routines_args: List[str] = None,
Expand All @@ -812,8 +815,9 @@ def run_scripts(
One or multiple inputs to be replaced in the routine and separated by commas.
E.g. GME,AMC,BTC-USD
"""
if os.path.isfile(path):
with open(path) as fp:

if path.exists():
with path.open() as fp:
raw_lines = [x for x in fp if (not is_reset(x)) and ("#" not in x) and x]
raw_lines = [
raw_line.strip("\n") for raw_line in raw_lines if raw_line.strip("\n")
Expand Down Expand Up @@ -850,22 +854,95 @@ def run_scripts(
# TODO: Add way to track how many commands are tested
else:
if verbose:
terminal(file_cmds, appName="openbb_script")
terminal(file_cmds, appName="openbb_script", test_mode=True)
else:
with suppress_stdout():
terminal(file_cmds, appName="openbb_script")

terminal(file_cmds, appName="openbb_script", test_mode=True)
else:
console.print(f"File '{path}' doesn't exist. Launching base terminal.\n")
if not test_mode:
terminal()


def build_test_path_list(path_list: List[str], filtert: str) -> List[Path]:
if path_list == "":
console.print("Please send a path when using test mode")
return []

test_files = []

for path in path_list:
user_script_path = USER_DATA_DIRECTORY / "scripts" / path
default_script_path = MISCELLANEOUS_DIRECTORY / path

if user_script_path.exists():
chosen_path = user_script_path
elif default_script_path.exists():
chosen_path = default_script_path
else:
console.print(f"\n[red]Can't find the file:{path}[/red]\n")
continue

if chosen_path.is_file() and str(chosen_path).endswith(".openbb"):
test_files.append(chosen_path)
elif chosen_path.is_dir():
script_directory = chosen_path
script_list = script_directory.glob("**/*.openbb")
script_list = [script for script in script_list if script.is_file()]
script_list = [script for script in script_list if filtert in str(script)]
test_files.extend(script_list)

return test_files


def run_test_list(path_list: List[str], filtert: str, verbose: bool):
os.environ["DEBUG_MODE"] = "true"

test_files = build_test_path_list(path_list=path_list, filtert=filtert)
SUCCESSES = 0
FAILURES = 0
fails = {}
length = len(test_files)
i = 0
console.print("[green]OpenBB Terminal Integrated Tests:\n[/green]")
for file in test_files:
console.print(f"{((i/length)*100):.1f}% {file}")
try:
run_scripts(file, test_mode=True, verbose=verbose)
SUCCESSES += 1
except Exception as e:
fails[file] = e
FAILURES += 1
i += 1
if fails:
console.print("\n[red]Failures:[/red]\n")
for file, exception in fails.items():
logger.error("%s: %s failed", file, exception)
console.print(f"{file}: {exception}\n")
console.print(
f"Summary: [green]Successes: {SUCCESSES}[/green] [red]Failures: {FAILURES}[/red]"
)


def run_routine(file: str, routines_args=List[str]):
user_routine_path = USER_DATA_DIRECTORY / "routines" / file
default_routine_path = MISCELLANEOUS_DIRECTORY / "routines" / file

if user_routine_path.exists():
run_scripts(path=user_routine_path, routines_args=routines_args)
elif default_routine_path.exists():
run_scripts(path=default_routine_path, routines_args=routines_args)
else:
print(
f"Routine not found, please put your `.openbb` file into : {user_routine_path}."
)


def main(
debug: bool,
test: bool,
filtert: str,
paths: List[str],
path_list: List[str],
verbose: bool,
routines_args: List[str] = None,
):
Expand All @@ -890,71 +967,15 @@ def main(
"""

if test:
os.environ["DEBUG_MODE"] = "true"

if paths == "":
console.print("Please send a path when using test mode")
return
test_files = []
for path in paths:
if path.endswith(".openbb"):
file = str(REPOSITORY_DIRECTORY / path)
test_files.append(file)
else:
folder = str(REPOSITORY_DIRECTORY / path)
files = [
f"{folder}/{name}"
for name in os.listdir(folder)
if os.path.isfile(os.path.join(folder, name))
and name.endswith(".openbb")
and (filtert in f"{folder}/{name}")
]
test_files += files
test_files.sort()
SUCCESSES = 0
FAILURES = 0
fails = {}
length = len(test_files)
i = 0
console.print("[green]OpenBB Terminal Integrated Tests:\n[/green]")
for file in test_files:
file = file.replace("//", "/")
repo_path_position = file.rfind(REPOSITORY_DIRECTORY.name)
if repo_path_position >= 0:
file_name = file[repo_path_position:].replace("\\", "/")
else:
file_name = file
console.print(f"{file_name} {((i/length)*100):.1f}%")
try:
if not os.path.isfile(file):
raise ValueError("Given file does not exist")
run_scripts(file, test_mode=True, verbose=verbose)
SUCCESSES += 1
except Exception as e:
fails[file] = e
FAILURES += 1
i += 1
if fails:
console.print("\n[red]Failures:[/red]\n")
for key, value in fails.items():
repo_path_position = key.rfind(REPOSITORY_DIRECTORY.name)
if repo_path_position >= 0:
file_name = key[repo_path_position:].replace("\\", "/")
else:
file_name = key
logger.error("%s: %s failed", file_name, value)
console.print(f"{file_name}: {value}\n")
console.print(
f"Summary: [green]Successes: {SUCCESSES}[/green] [red]Failures: {FAILURES}[/red]"
)
run_test_list(path_list=path_list, filtert=filtert, verbose=verbose)
return

if debug:
os.environ["DEBUG_MODE"] = "true"
if isinstance(paths, list) and paths[0].endswith(".openbb"):
run_scripts(paths[0], routines_args=routines_args)
elif paths:
argv_cmds = list([" ".join(paths).replace(" /", "/home/")])
if isinstance(path_list, list) and path_list[0].endswith(".openbb"):
run_routine(file=path_list[0], routines_args=routines_args)
elif path_list:
argv_cmds = list([" ".join(path_list).replace(" /", "/home/")])
argv_cmds = insert_start_slash(argv_cmds) if argv_cmds else argv_cmds
terminal(argv_cmds)
else:
Expand Down

0 comments on commit 77106c2

Please sign in to comment.