Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow stocks/load --file from anywhere #4410

Merged
merged 4 commits into from
Mar 6, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 10 additions & 18 deletions openbb_terminal/parent_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1088,6 +1088,13 @@ def __init__(self, queue):
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."""
Expand Down Expand Up @@ -1208,25 +1215,10 @@ def call_load(self, other_args: List[str]):
# 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)
Expand Down
4 changes: 4 additions & 0 deletions openbb_terminal/stocks/stocks_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ def __init__(self, queue: Optional[List[str]] = None):

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):
Expand Down