diff options
author | montezdesousa <79287829+montezdesousa@users.noreply.github.com> | 2023-03-06 20:25:02 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-06 15:25:02 -0500 |
commit | f2316b15e8eb7111cf16d3d31893e415ef784a76 (patch) | |
tree | 2551560720a4a9ad765e6692331c016869665f4a | |
parent | 2e841b34d449b2e80720ac6dedcfbddd3071a764 (diff) |
Allow `stocks/load --file` from anywhere (#4410)
* allow load file from anywhere
* ruff
---------
Co-authored-by: James Maslek <jmaslek11@gmail.com>
-rw-r--r-- | openbb_terminal/parent_classes.py | 28 | ||||
-rw-r--r-- | openbb_terminal/stocks/stocks_controller.py | 4 |
2 files changed, 14 insertions, 18 deletions
diff --git a/openbb_terminal/parent_classes.py b/openbb_terminal/parent_classes.py index a7e7aef4f2f..bd60472e968 100644 --- a/openbb_terminal/parent_classes.py +++ b/openbb_terminal/parent_classes.py @@ -1088,6 +1088,13 @@ class StockBaseController(BaseController, metaclass=ABCMeta): self.suffix = "" # To hold suffix for Yahoo Finance self.add_info = stocks_helper.additional_info_about_ticker("") self.TRY_RELOAD = True + self.USER_IMPORT_FILES = { + filepath.name: filepath + for file_type in ["csv"] + for filepath in ( + get_current_user().preferences.USER_CUSTOM_IMPORTS_DIRECTORY / "stocks" + ).rglob(f"*.{file_type}") + } def call_load(self, other_args: List[str]): """Process load command.""" @@ -1208,25 +1215,10 @@ class StockBaseController(BaseController, metaclass=ABCMeta): # This seems to block the .exe since the folder needs to be manually created # This block makes sure that we only look for the file if the -f flag is used # Adding files in the argparse choices, will fail for the .exe even without -f - STOCKS_CUSTOM_IMPORTS = ( - get_current_user().preferences.USER_CUSTOM_IMPORTS_DIRECTORY - / "stocks" - ) - try: - file_list = [x.name for x in STOCKS_CUSTOM_IMPORTS.iterdir()] - if ns_parser.filepath not in file_list: - console.print( - f"[red]{ns_parser.filepath} not found in custom_imports/stocks/ " - "folder[/red]." - ) - return - except Exception as e: - console.print(e) - return - - df_stock_candidate = stocks_helper.load_custom( - str(STOCKS_CUSTOM_IMPORTS / ns_parser.filepath) + file_location = self.USER_IMPORT_FILES.get( + ns_parser.filepath, ns_parser.filepath ) + df_stock_candidate = stocks_helper.load_custom(str(file_location)) if df_stock_candidate.empty: return is_df = isinstance(df_stock_candidate, pd.DataFrame) diff --git a/openbb_terminal/stocks/stocks_controller.py b/openbb_terminal/stocks/stocks_controller.py index 7431695b70a..5e9c9557779 100644 --- a/openbb_terminal/stocks/stocks_controller.py +++ b/openbb_terminal/stocks/stocks_controller.py @@ -90,6 +90,10 @@ class StocksController(StockBaseController): if session and get_current_user().preferences.USE_PROMPT_TOOLKIT: choices: dict = self.choices_default + choices["load"] = { + "--file": {c: None for c in self.USER_IMPORT_FILES}, + "-f": "--file", + } self.completer = NestedCompleter.from_nested_dict(choices) def print_help(self): |