mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-11-25 02:33:10 +00:00
start routines
This commit is contained in:
@@ -1,19 +1,19 @@
|
||||
import argparse
|
||||
import logging
|
||||
from typing import List, Dict
|
||||
from pathlib import Path
|
||||
|
||||
from prompt_toolkit.completion import NestedCompleter
|
||||
from openbb_terminal.featflags_controller import FeatureFlagsController
|
||||
|
||||
from openbb_terminal.parent_classes import BaseController
|
||||
from openbb_terminal.rich_config import console, MenuText
|
||||
from openbb_terminal.decorators import log_start_end
|
||||
from openbb_terminal.core.config.paths import (
|
||||
USER_ROUTINES_DIRECTORY,
|
||||
)
|
||||
from openbb_terminal.core.config.paths import USER_ROUTINES_DIRECTORY
|
||||
from openbb_terminal import feature_flags as obbff
|
||||
from openbb_terminal.menu import session
|
||||
from openbb_terminal.session.user import User
|
||||
from openbb_terminal.session import hub_model as Hub
|
||||
from openbb_terminal.session import local_model as Local
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -45,7 +45,8 @@ class AccountController(BaseController):
|
||||
self.ROUTINE_CHOICES = {filename: None for filename in self.ROUTINE_FILES}
|
||||
if session and obbff.USE_PROMPT_TOOLKIT:
|
||||
choices: dict = {c: {} for c in self.controller_choices}
|
||||
choices["send"] = self.ROUTINE_CHOICES
|
||||
choices["upload"]["--file"] = self.ROUTINE_CHOICES
|
||||
choices["upload"]["-f"] = choices["upload"]["--file"]
|
||||
|
||||
self.completer = NestedCompleter.from_nested_dict(choices)
|
||||
|
||||
@@ -76,13 +77,41 @@ class AccountController(BaseController):
|
||||
pass
|
||||
|
||||
@log_start_end(log=logger)
|
||||
def call_upload(self, _):
|
||||
def call_upload(self, other_args: List[str]):
|
||||
"""Upload"""
|
||||
console.print("Upload routine")
|
||||
pass
|
||||
parser = argparse.ArgumentParser(
|
||||
add_help=False,
|
||||
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
|
||||
prog="load",
|
||||
description="Load your portfolio transactions.",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-f",
|
||||
"--file",
|
||||
type=str,
|
||||
dest="file",
|
||||
required="-h" not in other_args,
|
||||
help="The file to be loaded",
|
||||
choices={c: {} for c in self.ROUTINE_FILES},
|
||||
metavar="FILE",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-n",
|
||||
"--name",
|
||||
type=str,
|
||||
dest="name",
|
||||
required="-h" not in other_args,
|
||||
help="The name of the routine",
|
||||
)
|
||||
if other_args and "-" not in other_args[0][0]:
|
||||
other_args.insert(0, "-f")
|
||||
ns_parser = self.parse_known_args_and_warn(parser, other_args)
|
||||
if ns_parser:
|
||||
routine = Local.get_routine(ns_parser.file)
|
||||
if routine:
|
||||
Hub.upload_routine(ns_parser.name, routine)
|
||||
|
||||
@log_start_end(log=logger)
|
||||
def call_download(self, _):
|
||||
"""Download"""
|
||||
console.print("Download routine")
|
||||
pass
|
||||
Hub.download_routine()
|
||||
|
||||
@@ -187,3 +187,59 @@ def logout_everywhere() -> Optional[requests.Response]:
|
||||
except Exception:
|
||||
console.print("[red]\nFailed to logout remotely.[/red]")
|
||||
return None
|
||||
|
||||
|
||||
def upload_routine(
|
||||
name: str = "",
|
||||
routine: str = "",
|
||||
) -> Optional[requests.Response]:
|
||||
"""Send a routine to the server."""
|
||||
|
||||
data = {"name": name, "script": routine}
|
||||
|
||||
try:
|
||||
response = requests.post(
|
||||
headers={"Authorization": User.get_token()},
|
||||
url=BASE_URL + "terminal/script",
|
||||
json=data,
|
||||
)
|
||||
if response.status_code == 200:
|
||||
console.print("[green]Successfully uploaded your routine.[/green]")
|
||||
else:
|
||||
console.print("[red]Error uploading your routine.[/red]")
|
||||
return response
|
||||
except requests.exceptions.ConnectionError:
|
||||
console.print("[red]Connection error.[/red]")
|
||||
return None
|
||||
except Exception:
|
||||
console.print("[red]Failed to upload your routine.[/red]")
|
||||
return None
|
||||
|
||||
|
||||
def download_routine(
|
||||
name: str = "",
|
||||
) -> Optional[requests.Response]:
|
||||
"""Download a routine from the server."""
|
||||
console.print("[red]Not implemented yet.[/red]")
|
||||
|
||||
# TODO: Implement when endpoint is ready.
|
||||
|
||||
# data = {"name": name}
|
||||
|
||||
# try:
|
||||
# response = requests.get(
|
||||
# headers={"Authorization": User.get_token()},
|
||||
# url=BASE_URL + "terminal/script",
|
||||
# json=data,
|
||||
# )
|
||||
# if response.status_code == 200:
|
||||
# console.print("Successfully downloaded your routine.")
|
||||
# else:
|
||||
# console.print("[red]Error downloading your routine.[/red]")
|
||||
# return response
|
||||
# except requests.exceptions.ConnectionError:
|
||||
# console.print("[red]Connection error.[/red]")
|
||||
# return None
|
||||
# except Exception:
|
||||
# console.print("[red]Failed to download your routine.[/red]")
|
||||
# return None
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
import json
|
||||
from openbb_terminal.core.config.paths import SETTINGS_DIRECTORY
|
||||
from typing import Optional
|
||||
from openbb_terminal.core.config.paths import (
|
||||
SETTINGS_DIRECTORY,
|
||||
USER_ROUTINES_DIRECTORY,
|
||||
)
|
||||
from openbb_terminal.rich_config import console
|
||||
|
||||
from openbb_terminal import feature_flags as obbff
|
||||
@@ -88,10 +92,12 @@ def apply_configs(configs: dict):
|
||||
|
||||
if configs:
|
||||
settings = configs.get("features_settings", {})
|
||||
|
||||
# TODO: Find a cleaner way to check if user has sync enabled.
|
||||
obbff.SYNC_ENABLED = is_sync_enabled(settings)
|
||||
User.update_flair()
|
||||
|
||||
if obbff.SYNC_ENABLED:
|
||||
console.print("Applying...", style="red")
|
||||
if settings:
|
||||
for k, v in settings.items():
|
||||
if hasattr(obbff, k):
|
||||
@@ -144,3 +150,20 @@ def cast_set_attr(obj, name, value):
|
||||
setattr(obj, name, int(value))
|
||||
else:
|
||||
setattr(obj, name, value)
|
||||
|
||||
|
||||
def get_routine(name: str) -> Optional[str]:
|
||||
"""Get the routine.
|
||||
|
||||
Returns
|
||||
-------
|
||||
str
|
||||
The routine.
|
||||
"""
|
||||
try:
|
||||
with open(USER_ROUTINES_DIRECTORY / name) as f:
|
||||
routine = "".join(f.readlines())
|
||||
return routine
|
||||
except Exception:
|
||||
console.print("[red]\nFailed to find routine.[/red]")
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user