Files
OpenBB/website/controller_doc_classes.py
James Maslek 099a9db7dc Release -> develop (#4581)
* Update trailmap.py (#4300)

* Fixes Failing AutoDocs (#4304)

* Fixes Failing AutoDocs

* Update test_print_help.txt

* Release 2.5.0 (#4355)

* Rename SDK folder

* Fixing all the tests I hope

* Depreciate the sia menu

* Fix routine script

* Update SDK generation scripts

* This needed fixing anyways

* Regenerate SDK files

* Update paths in sdk helpers

* Regenerate the remains of the SDK

* Fix candle not working with symbol via the sdk

* Fix pydocstyle errors

* deps

* Add in Docstrings

* Push to website since I need to upload images

* typos

* Add in Fixed Income Guide

* Tests : fix cassette

* changes to layout and adding more content

* trailmap

* poetry

* updates to requirements files

* add `_try_to_login` to sdk generation, fix `_chart` in `about()` url

* sentiment analysis

* refactor

* changing to base model default

* rounding

* renewing poetry

* fixed poetry

* spacing

* black

* adds selectable models for realized volatility calculations

* fixes line too long

* Add intro guides to Fixed Income

* Fix some missing docstrings

* Fix website not working

* missed a few

* fix /economy/test_yfinance_view tests

* fix etf/test_stockanalysis_model.py

* fix etf/test_stockanalysis_view.py

* fix test_stocks_controller

* deprecating non used function

* update financedatabase to 2.0.4

* Delete SIA from OpenBB Docs and SDK

* Searching was not done properly for short name and index

* changes the default data source for crypto.load

* Fix tests I broke

* Little fixy fix to remove duplicates

* Fix tests

* typo

* adds check for empty dataframe

* Patch Equity Report

* Update so that image links to website

* linting v2

* silly imports

* adding tempfile

* add imap unordered

* reordering imports

* Adding databento as a source (#4293)

* First commit

* Baby steps

* Keys and sdk

* Fix import for api key

* Add SDK endpoints

* Test + api checks

* tests

* roque print of api key

* Changes

* minor

* custom timeout

* improve timeouts

---------

Co-authored-by: hjoaquim <h.joaquim@campus.fct.unl.pt>

* Tests

* Integration test

* frozendict remove

* update spec

* rename sdk faq to troubleshooting

* add hidden import

* add hidden imports

* imports again

* valid url check

* new lines and prompt

* split imports

* more spec

* adds cones to the crypto-ta menu

* Fix tests

* Update code to reflect new changes to FinanceDatabase

* Small bugfix for DCF if you have no ticker loaded

* remove yfinance from news

* add reverse fx choices

* black

* ruff

* Add options greeks to the SDK

* Linting

* Delete rogue file and update test and add fixedincome to en.yml

* fix for options screener

* ruff

* updates tests for options screener

* reqs

* updates more test files

* rewrite for test_github_model

* rewrite test for test_syncretism_view

* removes test_view_available_presets[]

* black

* coverage

* adds option screener preset

* Update directories.py

* len & set

* set all list

* adds unit testing for cones

* ruff

* pylint

* adds options screener endpoint to SDK

* test markers

* Testing draft PR mode

* Update package to v2.0.7

* Remove etf/screener and tests

* Updated

* Merge reqs

* Updated readme

* hook test for pyinstaller

* Little fixy fix

* Remove etf/scr from SDK

* Update tests

* Update hook-whisper.py

* oops lol

* Update sdk

* update

* Etf

* removed old trailmap csv's

* Update fixedincome text

* add greeks to trail

* rouge file

* merge killed it

* fix test

* fix build failure

* catch for error message

* fix for frozendict

* caching search and prompting on hub

* whisper model prompts

* Fix long/short issue and fix transform issue

* separates volatility models from cones function

* Introduce CPI functionality

* improves error handling for volatility models when window length is too small

* ur welcome <3

* additional sanity check for volatility_view

* updates volatility_model test

* black

* Fix linting

* Make cpi a little bit more robust

* Add in missing value

* sdk updates

* update dep files

* Update trail_map_forecasting.csv

* import ordering pylint?

* fix trailing comma

* sorted trailmaps

* remove one test artifact

* rewrite test

* Adds the missing pieces.

* adds clickable URL in keys + Databento

* adds databento images

* adds images for Databento

* update deps

* req files

* swap order in databento

* ruff

* mypy

* lint

* sdk

* makes requested changes

* ruff v2..

* replaces screenshot.

* Removed because it shouldn't have been in this PR.

* ruff v3

* Minor edit, move installation to position 2 instead of last

* util linting changes

* linting for controller

* fix test

* test

* Revert some tests

* Guess not

* see if this sticks

* fixes

* Fix linters.  Add --video if not provided

* Checkout develop tests for keys

* Release time

* Revert papermill and update gh pages action

* Fix shortname (#4356)

* uninstall brotli on conda test

* Fix issue 4316 (#4357)

* Fix issue 4316

* Updating polygon_model + options_controller

* Tests : FRED

* ruff

---------

Co-authored-by: Theodore Aptekarev <aptekarev@gmail.com>
Co-authored-by: Jeroen Bouma <jer.bouma@gmail.com>
Co-authored-by: Danglewood <85772166+deeleeramone@users.noreply.github.com>
Co-authored-by: hjoaquim <h.joaquim@campus.fct.unl.pt>
Co-authored-by: Chavithra PARANA <chavithra@gmail.com>
Co-authored-by: teh_coderer <me@tehcoderer.com>
Co-authored-by: martinb-bb <105685594+martinb-bb@users.noreply.github.com>
Co-authored-by: Diogo Sousa <montezdesousa@gmail.com>
Co-authored-by: northern-64bit <75195383+northern-64bit@users.noreply.github.com>
Co-authored-by: luqazino@gmail.com <luqazino@gmail.com>
Co-authored-by: colin99d <colin99delahunty@gmail.com>
Co-authored-by: montezdesousa <79287829+montezdesousa@users.noreply.github.com>
Co-authored-by: andrewkenreich <andrew.kenreich@gmail.com>

* Release/2.5.1 (#4368)

* Fix routine script

* Update SDK generation scripts

* This needed fixing anyways

* Regenerate SDK files

* Update paths in sdk helpers

* Regenerate the remains of the SDK

* Fix candle not working with symbol via the sdk

* Fix pydocstyle errors

* deps

* Add in Docstrings

* Push to website since I need to upload images

* typos

* Add in Fixed Income Guide

* Tests : fix cassette

* changes to layout and adding more content

* trailmap

* poetry

* updates to requirements files

* add `_try_to_login` to sdk generation, fix `_chart` in `about()` url

* sentiment analysis

* refactor

* changing to base model default

* rounding

* renewing poetry

* fixed poetry

* spacing

* black

* adds selectable models for realized volatility calculations

* fixes line too long

* Add intro guides to Fixed Income

* Fix some missing docstrings

* Fix website not working

* missed a few

* fix /economy/test_yfinance_view tests

* fix etf/test_stockanalysis_model.py

* fix etf/test_stockanalysis_view.py

* fix test_stocks_controller

* deprecating non used function

* update financedatabase to 2.0.4

* Delete SIA from OpenBB Docs and SDK

* Searching was not done properly for short name and index

* changes the default data source for crypto.load

* Fix tests I broke

* Little fixy fix to remove duplicates

* Fix tests

* typo

* adds check for empty dataframe

* Patch Equity Report

* Update so that image links to website

* linting v2

* silly imports

* adding tempfile

* add imap unordered

* reordering imports

* Adding databento as a source (#4293)

* First commit

* Baby steps

* Keys and sdk

* Fix import for api key

* Add SDK endpoints

* Test + api checks

* tests

* roque print of api key

* Changes

* minor

* custom timeout

* improve timeouts

---------

Co-authored-by: hjoaquim <h.joaquim@campus.fct.unl.pt>

* Tests

* Integration test

* frozendict remove

* update spec

* rename sdk faq to troubleshooting

* add hidden import

* add hidden imports

* imports again

* valid url check

* new lines and prompt

* split imports

* more spec

* adds cones to the crypto-ta menu

* Fix tests

* Update code to reflect new changes to FinanceDatabase

* Small bugfix for DCF if you have no ticker loaded

* remove yfinance from news

* add reverse fx choices

* black

* ruff

* Add options greeks to the SDK

* Linting

* Delete rogue file and update test and add fixedincome to en.yml

* fix for options screener

* ruff

* updates tests for options screener

* reqs

* updates more test files

* rewrite for test_github_model

* rewrite test for test_syncretism_view

* removes test_view_available_presets[]

* black

* coverage

* adds option screener preset

* Update directories.py

* len & set

* set all list

* adds unit testing for cones

* ruff

* pylint

* adds options screener endpoint to SDK

* test markers

* Testing draft PR mode

* Update package to v2.0.7

* Remove etf/screener and tests

* Updated

* Merge reqs

* Updated readme

* hook test for pyinstaller

* Little fixy fix

* Remove etf/scr from SDK

* Update tests

* Update hook-whisper.py

* oops lol

* Update sdk

* update

* Etf

* removed old trailmap csv's

* Update fixedincome text

* add greeks to trail

* rouge file

* merge killed it

* fix test

* fix build failure

* catch for error message

* fix for frozendict

* caching search and prompting on hub

* whisper model prompts

* Fix long/short issue and fix transform issue

* separates volatility models from cones function

* Introduce CPI functionality

* improves error handling for volatility models when window length is too small

* ur welcome <3

* additional sanity check for volatility_view

* updates volatility_model test

* black

* Fix linting

* Make cpi a little bit more robust

* Add in missing value

* sdk updates

* update dep files

* Update trail_map_forecasting.csv

* import ordering pylint?

* fix trailing comma

* sorted trailmaps

* remove one test artifact

* rewrite test

* Adds the missing pieces.

* adds clickable URL in keys + Databento

* adds databento images

* adds images for Databento

* update deps

* req files

* swap order in databento

* ruff

* mypy

* lint

* sdk

* makes requested changes

* ruff v2..

* replaces screenshot.

* Removed because it shouldn't have been in this PR.

* ruff v3

* Minor edit, move installation to position 2 instead of last

* util linting changes

* linting for controller

* fix test

* test

* Revert some tests

* Guess not

* see if this sticks

* fixes

* Fix linters.  Add --video if not provided

* Checkout develop tests for keys

* merge release into develop (#4359)

* Update trailmap.py (#4300)

* Fixes Failing AutoDocs (#4304)

* Fixes Failing AutoDocs

* Update test_print_help.txt

* Release time

* Revert papermill and update gh pages action

* Fix shortname (#4356)

* uninstall brotli on conda test

* Fix issue 4316 (#4357)

* Fix issue 4316

* Updating polygon_model + options_controller

* Tests : FRED

* ruff

---------

Co-authored-by: teh_coderer <me@tehcoderer.com>
Co-authored-by: Jeroen Bouma <jer.bouma@gmail.com>
Co-authored-by: Chavithra <chavithra@gmail.com>
Co-authored-by: hjoaquim <h.joaquim@campus.fct.unl.pt>

* Add pydantic to reqs and get ffmpeg in docker image

* initialize session (#4360)

* Unpin old version of jsonschema (#4365)

* puts pip install openbb[all] inside quotes (#4366)

Co-authored-by: James Maslek <jmaslek11@gmail.com>

* Merge deps

* Hotfix/bug #3837 (#3942)

* Fix: Fixed spaces causing error in --sort flag at crypto/disc/...

* Fixed minor issues

* fixed formatting using black

* Ran tests locally

* fixed top command

* reverse flag to gainers

* reverse flag to losers

* fixed dappradar verbose exceptions

* tests

* tests

* tests

---------

Co-authored-by: James Maslek <jmaslek11@gmail.com>
Co-authored-by: Henrique Joaquim <h.joaquim@campus.fct.unl.pt>

* Update release files

---------

Co-authored-by: jeroen <jer.bouma@gmail.com>
Co-authored-by: Danglewood <85772166+deeleeramone@users.noreply.github.com>
Co-authored-by: Theodore Aptekarev <aptekarev@gmail.com>
Co-authored-by: hjoaquim <h.joaquim@campus.fct.unl.pt>
Co-authored-by: Chavithra PARANA <chavithra@gmail.com>
Co-authored-by: teh_coderer <me@tehcoderer.com>
Co-authored-by: martinb-bb <105685594+martinb-bb@users.noreply.github.com>
Co-authored-by: Diogo Sousa <montezdesousa@gmail.com>
Co-authored-by: northern-64bit <75195383+northern-64bit@users.noreply.github.com>
Co-authored-by: luqazino@gmail.com <luqazino@gmail.com>
Co-authored-by: colin99d <colin99delahunty@gmail.com>
Co-authored-by: montezdesousa <79287829+montezdesousa@users.noreply.github.com>
Co-authored-by: andrewkenreich <andrew.kenreich@gmail.com>
Co-authored-by: Brendan de Faria <brendan.defaria@gmail.com>

* hotfix/Fix troubleshooting and adding lines (#4379)

* Release Branch

* fix print_pretty_prediction, avoid error on DummyBackend.__version__

* Update gh-pages.yml

* fix removed start_required_configurations

* fix gh-pages yarn lockfile, integration tests libs

* ship it

---------

Co-authored-by: teh_coderer <me@tehcoderer.com>
Co-authored-by: Theodore Aptekarev <aptekarev@gmail.com>
Co-authored-by: Jeroen Bouma <jer.bouma@gmail.com>
Co-authored-by: Danglewood <85772166+deeleeramone@users.noreply.github.com>
Co-authored-by: hjoaquim <h.joaquim@campus.fct.unl.pt>
Co-authored-by: Chavithra PARANA <chavithra@gmail.com>
Co-authored-by: martinb-bb <105685594+martinb-bb@users.noreply.github.com>
Co-authored-by: Diogo Sousa <montezdesousa@gmail.com>
Co-authored-by: northern-64bit <75195383+northern-64bit@users.noreply.github.com>
Co-authored-by: luqazino@gmail.com <luqazino@gmail.com>
Co-authored-by: colin99d <colin99delahunty@gmail.com>
Co-authored-by: montezdesousa <79287829+montezdesousa@users.noreply.github.com>
Co-authored-by: andrewkenreich <andrew.kenreich@gmail.com>
Co-authored-by: Brendan de Faria <brendan.defaria@gmail.com>
2023-03-24 13:31:05 +00:00

520 lines
17 KiB
Python

# pylint: disable=unused-argument
import argparse
import inspect
import sys
from datetime import datetime, timedelta
from importlib.util import module_from_spec, spec_from_file_location
from inspect import FullArgSpec, getmembers, isclass
from pathlib import Path
from types import FunctionType, ModuleType
from typing import Any, Dict, List, Optional
from unittest.mock import patch
import pandas as pd
from pandas.tseries.holiday import USFederalHolidayCalendar
import openbb_terminal
import openbb_terminal.config_terminal as cfg
from openbb_terminal.core.plots.backend import plots_backend # noqa: F401
from openbb_terminal.decorators import disable_check_api
from openbb_terminal.helper_funcs import (
EXPORT_BOTH_RAW_DATA_AND_FIGURES,
EXPORT_ONLY_FIGURES_ALLOWED,
NO_EXPORT,
set_command_location,
)
from openbb_terminal.parent_classes import BaseController
from openbb_terminal.rich_config import console
from openbb_terminal.sdk import openbb
from openbb_terminal.stocks.comparison_analysis import finviz_compare_model
disable_check_api()
CRYPTO_DATA = openbb.crypto.load("BTC", to_symbol="usd", source="YahooFinance")
ETF_DATA = openbb.etf.load("SPY")
ETF_DATA.index.name = "date"
FOREX_DATA = openbb.forex.load(to_symbol="USD", from_symbol="EUR")
FOREX_DATA.index.name = "date"
STOCK_DATA = openbb.stocks.load("AAPL", start_date="2022-01-01")
STOCK_DATA.index.name = "date"
data_dict = {
"stocks": {
"data": STOCK_DATA,
"symbol": "AAPL",
},
"crypto": {
"data": CRYPTO_DATA,
"symbol": "BTC",
},
"forex": {
"data": FOREX_DATA,
"symbol": "EURUSD",
},
"futures": {
"data": STOCK_DATA,
"symbol": "ES",
},
"etf": {
"data": ETF_DATA,
"symbol": "SPY",
},
}
sub_folders_abbr = {
"discovery": "disc",
"due_diligence": "dd",
"overview": "ov",
"alternative": "alt",
"cryptocurrency": "crypto",
"behavioural_analysis": "ba",
"comparison_analysis": "ca",
"dark_pool_shorts": "dps",
"portfolio_optimization": "po",
"quantitative_analysis": "qa",
"technical_analysis": "ta",
"tradinghours": "th",
"fundamental_analysis": "fa",
"mutual_funds": "funds",
"government": "gov",
"insider": "ins",
}
sub_names_full = {
"alt": "Alternative",
"ba": "Behavioural Analysis",
"ca": "Comparison Analysis",
"crypto": "Cryptocurrency",
"dd": "Due Diligence",
"defi": "DeFi",
"disc": "Discovery",
"dps": "Darkpool Shorts",
"etf": "ETFs",
"fa": "Fundamental Analysis",
"forecast": "Forecasting",
"funds": "Mutual Funds",
"gov": "Government",
"ins": "Insiders",
"keys": "Keys",
"nft": "NFTs",
"onchain": "OnChain",
"ov": "Overview",
"po": "Portfolio Optimization",
"qa": "Quantitative Analysis",
"screener": "Screener",
"ta": "Technical Analysis",
"th": "Trading Hours",
}
required_flags = {
"crypto": {
"funot": ["-p", "ethereum"],
},
"econometrics": {
"coint": ["-t"], # TODO: add choice
"norm": ["-v"], # TODO: add choice
},
}
def get_expiration_date():
"""Gets the next expiration date for controller init"""
dt = datetime.now()
bdays_indx = pd.bdate_range(
dt.strftime("%Y-%m-%d"),
(dt + timedelta(days=20)).strftime("%Y-%m-%d"),
freq=pd.offsets.CustomBusinessDay(calendar=USFederalHolidayCalendar()),
).tolist()
expiration = [x.strftime("%Y-%m-%d") for x in bdays_indx if x.weekday() == 4][0]
return expiration
start_date = (datetime.now() - timedelta(days=365)).strftime("%Y-%m-%d")
end_date = datetime.now().strftime("%Y-%m-%d")
param_name_to_value = {
"expiration": get_expiration_date(),
"start_date": start_date,
"start": start_date,
"end_date": end_date,
"end": end_date,
"from_symbol": "USD",
"to_symbol": "EUR",
"vs": "USDT",
}
param_type_to_value = {
(dict, Dict): {},
list: [],
str: "",
int: 0,
float: 0.0,
bool: False,
}
def get_parameters(
controller_cls: BaseController, name: str, df_loaded: pd.DataFrame, symbol: str
) -> Dict[str, Any]:
"""Gets the parameters of a controller's `__init__` signature. If required parameters are missing,
we get the type and use a default value for it.
Parameters
----------
controller_class: Type[BaseController]
The controller class
name: str
The name of the controller
df_loaded: pd.DataFrame
The dataframe loaded from the `load` function
symbol: str
The symbol to use on controller init
Returns
-------
dict[str, Any]
The parameters with their values for controller init
"""
signature = inspect.signature(controller_cls) # type: ignore
kwargs: Dict[str, Any] = {}
for param in signature.parameters.values():
if param.name in ("ticker", "symbol", "coin"):
kwargs[param.name] = symbol
elif param.name == "data" and name in ("forecast", "qa"):
kwargs["data"] = df_loaded
elif (
param.default is inspect.Parameter.empty
and param.kind is not inspect.Parameter.VAR_KEYWORD
):
for param_name, value in param_name_to_value.items():
if param.name == param_name:
kwargs[param.name] = value
break
if param.name not in kwargs:
for param_type, value in param_type_to_value.items():
if isinstance(param_type, tuple):
if param.annotation in param_type:
kwargs[param.name] = {symbol: df_loaded}
break
elif param.annotation is pd.DataFrame:
kwargs[param.name] = df_loaded
break
elif param.annotation is param_type:
kwargs[param.name] = value
break
return kwargs
class ControllerDoc:
"""Class that retrieves the ArgumentParser for each command of the Controller and stores it in a dictionary
for use in auto-generating the documentation.
Parameters
----------
controller: BaseController
The controller to get the commands from
Attributes
----------
controller: BaseController
The controller to get the commands from
cmd_parsers: Dict[str, argparse.ArgumentParser]
A dictionary of the command name and the ArgumentParser for that command
cmd_funcs: Dict[str, FunctionType]
A dictionary of the command name and the function for that command
cmd_fullspec: Dict[str, FullArgSpec]
A dictionary of the command name and the full argument spec for that command
ignore: List[str]
A list of commands to ignore
commands: List[str]
A list of commands to document
Methods
-------
get_commands()
Get commands
get_command_parser(command: str)
Get the parser for a command
get_all_command_parsers()
Get all command parsers
has_commands()
Checks if controller has commands to document
"""
def __init__(self, controller: BaseController, trailmap: str):
self.trailmap = trailmap
self.name = trailmap.split(".")[-1]
params = data_dict.get(trailmap.split(".")[0], data_dict["stocks"])
self.symbol = params.get("symbol", "AAPL")
self.current_df = params.get("data", STOCK_DATA)
self.controller: BaseController = controller( # type: ignore
**get_parameters(controller, self.name, self.current_df, self.symbol)
)
root_menu = self.controller.path[0]
sub_menu = self.controller.path[1] if len(self.controller.path) > 1 else None
if sub_menu and sub_menu:
console.print(
f"[bold green]Loading {root_menu}[/] [bold yellow]{sub_menu}[/]"
)
else:
console.print(f"[bold green]Loading {root_menu}[/]")
self.cmd_parsers: Dict[str, argparse.ArgumentParser] = {}
self.cmd_funcs: Dict[str, FunctionType] = {}
self.cmd_fullspec: Dict[str, FullArgSpec] = {}
self.image_exportable: Dict[str, bool] = {}
self.ignore = [
"call_help",
"call_exit",
"call_clear",
"call_cls",
"call_quit",
"call_about",
"call_reset",
"call_support",
"call_wiki",
"call_record",
"call_stop",
"call_screenshot",
]
self.commands: List[str] = self.get_commands()
if self.name == "options" and hasattr(self.controller, "selected_date"):
self.controller.selected_date = get_expiration_date()
elif self.name == "ca" and hasattr(self.controller, "similar"):
self.controller.similar = finviz_compare_model.get_similar_companies(
self.symbol, ["Sector", "Industry"]
)
if hasattr(self.controller, "current_currency"):
self.controller.current_currency = "usdt"
if hasattr(self.controller, "source") and trailmap.split(".")[0] == "crypto":
self.controller.source = "YahooFinance"
for attr in ["ticker", "symbol", "coin", "etf_name"]:
if hasattr(self.controller, attr):
setattr(self.controller, attr, self.symbol)
for attr in ["current_df", "etf_data"]:
if hasattr(self.controller, attr):
setattr(self.controller, attr, self.current_df)
self.get_all_command_parsers()
def get_commands(self) -> List[str]:
"""Get commands"""
commands = []
for name, _ in getmembers(self.controller, predicate=inspect.ismethod):
if name.startswith("call_") and name not in self.ignore:
func = getattr(self.controller, name)
if hasattr(func, "__wrapped__"):
while hasattr(func, "__wrapped__"):
func = func.__wrapped__
self.cmd_funcs[name] = func
self.cmd_fullspec[name] = inspect.getfullargspec(func)
if "_" not in self.cmd_fullspec[
name
].args and "from openbb_terminal." not in inspect.getsource(func):
commands.append(name)
return commands
def get_command_parser(self, command: str) -> Optional[argparse.ArgumentParser]:
"""Get command parser"""
if command not in self.cmd_parsers:
self._get_parser(command)
if command in self.cmd_parsers:
return self.cmd_parsers[command]
return None
def _get_parser(self, command: str) -> None:
"""Get parser information from source"""
self.image_exportable[command] = False
def mock_func(fparser: argparse.ArgumentParser, *args, **kwargs):
"""Mock function to get the parser"""
allowed = [EXPORT_BOTH_RAW_DATA_AND_FIGURES, EXPORT_ONLY_FIGURES_ALLOWED]
export = kwargs.get("export_allowed", NO_EXPORT)
if export in allowed:
self.image_exportable[command] = True
else:
for arg in args:
if arg in allowed:
self.image_exportable[command] = True
break
self.cmd_parsers[command] = fparser
try:
with patch.object(
self.controller, "parse_known_args_and_warn", new=mock_func
) as _:
args = {}
fullspec = self.cmd_fullspec[command]
if "_" in fullspec.args:
return
if len(fullspec.args) > 2:
args.update({arg: ["1234"] for arg in fullspec.args[2:]})
with patch("openbb_terminal.rich_config.console.print"):
try:
_ = getattr(self.controller, command)(["--help"], **args)
except (SystemExit, AttributeError):
pass
except Exception as e:
print(e)
def run_image_exports(self) -> None:
"""Run image exports"""
if not self.image_exportable:
return
for command in self.commands:
self.get_image_export(command)
def get_image_export(self, command: str) -> None:
"""Get image export"""
if not self.image_exportable[command]:
return
console.print(
f"[yellow]Exporting {command} image for {self.name} controller[/]"
)
try:
cmd_name = command.replace("call_", "")
other_args = [
"--export",
f"{'_'.join(self.controller.path)}_{cmd_name}.png",
]
for action in self.cmd_parsers[command]._actions: # pylint: disable=W0212
if action.dest == "strike":
other_args.extend(["--strike", "100"])
if action.dest == "target_dataset":
other_args.extend(["--dataset", "AAPL"])
if action.dest == "ticker":
other_args.extend(["--ticker", self.symbol])
if self.name == "forecast" and action.dest == "values":
other_args.extend(["--values", "AAPL.close"])
set_command_location(f"{'/'.join(self.controller.path)}/{cmd_name}")
_ = getattr(self.controller, command)(other_args)
except (SystemExit, KeyError):
pass
def get_all_command_parsers(self) -> None:
"""Get all command parsers"""
for command in self.commands:
self.get_command_parser(command)
def has_commands(self) -> bool:
"""Checks if controller has commands"""
return len(self.commands) > 0
class LoadControllersDoc:
"""Class that loads all controllers and creates a ControllerDoc class instance for each one
Attributes
----------
controller_docs: Dict[str, ControllerDoc]
A dictionary of the controller name and the ControllerDoc class instance for that controller
Methods
-------
get_controllers()
Gets all controllers to create a ControllerDoc class instance for
get_controller_doc(controller: str)
Gets the ControllerDoc class instance for a controller
available_controllers()
Gets a list of available controllers names
"""
def __init__(self) -> None:
self.controller_docs: Dict[str, ControllerDoc] = {}
self.get_controllers()
def get_controllers(self) -> None:
"""Gets all controllers"""
for trailmap, module in self._get_modules().items():
for name, obj in getmembers(module):
if ( # noqa: SIM102
name != "TerminalController" and "BaseController" not in name
): # noqa: SIM102
if isclass(obj) and issubclass(obj, BaseController):
if trailmap not in self.controller_docs:
ctrl = ControllerDoc(obj, trailmap) # type: ignore
if ctrl.has_commands():
self.controller_docs[trailmap] = ctrl
def _get_modules(self) -> Dict[str, ModuleType]:
"""Gets all controllers modules"""
modules = {}
terminal_path = Path(openbb_terminal.__file__).parent
for file in terminal_path.glob("**/*controller.py"):
spec = spec_from_file_location(file.stem, file)
if spec is not None and spec.loader is not None:
module = module_from_spec(spec)
spec.loader.exec_module(module)
ctrl_path = (
str(file)
.replace(str(terminal_path), "")
.replace("\\", "/")
.split("/")[1:]
)
for sub_name, abbr in sub_folders_abbr.items():
ctrl_path = [
path.lower().replace(sub_name, abbr) for path in ctrl_path
]
trailmap = ".".join(ctrl_path[:-1])
if trailmap not in modules:
modules[trailmap] = module
return modules
def get_controller_doc(self, controller_name: str) -> ControllerDoc:
"""Get the ControllerDoc instance for a controller"""
if controller_name not in self.controller_docs:
raise KeyError(f"Controller {controller_name} not found")
return self.controller_docs[controller_name]
def available_controllers(self) -> List[str]:
"""Get available controllers"""
return [ctrl for ctrl in self.controller_docs if ctrl != ""]
if __name__ == "__main__":
try:
run_image_exports = "--images" in sys.argv
load_controllers = LoadControllersDoc()
for loaded in load_controllers.available_controllers():
controller_doc = load_controllers.get_controller_doc(loaded)
controller_doc.get_all_command_parsers()
if run_image_exports:
cfg.setup_config_terminal()
plots_backend().start()
plots_backend().isatty = True
controller_doc.run_image_exports()
except KeyboardInterrupt:
sys.exit(0)